Orientation change notification in Android - android

Trying to follow something. Boiling down to simplest: In my AndroidManifest.xml I have the following:
<application
android:allowBackup="true"
android:configChanges="orientation"
...
</application>
In my activity that runs I have this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d("PrimesAreInP", "onConfigurationChanged called");
}
However when I change the orientation of the phone from portrait to landscape I never see the onConfigurationChanged() print. What else do I need to do to see this call?

Try adding this for activity tag:
<activity android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboa‌​rdHidden"

I think you are missing this dude :
super.onConfigurationChanged(newConfig);
and not forget to write this line in activity tag of your menifest :
android:configChanges="orientation|screenSize"
good luck !!!

Related

Android: How to force reverse portrait orientation even if the device auto-rotate screen is off?

I'm working on an app/game that should switch between portrait and reversePortrait in multiplayer mode. (Mainly because each player might use the keyboard on his/her turn while the device is fixed on the table between them.)
My app works fine when the auto-rotate is ON.. But the reversePortrait orientation is NEVER achievable when the device auto-rotate is OFF!
What I did so far is that I've set the orientation in the Manifest file:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
then I change the orientation programmatically in runtime by calling this method when needed:
public void rotateScreen(boolean reverse){
if (reverse) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
But that still doesn't force to reversePortrait orientation if the device auto-rotation is OFF.
I also tried theonConfigurationChanged() method, but it didn't work as well. I think it only gets called AFTER changing the orientation already, not before!
I even tried screenOrientation="reversePortrait" in the Manifest file, but even that is ineffective when auto-rotation is OFF.
I don't know what exactly you want to achieve. Call your method in OnConfigurationChanged
#Override
public void onConfigurationChanged(Configuration newConfig) {
rotateScreen(true);
super.onConfigurationChanged(newConfig);
}
but here once first launch the screen is reversed but you can apply this as per your requirements.
Please provide more details what you want to achive
After lots of researching, seems that the only way to override the device locked orientation and set the screen to a reverse portrait orientation is to force-enabling the device auto-rotate.
first, must add this permission in the manifest file above the tag:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
then, define something like that in the mainActivity:
public boolean enableAutoRotate(){
boolean isAutoRotate = android.provider.Settings.System
.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
if (!isAutoRotate){
try {
android.provider.Settings.System
.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1);
} catch (Exception e){
e.printStackTrace();
}
}
isAutoRotate = android.provider.Settings.System
.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
return isAutoRotate;
}
The try and catch is important to prevent the app from crashing if the user didn't give or removed the permission.
then, set the orientation when needed using something like this:
public void rotateScreen(boolean reverse){
if (enableAutoRotate()){
if (reverse) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
rotateScreen method can also be boolean instead of void, if needed.

Orientation changes calls onCreate() method every time in android Kitkat

I already know that to stop being called OnCreate method on change of orientation in android.
and below is the code for that too.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden" />
But Its not working in my Google Nexus 5. Because it has the android kitkat os. Where as in Samsung Mega android 4.2 its working.
So what is the code for android kitkat to stop being call OnCreate() on orientation change.
I also fixed orientation using below code
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
But that code is also not working.
(Edited) To fix orientation I need to add android:screenOrientation="portrait"code in Activity tag. Thanks #RupaliG
Thanks
Use a flag check in onCreate. Make flag=true at the time of initialization/declaration
add android:configChanges="orientation" in ur manifest file
In your Java file override the onConfigurationChanged method and make the flag as false.
After doing so ur onCreate will be called but the code mention in if won't be called.
Move ur code inside the if condition.
try using this.
static boolean flag = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(!flag)
return;
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
Log.d("ONCONFIGCHANGE", "CALLED" );
flag = false;
super.onConfigurationChanged(newConfig);
}
try this one from 2.3.3+ :
android:configChanges="orientation|keyboardHidden|screenSize"

Locking orientation to portrait force close in my phone

I want my app to lock into portrait mode. For this I used this code :
<activity android:name="MyActivity"
android:label="#string/app_name" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
>
and in MyActivity class :
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
This code work correctly in emulator, but when I install and run in my phone, the app is force closed.
How can I solve this problem?
Yo already use in manifest then there is no need to use by pragmatically. So remove the onConfigurationChanged method from your code.
I am using
android:screenOrientation="portrait"
in the manifest file, no other code in Java side and is working.
I see two problems here:
Remove android:configChanges="orientation|keyboardHidden" from manifest
Remove onConfigurationChanged from java
and it should work just fine

Disable screen rotating on Android [duplicate]

This question already has answers here:
How do I disable orientation change on Android?
(13 answers)
Closed 10 years ago.
When I press a button, I would like to disable screen rotation on all my activities. How can I do that?
BTW, the phone can be located in landscape or portrait position when the user click the button.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can change your AndroidManifest.xml to
<activity android:name="MainActivity" android:configChanges="orientation">
Which informs the OS that you will handle these config changes (by doing nothing.)
See How do I disable orientation change on Android?
I've found the solution:
in manifest.xml:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:name="MyApplication">
in MyApplication.java:
public class UPackingListApplication extends Application {
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = [orientation we wanna to use (according to ActivityInfo class)];
super.onConfigurationChanged(newConfig);
}
}

Android - Vertical layout only

How do I make sure my app is only for vertical layout?
I have tried android:screenOrientation="portrait" but that doesn't seem to do the trick.
You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.
<activity android:name=".MyActivity"
android:label="My Activity"
android:screenOrientation="portrait">
Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.
If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:
public abstract class BasePortraitActivity extends Activity {
#Override
protected final void onCreate(Bundle state) {
super.onCreate(state);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
performOnCreate(state);
}
protected abstract void performOnCreate(Bundle state);
}
And than just extend BasePortraitActivity where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to YourActivity.onCreate().
you have to change into AndroidManifest.xml.
for each activity you have to insert:
android:configChanges = "orientation"
android:screenOrientation = "portrait"
for e.g.:
<activity android:name=".YourActivityName"
android:label="#string/app_name"
android:configChanges = "orientation"
android:screenOrientation = "portrait">
This works for a single activity.. But there seems no application wide setting there.
In your manifest under activity add this :
<activity
android:name="com.zeus.MyProject"
android:screenOrientation="portrait"
>
Activity is to block just in case want to block all and only repeat this line of code in their Activitys.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Here, talk to the java does not want that the user can rotate their activity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.
android:configChanges="orientation"
android:screenOrientation="portrait">
Add android:configChanges="keyboardHidden|orientation" to the activity.
Put this attribute in layout root:
android:orientation="vertical"
It may help.
Just to confirm Pentium10's answer.
I was having a similar issue and adding android:screenOrientation="portrait" to the activity tag.
Did the trick for me.

Categories

Resources