Set screen orientation for the whole app - java

I am implementing a way where the user can disable screen rotation from the app settings. If the box is checked then any Activity can be automatically rotated and follow the phone rotation settings. If not checked then the autorotation is disabled.
I know how to do it per Activity like this
if(!GlobalVar.sharedPreferences_static.isAutoRotate()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
Is there a way I can do this once for all the activities (the whole app) instead of doing it for every Activity? Thank you.

If I understood your problem correctly, here is what you can do: create an empty Activity (without setting its content) like this:
public class EmptyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your screen orientation logic here
}
}
and then you make all other Activities extend the EmptyActivity. So, you just need to implement your screen orientation logic once.

The same thing can be done in the manifest with:
android:screenOrientation="landscape"
but that attribute doesn't work if (only) applied to the application tag. You'd still have to put it on every activity, but at least it's syntactically easier.

You just add this line into your activity like this
<activity
android:name="Package name"
android:label="App anem"
android:screenOrientation="landscape" //Orientation
>
</activity>
Or you can set like this programmatically like this in the starting of the activity...
if (Config.sDeviceType.equalsIgnoreCase("MO")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Related

How to use onBackPressed in an activity that swipes the screen with another activity?

I've a little problem. I've two activities (GalleryActivity and GalleryVideoActivity) and after swiping between them, i would to come back to the first activity (GalleryActivity) pressing just one time on back button because i've to press back button as many times as i swipped. Is it possible? Thanks in advance to everyone!
So if i understand your issue is that you are moving between activities and everytime you move they create new instances so as a solution to suggest is as following ,
in your manifest file , in activity section add launchMode="SingleInstance" , as this would create only one instance of that activity .
This is an example
<activity
android:name=".ui.apppassword.PasswordRestoreActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
In GalleryVideoActivity override onBackpress method and navigate back to the GalleryActivity by putting simple intent.
Here is what you need to add to GalleryVideoActivity in order to implement onBackPressed as Mike stated:
#Override
public void onBackPressed() {
Intent toGalleryActivity = new Intent(this, GalleryActivity.class);
startActivity(toGalleryActivity);
}
This starts GalleryActivity everytime the back button is pressed in GalleryVideoActivity.

Call Activity by items in Navigation Drawer

I am building an application for android and I am not able to make that when I click on an item inside my Navigation drawer it opens a new activity pre established by me. can you help me?
I already tried some code using intents but when I click on the item it ends up closing the application.
case R.id.homepage: {
homepage();
break;
}
case R.id.pdefault: {
testdefault();
break;
}
private void homepage(){
startActivity(new Intent(getBaseContext(),MainActivity.class));
}
private void testdefault(){
startActivity(new Intent(getBaseContext(),testdefault.class));
}
The first case worked normally because I created a method for
start the activity, but I did the same for the second and it did not work.
The expected result is just a simple screen swap.
How did you create the activity? That name is not possible.
I recommend you to use right click - new - activity - empty activity. That will create the structure needed to recognize it as an activity.
Take into account that if you create it manually you will have to override from AppCompatActivity and declare the activity in the manifest as:
<activity android:name=".SomeActivity" />
It is not just creating a class. Make sure not to use underscores for activity names, keep the format of MainActivity

How to lock one activities orientation,then have another on top which is free to rotate?

I am developing an app for Android where I use the front camera as a mirror and have controls on top to do various functions. Now I was wondering how I can get the MainActivity to stay locked as 'portrait' and have another activity on top (which has all the buttons and has a transparent background) to have freedom to rotate?
Thanks
So you have two activites, one for which orientation is fixed and another one is free to rotate.
For the activity for which orientation is to be fixed, add this line in its activity tag in Manifest.xml :
<activity
android:name=".MyPreferencesActivity"
android:screenOrientation="landscape"/>
And by default the other activity is free to rotate.Mark the answer as verified if it works.
The above way is static (defined in xml).Ii it doesn't work, try this programmatical(can be changed during runtime) method also:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onButtonPressed(Uri uri) {getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}

android activity restarted when orientation changes

I read many posts about this problem like [this link][1] and one solution is add orientation configChanges to manifest and handle onConfigurationChanged event in order to prevent onCreate activity to be called again when rotation. I did it and event is triggered properly, however, after this execution, onCreate method is also executed! why? what I am missing? Thank you
manifest,
<activity
android:name="webPush"
android:configChanges="keyboardHidden|orientation"/>
activity,
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.vistaaib);
}
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.vistaaib);
...
I think this will work.........
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Beginning with Android 3.2 (API level 13), the will "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher you must use
android:configChanges="orientation|screenSize"
I Did this.
I added This code to manifest and it works perfect.
<activity
android:name="?"
android:label="#string/?"
android:theme="#style/?"
android:configChanges="orientation|screenSize">
You will need to add this under you activity if you want to change something when the device is rotation.
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();
}
}
Write this two lines of code in manifest file in that Activity.
Seem this will solve your problem.<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden"/>
Your activity will be restarted on any configuration change. Most likely it is being restarted because the keyboard state changes. Try adding this to the activity's attributes:
android:configChanges="orientation|keyboard|keyboardHidden"
If you are working for API level 12 or lower
In menifest file, put following just after declaring your Activity Name.
android:configChanges="orientation"
e.g.-
<activity
android:name=".NameOfYourActivity"
android:configChanges="orientation"/>
And in android 3.2(API level 13) or higher version screen size also changes on rotation changes so declare this too.
for this,
android:configChanges="orientation|screenSize"
Following Could be the reason
Event :screenSize
The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
Added in API level 13.
so along with "orientation" add "screenSize" as well

Android onConfigurationChanged not being called

I am having trouble with telling Android to not call onCreate() when the orientation changes. I have added android:configChanges="orientation" to my manifest but still when the orientation changes onCreate() is called. Here is my code.
AndroidManifest.xml
<activity android:name="SearchMenuActivity" android:theme="#android:style/Theme.NoTitleBar" android:configChanges="orientation"></activity>
SearchMenuActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the current layout to the search_menu
setContentView(R.layout.search_menu_activity);
Log.d(TAG, "onCreate() Called");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
//don't reload the current page when the orientation is changed
Log.d(TAG, "onConfigurationChanged() Called");
super.onConfigurationChanged(newConfig);
}
And my LogCat Output
06-23 12:33:20.327: DEBUG/APP(2905): onCreate() Called
//Orientation Changes
06-23 12:33:23.842: DEBUG/APP(2905): onCreate() Called
Does anyone know what I am doing wrong? Thanks.
This was my gremlin for the ~same problem:
Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must decalare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).
(From http://developer.android.com/guide/topics/resources/runtime-changes.html)
TL;DR: add "|screenSize" to android:configChanges="orientation" for API 14+
A couple of things to try:
android:configChanges="orientation|keyboardHidden|screenSize" rather than android:configChanges="orientation"
Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); anywhere. This will cause onConfigurationChange() to not fire.
Check that you are not using android:screenOrientation in your manifest.
If none of that works, read through the Android doc on handling runtime changes and make sure you are doing everything correctly. There may be something somewhere else in your code that's causing the problem. http://developer.android.com/guide/topics/resources/runtime-changes.html
EDIT: As derrik pointed out, I assumed that you were changing the configuration with the accelerometer detecting what way the device was facing. If you want the configuration to change as the keyboard is shown/hidden the configChanges in the manifest must include keyboardHidden as well.
Try use this one.....
android:configChanges="orientation|keyboardHidden|screenSize"
You should change the configChanges entry in AndroidManifest.xml to:
android:configChanges="keyboardHidden|orientation"
Otherwise, sliding the keyboard doesn't trigger onConfigurationChange() even though the orientation changes. I just tested this on my HTC Desire Z.
Add this to your manifest to each activity.
android:configChanges="keyboardHidden|orientation|screenSize"
Then, override onConfigurationChanged on your activity as such
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Manifest:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
Activity & Fragment:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged " +
(newConfig.orientation
== Configuration.ORIENTATION_LANDSCAPE
? "landscape" : "portrait"));
}
Output:
10-29 21:53:26.951 D/FragmentOne: onConfigurationChanged landscape
10-29 21:53:26.951 D/MainActivity:onConfigurationChanged landscape
It wasn't triggering in my Fragment (Fragments aren't registered in the AndroidManifest.xml) so I had to move it to the Activity managing my Fragment, in my case the TabsPagerActivity.
Few things can be cross check:
In my case I was tracking language change of device using the onConfigurationChanged.
Few things need to know about onConfigurationChanged is there is some difference in behavious between onConfigurationChanged of Activity and Application.
When you change the configuration of device the Application level onConfigurationChanged will be call automatically and immediately but onConfigurationChanged of activity will call when you navigate to that activity.
and another thing is only locale in manifest will not work sometime So you have declare other config change event along with the locale(in my case) that should be like android:configChanges="layoutDirection|locale"

Categories

Resources