Euphor08 February 2016

Change fragment layout portrait to landscape

I work with fragment but my problem is that : When I change orientation, if I had 2 columns in my scrollView they still in the screen even it is portrait, or If I had one column in portrait mode when I pass to landscape I had only one column.

I had 3 layout with the same name only number of columns which change, I put it all in folder layout, layout-land and layout-port.

What I need is that when I passe from portrait to landscape I would passe from 2 columns to one, and vice versa.

enter image description here

enter image description here

enter image description here

Answers


Dalvinder Singh February 2016

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    //change your desigen by java code
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
     //change your desigen by java code
}


topcbl February 2016

You can't just put it all in folder layout, layout-land and layout-port to make it work. There are 3 more steps you need to do: 1. Create a layout.xml file in values folder 2. Put xml layout with 2 column in layout-land and layout with 1 column in layout-port 3. In onCreateView() in your fragment, inflate the layout's name in layout.xml file instead current layout.

For more details, I'll show you some code: You're gonna write a MainFragment and you create a fragment_main.xml for its layout. What you need to do is:

  1. Write fragment_main.xml in layout folder (1 column layout)

  2. Write fragment_main_land.xml in layout folder (2 column layout)

  3. Create layouts.xml file in values-land folder with the code lines <resources> <item name="layout_main" type="layout">@layout/fragment_main</item> </resources>

  4. Create layouts.xml file in values-land folder with the code lines

    <resources> <item name="layout_main" type="layout">@layout/fragment_main_land</item> </resources>

  5. on onCreateView method of MainFragment, inflater.inflate(R.layout.layout_main, null);

Post Status

Asked in February 2016
Viewed 1,803 times
Voted 8
Answered 2 times

Search




Leave an answer