Various controllers for phone and tablet

Is there a way to have two different Java codes for the same Android application? One targeting phone devices and other targeting tablet devices.

For example, something like HomeActivity.java and HomeActivity-large.java

Basically, I would like to make an Android app that has a different layout, following its resolution.

I know that I can use layout-large to have a specific layout for large devices. I'm not sure how to have specific logic for tablets and another for phone purposes.

The reason I want to do this is because I would like to make an API call on a screen that I don't want to do on the phone.

As an alternative way, I thought of using a boolean (I'm not sure which one) to check which device the application is running on. But I am afraid that this may become promiscuous.

Another thing could be to create a library with all the features of my application, and then with two different applications using the library. The problem with this solution is that some code will be replicated.

Any suggestion?

+4
source share
2 answers

Option 1 : To have one project that compiles into one or more applications, you need to use conditional compilation. Basically, something like this

//#ifdef Phone // do something here //#else Tablet // do something else //#endif 

See Java (Eclipse) - Conditional Compilation for how to use conditional compilation for Android in Eclipse.

Option 2 : You can check the screen size at runtime to determine which interface to display. Cm:

http://developer.android.com/guide/practices/screens_support.html

 xlarge screens are at least 960dp x 720dp large screens are at least 640dp x 480dp normal screens are at least 470dp x 320dp small screens are at least 426dp x 320dp 
+3
source

Different solutions, only the first two that I can imagine:

1) Place a tablet on the tablet with a specific identifier and with the visible attribute set to GONE. After inflating the layout, check for the presence of the view using findViewById (...). If the result is zero, then you are in the layout of the smartphone, otherwise you are on the tablet. This approach is completely dependent on the automatic resolution recognition of system devices.
2) Use this code snippet to get the width and height of the screen in pixels and decide for yourself which code you are executing.

 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); Log.d("log", "Screen is " + metrics.widthPixels + "x" + metrics.heightPixels); 
+1
source

All Articles