0

I am currently working on a app and it might be used on all android devices. I find it really challenging to adjust my XML layout files according to various screen sizes. I have surfed a lot over this topic and found a useful doc at Developer site. The document is decent and provides enough information on what should be done for screen compatibility.

Questions :

1.If I have two different layouts in folders like res/layout-sw600dp and res/layout-sw720dp, will the app automatically decides which one of these layouts is to be used ?

2.Assuming that I prefer a ListView for handsets and GridView for Tabs as a Home Page display, how will I define my layouts and how will I refer them for UI ?

Any ideas on how I can pull off the 2nd question's feature will be highly appreciated. Thanks in advance.

San
  • 2,078
  • 1
  • 24
  • 42
  • You can defferenciate it in java by checking "hasHoneycomb", tabs will give tru as return value. One way is : – Jithu Feb 17 '14 at 14:11
  • 1
    http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html. just have the layouts in the appropriate res/layout folders. – Raghunandan Feb 17 '14 at 14:12
  • That would differentiate my layout according to the boolean return value, right ? – San Feb 17 '14 at 14:12
  • yes, the honeycomb version is only for tablets, and you can use this to differentiate between tablets and phones. – Jithu Feb 18 '14 at 09:32

4 Answers4

1

You can defferenciate it in java by checking "hasHoneycomb", tabs will give tru as return value. One way is : you can set different layout from setcontentview() according to condition.

if(hasHoneycomb()) {
setcontentView(layout_for_tabs);
} else {
setcontentView(layout_for_phones);
}
Jithu
  • 1,478
  • 1
  • 13
  • 21
0

Yes, app automatically decide which layout file to use for current device screen.

Give the ListView and GridView different ids. In code use findViewById() method when creating views. If ListView found (findViewById(R.id.list) returned View) app is running on handset, otherwise (findViewById(R.id.list) returned null and findViewById(R.id.grid) returned View) app is running on tablet.

Ilia Kopylov
  • 758
  • 7
  • 18
  • So you're saying that Grid will automatically be set for Tablets and List for Handsets ? how would it differentiate which to choose on what devices ? I should add something explicitly right, what would that be ? – San Feb 17 '14 at 14:31
  • If you put layout with GridView to res/layout-sw720dp it will be shown on devices with screen width 720dp or greater. – Ilia Kopylov Feb 17 '14 at 14:46
0

1) Yes, it will, based on the screen size.

2) Give them different IDs and see which one is visible:

ListView mList = (ListView) findViewById( R.id.homeList );
GridView mGrid = (GridView) findViewById( R.id.homeGrid );

if( mList != null ) {
    // set list adapter
} else if( mGrid != null ) {
    // set grid adapter 
} else {
    // neither view exists...
}
323go
  • 14,143
  • 6
  • 33
  • 41
  • I dont understand this. The first if condition would always be true cause a list view would already exist according to your code. I'm confused. – San Feb 17 '14 at 14:26
  • 1
    No it won't always be true. Only if current layout contains view with id 'homeList'. – Ilia Kopylov Feb 17 '14 at 14:49
  • You will have two different layouts for your homescreen, one with a ListView (homeList) and another with a GridView (homeGrid). Android will choose the layout based on which layout folder you place it in, and what size screen it's running on. If it chooses the layout with the GridView, then mList will be null. – 323go Feb 17 '14 at 15:12
-1

For question 1, Yes Android will automatically choose and decide the most suitable amongst the two.

For 2, here is link - Determine if the device is a smartphone or tablet? Hope it helps. :)

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74