0

I'm trying to write an Android program in eclipse. The idea of the program is to display a message in TextView saying "The device has been flipped" if the device is rotated exactly 180 degrees (in the xy plane, I think?). I'm using the rotation sensor and trying to write my code in the onSensorChanged event. Currently this code is supposed to change a TextView when any rotation is detected, however it does not.

So my questions in simple are:

  1. How do I get the textview to change given any rotation?
  2. How do I then apply this to a rotation of 180 degrees?


package com.example.rotation2;

import android.hardware.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity implements SensorEventListener {

TextView message;
private SensorManager mSensorManager;
private Sensor rotation;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);

    TextView message = ((TextView) findViewById(R.id.message_view));


    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    rotation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
    if (rotation != null){
      // Success! There's a rotation sensor.
        message.setText(R.string.compatible);
      }
    else {
      // Failure! No rotation sensor.
        message.setText(R.string.incompatible);
      }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

@Override
public void onSensorChanged(SensorEvent event) {
    message.setText(R.string.rotated);

}


}
Renjith
  • 5,783
  • 9
  • 31
  • 42
Aflah Bhari
  • 108
  • 2
  • 6

2 Answers2

0

Try this:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      txtView.setText("Orientation Changed");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        txtView.setText("Orientation Changed");
    }
}

if you want to detect keyboard visibility:

if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
  Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
  Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
Sathya
  • 678
  • 4
  • 12
  • I tried implementing that code, Sathya but unfortunately it doesn't update the TextView for some reason. I tried including them in the strings.xml and using them like that but no dice :( – Aflah Bhari Aug 28 '13 at 10:44
  • Thanks but for the time being, I won't be using or really doing anything with the keyboard. – Aflah Bhari Aug 28 '13 at 10:52
0

Overide this method in Activity:

 public void onConfigurationChanged(Configuration newConfig) { 
   super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
       Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
  }

in Android Manifest file mention this under activity

android:configChanges="orientation|screenSize".
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • I got this code working and although it does in some sense solve my first question it still doesn't solve my second. I wish to display some sort of message when the screen has been rotated be exactly 180 degrees. This is important because I will later on incorporate this code elsewhere, where instead of causing a change in a TextView it will call a particular method. TL;DR: I'd prefer to use the rotation sensor because this doesn't seem helpful in cases of 180 degrees rotation (and because of latency issues as mentioned in another comment). – Aflah Bhari Aug 28 '13 at 23:32
  • getWindowManager().getDefaultDisplay().getOrientation() from this you will get the orientaion... check the integer value with Surface.ROTATION_0,Surface.ROTATION_90,Surface.ROTATION_180,Surface.ROTATION_270 – Jagadesh Seeram Sep 03 '13 at 05:43