0

i know its a noob question but i am new in android development and i dont want my app rotate in landspace. according to some of questions i found sensor portrait. i tried to add it in manifest but i dont know where. if its not working is there any source code to disable rotate? i am really need help my layout looks owful in landspace. i dont know how to fix the background what can i do to fix layout in landspace? what should i do? i didnt find the answer in question. thanks

  1. How to use sensorportrait?
  2. source code for disable rotate?
  3. fix layout in landspace?

i really dont want the answer for thirdone but its a good point for a app to work in portrait and landspace.

4 Answers4

3

You just need to add this code in your manifest file

<activity
        android:name=".LoginActivity"
        android:screenOrientation="landscape" />

screenOrientation = "landscape" is force screen to landscape

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Hitesh Kushwah
  • 1,351
  • 13
  • 23
1

To dynamically change the orientation of the activity, call the setRequestedOrientation(int) method. More info on the method can be found here.

The int argument sent in method can be changed and fixed. The relevant values for the same can be found here.

Vaibhav Singhal
  • 888
  • 9
  • 13
0

For preventing the rotation of your activity, you can also set the orientation in your onCreate. It checks your orientation and if it's LANDSCAPE it will change it.

Example:

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.your_layout);
  if(getResources().getConfiguration().orientation ==
        Configuration.ORIENTATION_LANDSCAPE){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }
}
Richard
  • 56,349
  • 34
  • 180
  • 251
Phil
  • 304
  • 1
  • 5
  • 12
0

Take a look at this document, it explains each property in your AndroidManifest.xml. https://developer.android.com/guide/topics/manifest/activity-element.html

  1. How to use sensorportrait?

    These Properties are defined in your AndroidManifest.xml file.

    android:screenOrientation=["unspecified" | "behind" | "landscape" | "portrait" | "reverseLandscape" | "reversePortrait" | "sensorLandscape" | "sensorPortrait" | "userLandscape" | "userPortrait" | "sensor" | "fullSensor" | "nosensor" | "user" | "fullUser" | "locked"]

  2. source code for disable rotate?

    In your AndroidManifest.xml file, where you define "activity", u set orientation you support as defined above.

  3. fix layout in landspace?

    You can have different layout for your activity in landscape mode. for this simply create layout-land folder parallel to layout folder in res. e.g. if you have activity_main.xml in layout folder, create another activity_main.xml in layout-land folder. You can move stuff around as you wish. Note: I would reccomend you take a look at Fragments, this might help you on what you are trying to achieve. https://developer.android.com/guide/components/fragments.html

bond
  • 11,236
  • 7
  • 48
  • 62