Ferdau
Ferdau

Reputation: 1558

Force phone layouts on tablet

Is it in any way possible to make a tablet use the regular layout when there is a large one available?

So for example:

if(!user.payedForTabletApp) DO_NOT_USE_LARGE_LAYOUT();

Upvotes: 2

Views: 1065

Answers (2)

inazaruk
inazaruk

Reputation: 74790

Here is how you can approach the problem:

1) Create two layouts: res/layout/phone.xml and res/layout/tablet.xml
2) Create layout aliases:
2.1) res/layout/main.xml -> res/layout/phone.xml
2.2) res/layout-large/main.xml -> res/layout/tablet.xml
2.3) res/layout-sw600dp/main.xml -> res/layout/tablet.xml

By alias I mean something like this:

<resources>
    <item name="main" type="layout">@layout/phone</item>
</resources>

For more examples of layout aliases see this tutorial.

3) Update your onCreate() function in activity or onCreateView() function in fragment to use appropriate layout.

Here is an example for activity:

int layoutResId = R.layout.main;
if (!user.payedForTabletApp) {
    layoutResId = R.layout.phone;
}
setContentView(layoutResId);

Upvotes: 2

Chris.Jenkins
Chris.Jenkins

Reputation: 13129

The short answer to this is no. Android handles the resource system for you.

You could how ever provide two different layout files, in you layout folder.

Something like activity_home_normal.xml and activity_home_large.xml obviously I DO NOT RECOMMEND THIS!

Then you can do:

if(!user.payedForTabletApp){
    setContentView(R.layout.activity_home_normal);
}else{
    setContentView(R.layout.activity_home_large);
}

BUT I would look at charging your uses for other features not layouts. If I buy an app on iOS I expect the same app to work on both device sizes - same for android.

Layouts optimise user experience.

Upvotes: 1

Related Questions