I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity> element/s in
the manifest and you're done.
You can follow the logic below to prevent auto rotate screen while your AsyncTask is running:
Store your current screen orientation inside your activity using getRequestedOrientation().
Disable auto screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
Run/execute your AsyncTask.
At the end of your AsyncTask restore your previous orientation status using setRequestedOrientation(oldOrientation).
Please note that there are several ways to access Activity (which runs on UI thread) properties inside an AsyncTask. You can implement your AsyncTask as an inner class or you can use message Handler that poke your Activiy class.
The easiest way I found to do this was to put
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
setContentView(R.layout.activity_main);
so...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
Rather than going into the AndroidManifest, you could just do this:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
#Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
Activity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="QRCodeActivity" android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
User "portrait" in your AndroidManifest.xml file might seem like a good solution. But it forces certain devices (that work best in landscape) to go into portrait, not getting the proper orientation. On the latest Android version, you will get wearing an error. So my suggestion it's better to use "nosensor".
<activity
...
...
android:screenOrientation="nosensor">
Add the following to your AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
android:configChanges="orientation"
So, the full activity node would be:
<activity android:name="Activity1"
android:icon="#drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
Prevent Screen Rotation just add this following line in your Manifests.
<activity
android:name=".YourActivity"
android:screenOrientation="portrait" />
This works for me.
android:screenOrientation="portrait"
on application tag <application
and
<activity
...
...
android:screenOrientation="locked">
If you are using Android Developer Tools (ADT) and Eclipse
you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. Finally, select your preferred orientation.
You can select one of the many options.
You have to add the following code in the manifest.xml file.
The activity for which it should not rotate, in that activity add this element
android:screenOrientation="portrait"
Then it will not rotate.
You can try This way
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Use AsyncTaskLoader to keep your data safe even if the activity changes, instead of using AsyncTask that is a better way to build apps than preventing screen rotation.
Related
App is crashing on orientation change,fragment has Fragmentstatepager
Crash log:
java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.ja va:569)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
Solution: I fixed the bug by replacing getChildManager() with getSupportManager()
Write following code in your Manifest file...
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
and write following code in YourActivity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Hope this helps you..
you can lock the orientation by adding the following code
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should read and learn the official tutorial Android-Google about Layout.
For have a good showing, you have to add your elements (Button, TextView..) on multiple Layout.
After your App will be automatically resize depending orientation and size of your phone.
Or you can just locked orientation in your Manifest app
android:screenOrientation="landscape" or "portrait"
Good Luck.
EDIT :
Official :
http://developer.android.com/guide/topics/ui/declaring-layout.html
An other :
http://code.tutsplus.com/tutorials/android-user-interface-design-layout-basics--mobile-3671
I am recently facing a problem while creating new android application in both eclipse as well as android studio.
I am using same SDK in both eclipse and android studio.
When I create a new application and i simply run it. The launcher activity was getting loaded twice.
Means, I am getting MainActivity loading twice one on top of another.
OnCreate() method of my MainActivity also got invoked twice.
The code goes like follows
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("OnCreate Invoked");
}
}
Output
OnCreate Invoked
OnCreate Invoked
Can you please help me in resolving this issue.
Thanks in advance.
It seems you are getting multiple instance of your first activity.
Use this in manifest of 1st activity:
android:launchMode="singleTop"
If I set orientation programatically in onCreate - I can reproduce this issue.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
If needed, set activity orientation in the manifest file like this.
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I want to keep landscape mode always.
I Lock screen to portrait mode on Nexus7, I set android:screenOrientation="sensorLandscape" and android:configChanges="orientation|keyboardHidden"in AndroidManifest.xml, when I go to other activity and back to this activity, then the screen will rotate portrait and rotate to landscape again (Didn't call onCreate method again), but I don't want to rotate the screen in any situations, how to fix it?
I think this is the answere you're looking for:
stackoverflow.com/a/2730894/2249774
For All your Activities in the manifest set
android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"
Example :-
<activity android:name="com.example.test.testActivity" android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"">
<intent-filter>
<action android:name="com.example.test.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Repeat this for all activities used in the manifest..
Try editing your AndroidManifest.xml to look like this
<activity
android:name=".YOURACTIVITYNAME"
android:screenOrientation="landscape" />
In your AndroidManifest file, try typing in the following code:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
example:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="#string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>
I got a EditText on my first tab. Of course, soft keyboard appearing when I touch on my EditText. But when I access to second tab, it means second tab activity, soft keyboard stay appearing on my first tab and don't disappear even though there is no EditText on my second tab activity.
I want to prevent softkeyboard from appearing when I access to my second tab.
How can I solve this problems?
Here's my manifest files
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidaccountbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.androidaccountbook.AccountTabActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<!-- screenOrientation prevent from rotation -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.androidaccountbook.accountMainThread"
android:windowSoftInputMode="stateHidden" />
<!-- activity for intent -->
<activity android:name="com.example.androidaccountbook.TodayExpenseList"
android:windowSoftInputMode="stateHidden" />
<activity android:name="com.example.androidaccountbook.settingThread"
android:windowSoftInputMode="stateHidden"/>
<activity android:name="com.example.androidaccountbook.FailedConnectDatabase"
android:windowSoftInputMode="stateHidden"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
I figured how to works on it and I tried it, it works.
Because I want to access to second activity, and surely life-activity onPause method will occurs. So, I wrote the following hideKeyboard method on my onPause method.
This is how onPause method looks like
#Override
protected void onPause(){
super.onPause();
hideKeyboard(this,editText);
}
This is my hideKeyboard method looks like
public void hideKeyboard(Context context, EditText text){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
The best solution would be: (In the Manifest file)
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />
I use this and it always works:
<activity
android:name="MyProfileActivity"
android:windowSoftInputMode="stateHidden|adjustPan" />
I am using Samsung Galaxy Note to run a phonegap application.
When I take picture, I can see that screen rotates just before entering camera.
How can I disable this?
I have tried to force portrait orientation on both, the main thread & camera activity,
but those do not seem to work:
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".App" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"
android:label="#string/app_name" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.phonegap.DroidGap" android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
</intent-filter>
</activity>
<activity android:name="com.android.camera.Camera"
android:screenOrientation="portrait">
</activity>
</application>
You need to add the permission on the manifest:
<uses-feature android:name="android.hardware.screen.portrait" />
Doc android of balise uses-feature
For example, if your app requires portrait orientation, you should declare <uses-feature android:name="android.hardware.screen.portrait"/> so that only devices that support portrait orientation (whether always or by user choice) can install your app. If your application supports both orientations, then you don't need to declare either.
How do you take picture? If you are sending intent to fire up camera application you lose control about orientation changes. Also note, that before going to sleep and locking screen launcher forces your application into portrait with an butt-kick regardles of the settings or overriding methods. Most probably you can not and behaviour on other devices could be different.
Use android:configChanges="orientation|keyboardHidden" where you are setting screen as portrait.
There are 2 things here that should be taken into account. First, this
android:screenOrientation="landscape"
has no effect if in the java code there is something like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Then, there is this other method that could be bothering you:
mCamera.setDisplayOrientation(90);
This would rotate your camera preview.
Hope this helps!