Landscape/portrait layout

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Landscape/portrait layout

Postby Johan » Fri Feb 27, 2009 10:04 am

Hello,

I'm new to android and I am working on an application of which the layout changes when the orientation of the screen changes.
In landscape mode I want to show data using layout x and in portrait mode using layout y (with different widgets in it).

It seems there are 2 ways to achieve this.
1. I understand that an activity is destroyed and recreated when the orientation changes. Than I can detect the orientation in the onCreate method and set the contentview to the appropriate layoutview and it works.
2. Second possibility is using the onConfigChanges method which is called when the configuration changes. In this case I would create the 2 views in the onCreate method when the activity starts for the first time. And when onConfigChanges is called I can set the contentview to the appropriate view with setContentView().

I'm not really sure which of these 2 is the best way to make it work.

Any suggestions?

Thank you.
Johan
Freshman
Freshman
 
Posts: 7
Joined: Fri Feb 20, 2009 3:30 pm

Top

Postby jjaaxx44 » Fri Feb 27, 2009 10:27 am

i think 2nd option is good....its jst a thot.....if ur activty starts every time config changes der'll be lot of work u have to do in onsaveinstance method......if u go by 2nd option in onConfigChanges der wont be much of work......again it depends on ur logic
jjaaxx44
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Jan 31, 2009 4:44 am

Postby MrSnowflake » Mon Mar 02, 2009 9:28 am

You don't really get the concept of layouts and resource. (Re)Check the docs on this topic!

Android allows you to define multiple layouts for different types of devices: keyboard, no keyboard, no touchscreen, ... And portrait and landscape! Android chooses the best layout automatically, which means, you only have to call setContentView(myLayout) and Android will determinate, if you need the portrait or landscape layout automagically.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Emmanuel7 » Mon Mar 02, 2009 2:03 pm

The first solution is the easier one : you just have to handle the pause / resume event of your application, and everything will work. The application will nicely close, and restart with the new Layout.
As you HAVE to do this for other interruptions ( like a phone call while the user is in your activity), it means it is no more work for you.
The second solution is trickier, as you have to handle everything by yourselve, and be cautious about memory / resources leaks. But it allows you to have faster and/or nicer transition !

Google engineers highly recommend the first solution !

There is a very interesting blog serie on this subject here :
http://androidguys.com/?s=rotational+forces&x=9&y=9

Note on the part 3 ( the more interesting for you ? ), there is a comment from a google engineer on this very subject !
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby MrSnowflake » Mon Mar 02, 2009 2:46 pm

The 2nd one is pretty useless...

[edit]woops, I said 1st instead of 2nd...
Last edited by MrSnowflake on Mon Mar 02, 2009 7:00 pm, edited 1 time in total.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Emmanuel7 » Mon Mar 02, 2009 6:03 pm

Hum...
I think we don't speak about the same point in the first method.
I'm speaking about the way you let android framework destroy and recreate the activity on an orientation change.
As MrSnowFlake said, it is not necessary that you check by yourself the orientation, and choose the layout. The framework do it for you, just have a layout with the correct name ( I'm at work now, but I think it is something like layout-landscape and layout-portrait, no ? ). So just use the framework mechanism...

But there IS an important difference between the two solutions :
* the first one, you let the framework destroy and recreate the activity on an orientation change,
* the second one, you block the framework behavior on orientation change, and do whatever you want by yourself...
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Top

Postby Johan » Tue Mar 03, 2009 9:59 am

Thank, I'm going to use the first solution.
I'll put the portrait version of 'main.xml' in the layout-port directory and the landscape version in layout-land. Now the framework chooses the appropriate layout.
One more question: The 2 main.xml layout file don't use the same widgets: If layout-port/main.xml has for example a Button and the layout-land/main.xml has a TextView. I can easily create an object of them using findViewById(...).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. setContentView(R.layout.main); // will use the appropriate layout
  3.  
  4.  
  5.  
  6. TextView tv = (TextView) findViewById(R.id.textview);
  7.  
  8. // do something with tv
  9.  
  10.                
  11.  
  12. Button button = (Button) findViewById(R.id.button);
  13.  
  14. // do something with button
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



But using tv when I'm in portrait mode or using button when I'm in landscape mode will cause an error because the can't be found (because it is not in the current layout). So than I still need to know the orientation of the phone to know which widgets I need to create and use? Or am I still missing the point?
Johan
Freshman
Freshman
 
Posts: 7
Joined: Fri Feb 20, 2009 3:30 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests