0

I am writing an application which in portrait view has a gallery at the top and when you click a picture it will inflate and fill the entire screen.

This works however in landscape mode the gallery covers up most of the picture and it in general looks crappy.

I made a GridView for landscape mode. The problem I am having now is to change it from the gallery activity to the Gridview activity when the orientation changes. Any ideas?

Harmlezz
  • 7,972
  • 27
  • 35
Matthew Lueder
  • 953
  • 2
  • 12
  • 25

3 Answers3

6

The right way to do it is to define two layouts, one in layout-port and one in layou-land directories. That is, you will have the following two layouts: res/layout-port/main.xml and res/layout-land/main.xml.

In your software you simply write secContentView(R.layout.main); and android will take care of applying the right layout upon device rotation.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
2

Make an extra layout xml file for landscape mode and put it into the folder "layout-land"

Renard
  • 6,909
  • 2
  • 27
  • 23
1

Add this to your Activity:

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

    startActivity(newActivity)
}

Note that this is a bad idea. Actually, it's a really bad idea. A much better alternative would be to either force one orientation or to create a layout that looks great on both orientations.

Rob S.
  • 3,599
  • 6
  • 30
  • 39
  • Thanks this works. To other people who may be reading this in order to do this you need to add android:configChanges="orientation" to your manifest. – Matthew Lueder Apr 15 '12 at 01:31
  • And why is this a bad idea btw? – Matthew Lueder Apr 15 '12 at 01:32
  • @Matthew Lueder onConfigurationChanged() is really intended to be used to preserve data from one orientation change to the next. In theory, under the implementation I showed, a user could change the orientation a lot and build up a large backlog of activities which would create (at least I assume) unintended back button behavior for the user. So make sure you also use finish() appropriately. So in short, use it very carefully and only when you need absolutely need to for launching Activities. – Rob S. Apr 15 '12 at 02:12
  • @MatthewLueder You should never do this ? Other wise you will endup having memory leaks in your activity which are hard to debug.... Create different layouts for both the orientations.. – Amit Jul 17 '13 at 06:00
  • There are use cases for this... I'm working on a custom app to run on a Clover Station (android tablet for payments). It flips from landscape to portrait on the mount. The landscape side is for the checkout employee to enter the order and when they "flip" it around to portrait, the page presented would be order summary and the payment stuff. These are two different activities and while I could put all of the programming and XML in one activity, it makes more sense to break it out into two. Good advice about watching for memory leaks, though. – XtopherSD Nov 25 '17 at 00:02