1

I have a Image that is at the top center of the screen and a button which alignParentRight. And set to below Image for now. What I want is that when it is on landscape view, the image is not that long, I want to move the Button to the top. Is there a way to achieve this? Thanks

Portrait View

 _________________
|[  <--Image-->  ]|
|         [Button]|

Landscape View

 _________________________________
|     [  <--Image-->  ]           |
|                         [Button]|
|                                 |
Manto
  • 1,069
  • 2
  • 15
  • 34

2 Answers2

1

If you don't want to switch to using a different layout depending on screen orientation you could do the following.

Listen out for the screen orientation. I'm not sure how your app handles it at the moment. Does it restart the app (default behaviour) or do you prevent it by overriding onConfigChanged?

Either case, you can get the screen orientation using this:

WindowManager windowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.getDefaultDisplay().getOrientation();

Now you know your screen orientation, you can set the layout of the button programmatically. Instead of alignParentRight, you could use alignRightOf relative to the ImageView, assuming you're using RelativeLayout.

To set the layout programmatically (see here for source):

RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); //causes layout update
Community
  • 1
  • 1
BDFun
  • 531
  • 2
  • 7
  • "Does it restart the app (default behaviour) or do you prevent it by overriding onConfigChanged": it will not restart the activity. – Manto Jun 11 '12 at 21:29
  • I need something like onOrientationChange(){Move Buttons}. The buttons are already there but when the user changes the orientation, the buttons should relocate without restarting the activity. – Manto Jun 11 '12 at 21:31
0

You should create separate layout for landscape and portrait mode.see this.

Eight
  • 4,194
  • 5
  • 30
  • 51
  • I know that could be an option and I tried that way. But switching between this layout and the layout from layout-land folder take too much time to load... Is there a way that I can do something like gravity=top. – Manto Jun 09 '12 at 00:38
  • then, you should use the solution suggested by @BDFun. – Eight Jun 09 '12 at 02:10