What is Problem?
if app started as portrait then make activity as portrait , otherwise landscape , but don't detect sensor based or used based rotation otherwise.
My Solution:
Make variable isPortrait in application class and detect orientation in onCreate of Application class then use this variable in all activites to detect what orientation to use.
Problem in My Solution:
No Idea how can I change different configuration layouts and map them accordingly in this scenario , what if user change it orientation to landscape after app started in portrait ? is there any ActivityInfo flag to achieve this behaviour.
use this in your activity tag in manifest
android:configChanges="orientation"
android:screenOrientation="portrait"
In Mainfest within the atvitity tag add below code
android:configChanges="orientation|screenSize"
and in ur activity override onConfiguration method like below
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
log.e("onConfigurationChanged", "screen orientation change");
}
Related
I am stuck with my issue.Thing is that its a custom Tab Widget.In that have multiple
tab like Home - News - Abc - PQR .
The Activity should be for both orientation like portrait and landscape. so for that each tab have two xml for portrait which is store at layout-port/file.xml and landscape which store at layout-land/file.xml
For manage orientation portrait to landscape i have added android:configChanges="orientation|keyboardHidden" rule tag in each activity.
TAB_SAMPLE.java Tab file.
TAB_GROUP_ACTIVITY each Tab Group activity File
file.java Task file
After all this stuff i get issue here :
If i addandroid:configChanges="orientation|keyboardHidden"rule tag in tab_sample activity then its working perfect. like manage different view. port to land and land to port but its not working in Home.java.
Now if i remove android:configChanges="orientation|keyboardHidden" rule tag in tab_sample activity then its working for Home activity not for News.java
Mean when i change the orientation its keeping same xml form port not use from layout-land.in the sense its call OnCreate() again.
So as i found may be issue is in Tab Widget.
Update
Now after tracing my code i get that main issue is in grid view activity because its only activity which is not working.
Issue is between Tab host v/s Grid View. I don't know why its not taking layout-land xml file. i found this as same issue but no replay on that question also
see in Detail manifestfile.xml
I want to maintain both portrait and landscape in all activity.
Both XML File
Please help me how to solve this.
Oooohhh Finally i got the solution for above issue. It's was very difficult.
For maintain the state of orientation Landscape to portrait and vice-versa we generally adding android:configChanges="keyboardHidden|orientation" property tag under activity.
But here may be issue is Tab_Group_ Activity due to that i am not able to maintain state in GridView. Grid_File.java is Only single java file which was not handling the orientation rest of all other working perfectly.
Now if i remove android:configChanges="keyboardHidden|orientation" from TAB_SAMPLE.java then Its handling only Grid_File.java not others.
mean that was keeping same Layout XML in landscape also where i have two separate XML File.
Here is my solution:
I have add android:configChanges="keyboardHidden|orientation" in TAB_SAMPLE.java as well as
implement onConfigurationChanged(Configuration newConfig) and set Number of column of grid. like gridView.setNumColumns(6);
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// gridView.setSelection(index);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// Log.e("On Config Change", "LANDSCAPE");
gridView.setNumColumns(6);
} else
{
// Log.e("On Config Change", "PORTRAIT");
gridView.setNumColumns(4);
}
}
Generally we are adding either android:configChanges="keyboardHidden|orientation" tag under activity or implementing onConfigurationChanged(Configuration newConfig) but here i have written both.
How can I retain same state in landscape.
when I rotted the screen in landscape mode then activity is restarted and
my layout view is showing wrong. so please help me.
you may save your state in a Bundle in onSaveInstanceState() method and restore it back in onCreate() or in onRestoreInstanceState().
or if you want to disable orientation changes, you may add android:screenOrientation="landscape" to your Activity in the manifest file.
If your speaking that your layout doesnt look/fit properly in landscape mode then you will need a new xml with the same name & place it inside a new folder called "layout-land". That should help
For retain same state in landscape mode you must add android:configChanges="orientation|keyboard" in your Current Activity to Handle Configuration Change in your on way as:
<activity
android:configChanges="orientation|keyboard"
...
/>
or for Changing Layout or any View on configChanges you must override onConfigurationChanged of Activity as:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
//Handle config changes here, paying attention to
//the newConfig.orientation value
super.onConfigurationChanged(newConfig);
}
and if you want to save state on any view on Configuration Chnages then you must override onSaveInstanceState() for saving state of view like TextView, EditText,View Layout's .. and onRestoreInstanceState() for Retrieving state back after Device mode rotation.
for more info about how we handle runtime changes you can also see this android topic :
Handling Runtime Changes
I want to stop activity refresh when I move on to portrait mood to landscape mood and I also want load file layout-land when it move to portrait to landscape mood without any refreshment the activity.
I use <activity android:name=".Login"
android:configChanges="orientation|screenSize">
but,In this method when I move onto portrait to landscape mood ,It does not load the file from layout-land folder. what should I do for this? please someone help me.
Call method setContentView(R.layout.main) in onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
///in case you have some EditTexts , save their input before setting the new layout.
emailForConfigChanges = emailTextBox.getText().toString().trim();
passwordForConfigChanges = passwordTextBox.getText().toString().trim();
setContentView(R.layout.main);
//Now set the values back to the EditTexts .
}
You just have to add this method to your activity as it handles all the orientation changes if you have declared "orientation" in your manifest.
EDIT : And if EditTexts lose their values on rotation, get their values before calling setContentView() in onConfigurationChanged(). See the edit above.
I handled this problem only by addding android:configChanges="keyboardHidden|orientation"
hope this will help you.
Welcome all
I have a special need for my app. I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. Why? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again.
Please, can someone tell me how to achieve this?
I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest.
I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; and then overriding onConfigurationChanged() in my activity. But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called......
Thanks
Why not use the SensorManager to monitor when the phone has rotated 90 degrees. Actually this may be helpful also.
use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. Give the same layout both in portrait and landscape mode. when orientation change occurs, that method wil be called then change the image as u like
Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.
Here a example of your onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); // tem que ter
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}
Is it possible to start an intent when the phone goes from portrait to landscape mode and vice versa?
Thanks!
Yes it is possible.
Add the following to the activity declaration in the manifest:
android:configChanges="orientation"
so it looks like
<activity android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:name=".your.package">
Write the code to whatever you want in onConfigurationChanged() method
#Override
public void onConfigurationChanged(Configuration newConfig) {
//write your logic here
System.out.println("...inside config change...");
super.onConfigurationChanged(newConfig);
}
Have you tried to implement the OrientationEventListener and load the Intent on onOrientationChanged?
Create a static field in your Activity and check the current orientation in onCreate() using Configuration. If the field is empty, store the configuration, if it's not, check for changes, take the necessary actions and store the new configuration afterwards.