Different design for landscape and portrait orientation android
Asked Answered
A

4

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.

Abacus answered 8/10, 2012 at 12:8 Comment(0)
B
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

Briefless answered 8/10, 2012 at 12:10 Comment(0)
T
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 .

Transect answered 8/10, 2012 at 12:10 Comment(1)
Sorry for late reply! Thanks a lot ! :)Abacus
L
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

Lorrettalorri answered 8/10, 2012 at 12:11 Comment(1)
Sorry for late reply! Thanks a lot for the answer :)Abacus
H
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(); 
        }
    }
Hesychast answered 9/10, 2014 at 8:26 Comment(3)
try to add some explanation rather than just adding code snippet as an answerSmack
Shouldn't this code be in onStart()? I don't think onCreate() is called when turning the device.Osullivan
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 againDibromide

© 2022 - 2024 — McMap. All rights reserved.