2

I have an activity which contain 3 fragments. 2 of them have data in a listview therefore when user rotate the screen everything is ok. but on my third fragment which contain buttons, when the user rotate the screen to landscape orientation,everything goes astray!

my third fragment in protrait orientation

portait orientation

my third fragment in landscape!

enter image description here

I have even tried to use scrollview but it's not working because when in landscape i don't want that big space at the middle. i wan't 3 x 2 buttons arrangment(i have 6 buttons.) when in landscape. and i think i can only achieve that by using 2 xml layouts for my fragment.

Question is how can i do that?

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • @SaiPhani How will i then call it in my fragment because it won't be used by the entire activity but only i fragment? – Edijae Crusar Nov 23 '15 at 07:33
  • 1
    you dont need to call specifically. define your layout with the same name of you 3rd fragment and append `-land` at the end. Android will call it automatically. If your fragment name is `third_fragment.xml`. create a new xml with a name `third_fragment-land.xml`. Check this link http://stackoverflow.com/questions/4858026/android-alternate-layout-xml-for-landscape-mode/4858052#4858052 – Sai Phani Nov 23 '15 at 07:34
  • @SaiPhani let me try right now and i will tell you the results. don't go away(i beg you :) ) – Edijae Crusar Nov 23 '15 at 07:37
  • @SaiPhani great guidance, great help. i finally achieved what i want. thanks a lot man. (see my outcome (answer) below) – Edijae Crusar Nov 23 '15 at 08:48
  • Happy that it helped you – Sai Phani Nov 23 '15 at 08:52

3 Answers3

1

Make another layout folder layout-land under res and create landscape orientation layout file under this folder

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • how will i then call it(or use it) in my fragment ? – Edijae Crusar Nov 23 '15 at 07:30
  • Don't do anything, android do the rest. – Amirhossein Naghshzan Nov 23 '15 at 07:31
  • 1
    @gikarasojokinene you dont need to call specifically. define your layout with the same name of you 3rd fragment and append `-land` at the end. Android will call it automatically. If your fragment name is `third_fragment.xml`. create a new xml with a name `third_fragment-land.xml`. Check this link http://stackoverflow.com/questions/4858026/android-alternate-layout-xml-for-landscape-mode/4858052#4858052 – Sai Phani Nov 23 '15 at 07:32
1

After great help from sai Phani(see his comments above). i finally achieved my objective which was to create a landscape layout for my fragment by doing the following.

  • Create a folder under src\main\res and call it layout-land
  • copy your fragment layout xml (e.g peoplefragment.xml) which is in src\main\res\layout and paste it into src\main\res\layout-land folder. Don't change the file name!
  • Once you have pasted it, you can change the views alignment the way you would like them t appear on landscape. for my sutuation, i wanted them to appear like this in landscape

landscape orientation

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
0

You may design your layouts that looks good for both portrait and landscape but you may customize for both orientation. Look at SO link on the same issue @ Android: alternate layout xml for landscape mode. The idea is simply to create folder names the correct way specified in Google documentation.

A good Google webpage is @ Supporting Multiple Screens, search for "Using configuration qualifiers". There is a table stating Orientation and the folder names to use.

Community
  • 1
  • 1
The Original Android
  • 6,147
  • 3
  • 26
  • 31