How to lock a layout on ORIENTATION_LANDSCAPE and another on ORIENTATION_PORTRAIT

Asked

Viewed 109 times

3

I’m using the method for when change of layout change position but when start application in landscape it starts layout teste_01 landscape mode being that its format is portrait how to fix it.

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    setContentView(R.layout.activity_teste_02);
    inicializarInterface();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){    
    setContentView(R.layout.activity_teste_01);
    inicializarInterface();
}

1 answer

3


You can use the folder res to create specific layouts for Portrait or Landscape.

The briefcase layout inside the briefcase res, serves for generic layouts, without any "treatment", for any type of screen (resolution or orientation).

If you want to create a specific layout for Portrait, just create a folder layout-port inside your briefcase res. And to create a specific layout for Landscape, create a folder layout-land inside your briefcase res.

Note: don’t forget to keep the same name for both layouts in both folders.

In his Activity you don’t have to worry about which layout will be inflated, calling only setContentView(R.layout.seu_layout) and let Android take care of the rest

For more details, follow the official documentation: http://developer.android.com/training/basics/supporting-devices/screens.html

  • @Rodolfo is just an example! You need to implement the layout ( .xml file) you created.

  • @Rodolfo what file name . xml you created in each folder?

  • @Rodolfo well. After having created his activity_teste.xml in the briefcase layout-land and/or in the folder layout-port, in his Activity place setContentView(R.layout.activity_teste);

  • thanks already found my mistake

  • @Rodolfo great! I advise to give a read/ studied on how the folder works res Android so you can use its full potential

  • I have a question if you think it best I open another question how to do not stay Initiating when changing from landscape layout to portrait and vice versa type, stay in fashion waiting I do not know if I got to understand.

  • @Rodolfo could not understand. Open another question and better detail the question with what you need please

  • I have already published a new question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.