Question

I want to develop an android application for perfectly screen design arranged for portrait and landscape modes. and should have to give the screen orientation facility to user. how to change two xml files of the application when change the screen orientation of the device?

Was it helpful?

Solution 2

create another layout folder called layout-Land under the res folder. copy all landscape xml files to that layout -Land folder with the same name. Recheck all portrait xml files are in the layout folder. add android:configChanges="orientation" to the manifest

 <activity
        android:name=".LoginActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateVisible|adjustPan" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

cheerz

OTHER TIPS

do not add android:configChanges="orientation" it'll stop recreation of activity which ultimately not allows you to have vertical and horizontal layout applicable.

Just make another layout and put it in your layout-land folder. Your structure should look like this:

res/
    layout/ 
        my_layout.xml #will be used in portrait
    layout-land/ 
        my_layout.xml #will be used in landscape

Do not add the android:configChanges attribute as suggested -- as @nitin said, that will prevent your Activity from being recreated and you'll have to do some extra work to handle resource switching. When your Activity is created, and you create the layout (e.g. setContentView(R.layout.my_layout.xml), it will resolve the right layout resource based on the orientation.

Add this code in manifest file in activity tag:

For landscape mode:

<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

For portrait mode:

<activity android:name="MyActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top