14

Can we have a different xml for landscape and different xml for portrait orientation?

I am working on a simple app, have few buttons and textviews, the xml looks good in portrait, But with same xml when I check the landscape orientation, design doesn't look good.

Any suggestions are appreciated.. Thank you.

Adarsh H S
  • 1,188
  • 6
  • 20
  • 42

4 Answers4

37

Yes ofcourse.

You will have to create two versions of xml files and put in layout-port and layout-land folder inside res folder.

eg :

res/layout [Portrait Mode; default]
 main.xml
res/layout-land [Landscape Mode]
 main.xml 

You can refer further more on the same at http://developer.android.com/training/basics/supporting-devices/screens.html

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
Kartik Domadiya
  • 29,430
  • 19
  • 92
  • 103
5

If you want to make another layout for landscape then put it in

res -> layout-land folder .

Both the names of the xml are must be same for which is used for portrait and landscape .

Chirag
  • 56,384
  • 29
  • 154
  • 197
1

you should make a different xml file for landscape orientation. below link can help you

http://developer.android.com/training/basics/supporting-devices/screens.html

Stefan
  • 4,997
  • 8
  • 26
  • 51
Hardik Nadiyapara
  • 2,416
  • 2
  • 15
  • 24
1
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_orientation_app);
   if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().
            heightPixels) 
        {  
            Toast.makeText(this,"Screen switched to Landscape mode",Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 
            Toast.makeText(this,"Screen switched to Portrait mode",Toast.LENGTH_SHORT).show(); 
        }
    }
laaposto
  • 11,377
  • 15
  • 52
  • 68
udaysagar
  • 11
  • 2
  • 3
    try to add some explanation rather than just adding code snippet as an answer – user2720864 Oct 09 '14 at 08:35
  • Shouldn't this code be in onStart()? I don't think onCreate() is called when turning the device. – Al Lelopath Oct 26 '16 at 15:17
  • Al Lelopath, when a device is rotated, the activity is completely removed. OnDestroy is called, then the Activity is restarted. So yes, onCreate is called again – jb15613 Jan 11 '17 at 03:47