How can I detect screen rotation? - android

is it possible to detect screen rotation? I mean - rotation only, which is clearly distinguishable from activity initialization from another activity?
The onXxx methods seem not to be useful for this, I have tried adding/removing a flag from the starting Intent (the removing seems not to be reflected, on rotate the flag is there), and have tried adding android:configChanges="orientation" for the activity in the manifest, however the onConfigurationChanged method seems to be called every second rotation... wired.
I guess I am missing something... but haven't found clear solution in the other related threads.
Any ideas?

Manifest:
<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>
Activity:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
try this link also
How do I detect screen rotation

Use onConfigurationChanged method to detect the screen rotation isn't good idea. Configuration Change in this activity wouldn't works correctly when user rotate the screen.
So I use this solution to solve this problem.
public class SampleActivity extends AppCompatActivity {
public static final String KEY_LAST_ORIENTATION = "last_orientation";
private int lastOrientation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
lastOrientation = getResources().getConfiguration().orientation;
}
}
#Override
protected void onStart() {
super.onStart();
checkOrientationChanged();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
}
private void checkOrientationChanged() {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation != lastOrientation) {
onScreenOrientationChanged(currentOrientation);
lastOrientation = currentOrientation;
}
}
public void onScreenOrientationChanged(int currentOrientation) {
// Do something here when screen orientation changed
}
}
But code still not good enough to use it in my project, so I applied this code to my own library (https://github.com/akexorcist/ScreenOrientationHelper).
compile 'com.akexorcist:screenorientationhelper:<latest_version>'
You can create base activity class like this
public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper.onCreate(savedInstanceState);
helper.setScreenOrientationChangeListener(this);
}
#Override
protected void onStart() {
super.onStart();
helper.onStart();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
helper.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
helper.onRestoreInstanceState(savedInstanceState);
}
#Override
public void onScreenOrientationChanged(int orientation) {
}
}
Then extend the activity with this base class
public class MainActivity extends BaseActivity {
...
#Override
public void onScreenOrientationChanged(int orientation) {
// Do something when screen orientation changed
}
}
Done!

Why don't you try this?
in onCreated get the orientation of the phone:
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();
then, override the method onConfigurationChanged and check if the orientation has changed:
#Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation != myOrientation)
Log.v(tag, "rotated");
super.onConfigurationChanged(newConfig);
}
Don't forget to add into the manifest android:configChanges="orientation" in the activity.

Related

Button savedInstanceState and onRestoreInstanceState

Saving the State of Drawable xml Button.
private boolean mJam;
private button mSwitch;
private static final String KEY_ENABLE = "KEY_ENABLE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( null == savedInstanceState ) {
mJam = false;
} else {
mJam = savedInstanceState.getBoolean(KEY_ENABLE);
}
button_layout();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_ENABLE, mJam);
outState.putBoolean(KEY_ENABLE, mSwitch.isSelected()); // Saving the state of the
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
mSwitch.setSelected(savedInstanceState.getBoolean(KEY_ENABLE));
mJam = savedInstanceState.getBoolean(KEY_ENABLE);
super.onRestoreInstanceState(savedInstanceState);
}
Note: If you don't want to use ScreenSize in Android manifest, This code can be used,
If you want to do it Manually, then go for this one.
P.S: setting Screen Size in Manifest, never go to onCreate if the Orientation Change, It Just saved all the instance and display as it is, when
Android orientation Change.

how to set listview's position after configuration change

I'm struggling to recover my position in a listview on screen rotation configuration change.
Amongst the many things I've tried I came to this:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
...
mVisibleItem = -1;
} else {
if (savedInstanceState.containsKey(LV_VISIBLE_ITEM)) {
mVisibleItem = savedInstanceState.getInt(LV_VISIBLE_ITEM);
}
}
setRetainInstance(true);
}
and here I'm trying to set the position in the listview
#Override
public void onResume() {
super.onResume();
if (mVisibleItem > 0) {
mlvDictionaryIndex.setSelectionFromTop(mVisibleItem, 0);
}
}
However, much to my surprise, after rotating the screen and watching mVisibleItem gets set with the correct value, in onResume I see that mVisibleItem equals -1. How come?
use onSavedInstanceState to write in the bundle the returned value of ListView.onSaveInstanceState(), and restored it onActivityCreated
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mListView != null) {
outState.putParcelable(LISTVIEW_INTERNAL_STATE_KEY, mListView.onSaveInstanceState());
}
}
after the data are reload then you can call
mListView.onRestoreInstanceState(savedInstanceState.getParcelable(LISTVIEW_INTERNAL_STATE_KEY));
Override onSaveInstanceState such as below"
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("pos", pos);
}
Then in your onCreate method have read the savedInstanceState to check if this is an orientation change or a new activity.
private int pos = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
//This is a new activity
}else{
pos = savedInstanceState.getInt("");
}
Now you have the position in the list, and you can scroll to this in configuration change.
Maybe after the data in the listview is reloaded,the code below will work.
mlvDictionaryIndex.setSelectionFromTop(mVisibleItem, 0);
So you can use post() method, just like below:
post(new Runnable() {
public void run() {
mlvDictionaryIndex.setSelectionFromTop(mVisibleItem, 0);
}
});

How to scale ImageView after the orientation is changed?

When I rotate the screen, ImageView doesn't rescale image, but native Imageview rotates the image.
How can I achieve this?
Project https://github.com/jasonpolites/gesture-imageview
Manifest:
<activity android:name="ScaleTypeCenterInsidePortrait"
android:configChanges="orientation|screenSize"></activity>
My ScaleTypeCenterInsidePortrait class:
public class ScaleTypeCenterInsidePortrait extends ExampleActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scale_type_inside_portrait);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
replace these lines in your code
public class ScaleTypeCenterInsidePortrait extends ExampleActivity {
ImageView imv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imv=new ImageView(this);
imv.setimageResource(R.drawable."your image name in the drwable folder");
imv.setscaleType(ScaleType.Center_Inside);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(imv);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(imv);
}
}
}
and put you manifest these line
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">

How to clear EditText error state on orientation change

I need to clear error for EditText, when orientation screen change. I try in onStart() method set setError(null), but it doesn't work.
I tried it on the new project, but doesn't still work. Whem i click on button, field set error, when i change orientation i expect, that erro clear, but it doen't work.
public class MainActivity extends ActionBarActivity {
EditText text;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setError("Error");
}
});
}
#Override
public void onStart(){
super.onStart();
text.setError(null);
}
}
UPDATE:
It seems you have to clear errors onPause instead of onResume/onStart:
#Override
protected void onPause(){
super.onPause();
text.setError(null);
}
The above works for me.
OLD:
Android will automatically handle EditText for you, and its states, on orientation change.
If you want to handle the state yourself you need to implement and override onSaveInstanceState and not call super(). You don't need to override onRestoreInstanceState unless you need to handle restoring for other stuff.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}
This is what I will do when orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
text.setError(null);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
text.setText("Something");
}
}
And you can just the value of TextView as text.setText("") if you want to clear the "Error message" and if again the orientation changes, just set the text to anything you want.

how to store values and retrieve it when the orientation changes?

I have checked many posts, and as per that I have done coding for the orientation change. When orientation changes, the problem is I am not able to retrieve the entered values inside TextViews. Can anyone plz tell where did I go wrong?
Coding:
In manifest file for the corresponding activity I added:
android:configChanges="orientation|keyboardHidden"
In activity, I added the following methods:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
//Initialized the widgets
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//have written separate layout files for portrait and landscape
setContentView(R.layout.home_screen);
//Initialized the widgets again
retrieveSavedState(); //sets the TextViews again
}
#Override
protected void onPause() {
super.onPause();
saveState(); //save the TextView values
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
}
#Override
protected void onResume() {
super.onResume();
retrieveSavedState();
}
#Override
public Object onRetainNonConfigurationInstance()
{
final MyDataObject data = collectMyLoadedData();
return data;
}
the above method can be used to save any object to save data...
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data = loadMyData();
}
...
}
and u can retreive that object in the onCreate() which will we called when orientation changes.. after reading data from that object you can use it where ever you want..
http://developer.android.com/guide/topics/resources/runtime-changes.html
http://developer.android.com/guide/topics/resources/runtime-changes.html
other mechanisms are also there but this is the best way for my knowledge.. you can also use shared preferences to store and retrieve data...
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html

Categories

Resources