2

I would like to rotate Buttons/TextViews/etc. on screen orientation change but I want to keep layout unchanged. How can it be done? I'm using LinearLayouts.

This is what I mean: http://i46.tinypic.com/209hwnr.png

Ziem
  • 6,579
  • 8
  • 53
  • 86
  • 1
    can create the differnt layout in layout-land and layout-port – Dheeresh Singh Jun 06 '12 at 11:27
  • 1
    `I would like to rotate Buttons/TextViews/etc. on screen orientation change but I want to keep layout unchanged.` -- do you not see that you're stating `I want to use a new layout but I don't want to use a new layout.`? – mah Jun 06 '12 at 11:30
  • **mah** is it possible without creating new leyout? – Ziem Jun 06 '12 at 11:38

3 Answers3

5

Create res->layout->layout-land and put your xml file for landscape mode

Your layout file in layout folder is only for portrait mode Now if you need landscape then create layout-land folder.

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1

It can be done it two ways:

1.) Either you define a new xml file in layout-land folder.

2.) Use android:configChanges="orientation" in your activity tag inside manifest.xml

Then in your activity class:

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

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        ChangeToLandscape();
    } else {
        ChangeToPortrait();
    }
}

LayoutParams lp;
public void ChangeToLandscape() {
    lp = new LayoutParams(new ViewGroup.MarginLayoutParams(
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    textView1.setLayoutParams(lp);
    lp.setMargins(0, 0, 0, 0); // Whatever you want
    // Similar for other views
}

//Similarly, implement `ChangeToPortrait()`

Hope it helps !!!

Ziem
  • 6,579
  • 8
  • 53
  • 86
GAMA
  • 5,958
  • 14
  • 79
  • 126
0

You'll have to change the layout parameters.

mihail
  • 2,173
  • 19
  • 31
  • Which parameters? I tried to change layouts orientation to opposite (on screen orientation change) but it didn't work. – Ziem Jun 06 '12 at 11:33
  • The layout parameters of the layout. It is possible but too much work to do in java. But anyway, check the Samir Mangroliya's answear. It's much better approach. – mihail Jun 06 '12 at 11:47