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,208
  • 6
  • 21
  • 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
  • 179,855
  • 19
  • 132
  • 245
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
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,621
  • 29
  • 151
  • 198
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
  • 5,203
  • 8
  • 27
  • 51
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 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,835
  • 15
  • 54
  • 71
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