Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set application fragments to portrait mode only?

I have an Android app which has a main activity and 3 Fragments which are tabs. I would like the application to remain in portrait mode at all times but I can't seem to get this working. This is what I have tried, as per another stack overflow post, but I'm not sure what I'm doing wrong....does it need to be different if using fragments?

    <activity
        android:name="com.tutorial.test.activities.act1"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Thank you!!

Edit: The ViewPager is on the FragmentActivity for which I am setting the screenOrientation as above.

like image 247
user2573690 Avatar asked Dec 01 '22 19:12

user2573690


1 Answers

Try this..

You can try with programmatically.

After rootView in your java add this line getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

For Ex:

View rootView = inflater.inflate(R.layout.activityxml, container, false);       
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

And also in your manifest change it android:configChanges="orientation|keyboardHidden" as android:configChanges="keyboardHidden"

<activity
        android:name="com.tutorial.test.activities.act1"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden" >
like image 183
Hariharan Avatar answered Dec 04 '22 13:12

Hariharan