10

I want to develop an universal app . I am novice to this approach .That means a single apk for both tablet and phone application . I had go through

Support Library

Fragments

My aim is to build different UI for Tablet and Phone inside a single APk .

I had read Getting Your App Ready for Jelly Bean and Nexus 7 . This article mention that

res/layout/activity_home.xml

To take advantage of the extra space on the 7” screen you might provide an alternative layout:

res/layout-sw600dp/activity_home.xml

The sw600dp qualifier declares that these resources are for devices that have a screen with at least 600dp available on its smallest side. 

    Furthermore you might even provide a different layout for 10” tablets:

res/layout-sw720dp/activity_home.xml

That means we can use different layout for different device . This confuses me

EDIT 1 :: Scenario

Suppose

if my phone UI layout contains one-view pager and 

tablet UI layout contain two-view pager  . 

How can we achieve this ?? In this article it says that you an designe different layout with same name for different screen and keep it corresponding folder . But my doubt will this arise exceptions if it try to initialize widget component of Tablet layout when app is running in a phone .

EDIT 2 : The idea came into my mind is determine which type of device is i am using ie Tab or phone .

Determine if the device is a smartphone or tablet?

Then avoid the initialization of widgets if app is phone . Is there any better way than this ??

EDIT 3 : My application support from 2.3 to higher versions

If my layouts for tablet holds additional widgets compared to phone layout .How an i initialize and use . Hope all understood my need . So please clarify my doubt

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
  • **That means we can use different layout for different device . This confuses me.** What confuses you? I think you've done loads of research already. – Fahad Ishaque May 21 '13 at 10:56
  • I preferably use layout/large (for 5.1-7 inch device), layout/xlarge( for 10 inch tabs) and layout normal for mobiles. Mobile layouts can be further defined by using layout/large-xhdpi or ldpi/mdpi/hdpi – Fahad Ishaque May 21 '13 at 10:57

2 Answers2

6

When I face the problem, I create following structure.

res/layout
res/layout-sw600dp

then to distinguish values and other resources,

res/values
res/values-sw600dp
res/values-sw720dp

You noticed that there is only one layout directory and two values directory to specify margins and paddings and other resources. So single layout can be used for 7" as well 10" tablet. This is my scenario, you can also define layout-sw720dp. I did that due to reduce compilation time of layouts.

I have also different layouts in phone and tablet. For example, I have a ListView in first screen, then when user click on item, it will open other activity and there is DetailView for that. But in tablet, I have left side ListView and right side DetailView.

So to do so, in values/strings, I place following code,

<bool name="isTablet">false</bool>

and same for tablet values-sw600dp/strings

<bool name="isTablet">true</bool>

Now, come to part of coding. I have a splash screen and which has common layout. So it will display common screen. But when user click on any button, it will check whether it is tablet or not. To check it,

boolean isTablet = getResources().getBoolean(R.bool.isTablet);

You have now flag indicate whether your application is running on phone or tablet.

I have created two packages,

com.phone
com.tablet

then as per flag, I direct my activity to phone package and tablet package.

Example,

if(isTablet)
    startActivity(this,TabXYZ.class);
else
    startActivity(this,PhXYZ.class);

And this approach has solved my problem.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • So you are telling me to use two layout handler class for one functionality – edwin May 21 '13 at 12:24
  • But that functionality will be different if screens are different my friend. If functionality is same, then go for fragment, it will solve your problem, but though i have problem with screen design different for both phone and tablet, i use this approach. – Chintan Rathod May 21 '13 at 12:58
  • 1
    I considered your answer.. But still looking is there any other way to handle this – edwin May 22 '13 at 04:26
  • Refering some article shows sw600dp qualifier is only valid for platforms above android 3.2 .My application supports from 2.3 . what was your case?? – edwin May 22 '13 at 06:52
  • I have created my application in 2.3.3 (Ginger Bread). No issue right now. And is also on play store. – Chintan Rathod May 22 '13 at 06:57
  • If you want to check, download and try https://play.google.com/store/apps/details?id=com.heliumcreative.bluemartini&feature=nav_result#?t=W251bGwsMSwyLDNd – Chintan Rathod May 22 '13 at 07:09
  • @edwin, if your problem solved with answer, please mark accepted so other can get benefit from it. – Chintan Rathod May 25 '13 at 03:48
1

findViewById will return the widget in case it's present in the layout, null in case there's no such widget. So if it returns non-null you can proceed with further initialization.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • I've seen they sometimes do like this: if you know that on a certain layout you have more widgets than on another layout then you can just check for the presence of a single widget from that 'extra' group. If findViewById gives non-null it means you will also have all the rest of your extra widgets. – Alexander Kulyakhtin May 21 '13 at 11:35