making steady layout position as from landscape or portrait - android

how to make the layout of the project app in android studio steady as the position i choose ,i mean how to make landscape or portrait in a steady position and don't change to vise versa as when enter the page the layout go to those positions by it's self and don't be able to be changed even if the device state changed?
< RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inside"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">

In your manifest file you can force the layout of each Activity like this
<activity android:label="#string/app_name"
android:name=".MyActivityName"
android:screenOrientation="portrait" >
/* ... */
</activity>
or
<activity android:label="#string/app_name"
android:name=".MyActivityName"
android:screenOrientation="landscape" >
/* ... */
</activity>

Related

Screen Orientation Resets When Navigating Between Activities

I have an app which navigates between 2 activities. Both activities initialize the requested orientation to the sensor:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
and I don't have any overrides in the layout files:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainListActivity"
android:background="#drawable/mainbg"
android:scaleType="centerCrop">
and my manifest is the same:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.domain.project">
<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.NoActionBar">
<activity android:name=".OneOfMyActivities"></activity>
<activity
android:name=".MainListActivity"
android:label="#string/title_main_list"
android:theme="#style/AppTheme.NoActionBar.NoActionBar" />
<activity
android:name=".DetailActivity"
android:label="#string/title_detail"
android:parentActivityName=".MainListActivity"
android:theme="#style/AppTheme.NoActionBar.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="my.domain.project.MainListActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The app functions correctly as far as updating orientation when you rotate the device. If you take the device and rotate it into landscape mode, the screen correctly swaps to landscape, and stays there. However, when you navigate to the other activity in the app, the other activity displays in portrait mode, and then immediately shows the rotate animation before going to landscape. So, it obviously knows which orientation it should be in. Why does it go from landscape to portrait and back to landscape? Is there any way to have it remember that it's already in landscape, and initialize the new activity in landscape?
Having the screen jump and rotate every time you navigate, when the user is not moving their device is rather annoying.
I've tried googling for this, but all I seem to find are answers about how to lock screen orientation, or questions asking why the activity restarts when the screen orientation changes. Neither of which are my question. I want to still allow the app to change screen orientation when the user rotates the device, I just want it to remember that state when navigating between activities.
thank you.
You can try putting this
android:screenOrientation="fullSensor"
In your activity tag and remove the then redundant
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)

screen orientation - reverse portrait

I have a very basic layout with a Frame and a TextView.
When I put my phone upside down (reverse portrait) the Text is not facing me, the screen does not rotate. Is it en intended behavior that by default the screen only rotate in 3 direction?
<TextView
android:text="Text"
android:textAlignment="center"
android:textSize="70dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
Manifest:
<activity
android:name=".Menu"
android:screenOrientation="user"/>
From what I could gather through researches it is part of the default settings of recent android OSs. Would I need to program anything complex to get this working? Or a small setting would do the job (like adding a layout)?
I target OS 5+ (API 21)
In your AndroidManifest.xml file, you need to configure the <activity> to use the orientation from the sensor. This should be the default, but you can force it to the sensor's orientations, for all 4 possible orientations, with android:screenOrientation="fullSensor"
<activity
android:name=".ui.MainActivity"
android:label="#string/app_name"
android:screenOrientation="fullSensor"
android:theme="#style/AppTheme.NoActionBar" />

how to set orientation to chrome custom tab in android?

i have given the fix orientation to my app as a portrait.i am using chrome custom tab to open another site, however this property can not be set to chrome custom tab to remain portrait. it change its orientations.
in manifest i have set activity like
<activity
android:name=".uicontent.carrental.ZoomCarDrawerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTask"
android:noHistory="false"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustPan" />
thank you.

setRequestedOrientation doesn't lock the orientation

I am trying to lock an activity to landscape mode and have an overlying activity receive and use the orientation changes. The top activity is partly transparent so it is important that the bottom activity locks and is displayed correctly. I have tried to set it programatically with setRequestedOrientation() and in the AndroidManifest.xml with android:screenOrientation="landscape"
When I lock the orientation using the manifest the top activity also locks. When I add android:configChanges="orientation|screenSize|keyboardHidden" the top activity changes orientation but so does the bottom activity. Sometimes the bottom activity goes completely blank (transparent) after the orientation changes.
This is in my AndroidManifest.xml:
<application
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TopActivity"
android:screenOrientation="sensor"
android:configChanges="orientation|screenSize|keyboardHidden"
android:theme="#style/Theme.Transparent">
</activity>
</application>
MainActivity is the bottom activity. How do I properly handle the orientation changes so that the single activities are locked the way I set them to?
just add screenOrientation to the activity which you don't want to rotate on orientation change in the manifest file like this:
<activity android:name=".Activity"
android:screenOrientation="portrait" />
All other activities will automatically rotate on orientation change.
No need to give screenOrientation for other activities.
1) To disable orientation (i.e. to prevent Android from restarting your activity if the user flips the phone), then specify "configChanges" in your AndroidManifest (like you're already doing, apparently), and then simply don't respond to any changes:
<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize">
2) Alternatively, let's say you want to force your app to "landscape" mode. Instead of "configChanges", you can specify "screenOrientation"
<activity android:screenOrientation="landscape" ... >
3) Finally, if you want to handle the orientation change, you'll need to a) save state, then b) do whatever processing you need (including, perhaps, notify other activities):
http://developer.android.com/guide/topics/resources/runtime-changes.html

Lock Horizontal View

I want to lock horizontal view in my app, is it possible?
Help is highly appreciated,
Thanks,
Use android:screenOrientation="landscape" attribute for your activity.
add this code in your menifest...
<activity android:name=".activity"
android:configChanges="orientation" android:screenOrientation="portrait"></activity>
For new android Studio 3 and above
you need to put these lines in AndroidManifest.xml code if you want to set the orientation to portrait
One thing this is sensor portrait, means both upside - downside will work.
But if you only want to put portrait for upside then, replace sensorPortrait to portrait
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="sensorPortrait"
tools:ignore="LockedOrientationActivity">
Same goes for only landscape too
just one parameter change portrait to landscape
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation= "sensorLandscape"
tools:ignore="LockedOrientationActivity">

Categories

Resources