Android - Vertical layout only - android

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.

Related

Orientation change notification in 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 !!!

Android orientation change even after locking it in system settings

as in the topic, ive got a trouble with locking screen orientation after its been locked in device's settings. Its a landscape app. Ive read couple threads about similar problem but havent got any revelant answer.
Thanks in advance
Mac
You can add android:screenOrientation="landscape" in your activity tag to specify the mode of screen orientation in your manifest file
<activity
android:name="com.xyz.myapp.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
Hope it helps .
Add setRequestedOrientation in the onCreate method like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.homepage);
This would set to landscape if auto_rotate is off, otherwise if would rotate normally as long as it wasn't set to land/port in the manifest.
if(android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) != 1){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

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

ActivityNotFoundException invoking an inner-class activity

I have a non-Activity class (let's call it "NonActivity") that needs to post a message and get user feedback. I have a message activity (MsgActivity) class to do this. But only Activity classes can call startActivityForResult() so I made an inner helper class in NoActivity:
// just to provide an Activity to launch MsgActivity
class ActivityMsgClass extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent iMA = new Intent(this, MsgActivity.class);
iMA.putExtra("MsgText", mParams[0]);
...blah blah ...
iMA.putExtra("ButtonCode", iBtns);
startActivityForResult(iMA,3);
}
}
My Activity class is declared in the Manifest thusly:
<activity android:name="ActivityMsgClass"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:launchMode="singleInstance"></activity>
But when I try to invoke it . . .
Intent i = new Intent(ctx, ActivityMsgClass.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
... I get an ActivityNotFound exception. I've also tried it without the FLAG_ACTIVITY_NEW_TASK, and I've also tried qualifying the name in the manifest, e.g.,
<activity android:name=".NoActivity.ActivityMsgClass"
. . . to no avail. What am I doing wrong?
Thanks in advance.
The technical solution is to specify the fully qualified activity path in the manifest.
The actual solution is to avoid doing this. Let activity be a public class, and not an inner class, this is just not good practice.
In your manifeast file change like below and try...
<activity android:name=".ActivityMsgClass"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:launchMode="singleInstance"></activity>
<activity android:name=".MsgActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:launchMode="singleInstance"></activity>
your activity name declaration should start with a period. declare it this way.
<activity android:name=".ActivityMsgClass"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:launchMode="singleInstance"></activity>
also make sure that the activity MsgActivity is also declared in the manifest.

how to not re-create itself after screen orientation change?

I created two layout folder in the layout-land,layout-port containing same files to pick layout depends on the mode change and i added this in my activity.
public void onConfigurationChanged(Configuration newConfig
{super.onConfigurationChanged(newConfig);setContentView(R.layout.main);}
and in manifest and android:configChanges="orientation";
<activity android:name = "DomainList"
android:configChanges = "keyboardHidden|orientation">
</activity>

Categories

Resources