android - How do I change TextView when I rotate my device? -
i'm trying write android program in eclipse. idea of program display message in textview
saying "the device has been flipped" if device rotated 180 degrees (in xy plane, think?). i'm using rotation sensor , trying write code in onsensorchanged
event. code supposed change textview when rotation detected, not.
so questions in simple are:
- how textview change given rotation?
- how apply 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 rotation sensor. message.settext(r.string.compatible); } else { // failure! no rotation sensor. message.settext(r.string.incompatible); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if 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); } }
overide method in activity:
public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // checks orientation of 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 under activity
android:configchanges="orientation|screensize".
Comments
Post a Comment