3

I want to develop an application for Galaxy Tab and Android phones. The tab and phone UI are very different. Like in tab, we have used fragments, etc. and on the phone we want to use tabs.

I am aware that by using layout-small or layout-large I can show different UIs, but what about functionality?

How should I proceed? Is there a simple way to handle functionality also for different screens? Or is it better to create two applications finally?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Harshad
  • 7,904
  • 3
  • 24
  • 42
  • my opinion is doing an app for 3.0 down and other for 3.0 up. – PedroAGSantos Aug 12 '11 at 06:33
  • 1
    I did not understand what you mean.. – Harshad Aug 12 '11 at 07:43
  • He's implying that anything with a framework version of 3.0 or higher is a tablet. This is wrong, and assuming it's true will cause your app to break on future devices :) – Alexander Lucas Aug 19 '11 at 00:33
  • @ Alexander Lucas - But Galaxy tab is less than 3.0 but still a Tablet. That's what my concern was. – Harshad Aug 19 '11 at 02:38
  • What are you trying to do that you need different functionality based upon the size/density of the screen? – CrackerJack9 Aug 19 '11 at 14:48
  • when device is phone i want to show UI that has 4 tabs in it. But when device is Tablet the screen is big so we want to split it in two using fragments. but it requires some coding part in both this thing thats why. if i have different `main.xml` in `layout-small` and `layout-large' device itself will choose which one to use according to phone screen.Similarly if I have two `MyActivity.java` to perform task can I use it in simple way like above which will do it automatically? – Harshad Aug 19 '11 at 17:11

4 Answers4

6

First be sure to read this recent blog post that gives an overview of how applications see different screen sizes and can adjust to them:

http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html

This post has some examples of basic adjustments -- providing different layout files for different screen sizes.

This is also something that the fragment APIs are intended to help with. If you are already writing your UI with fragments, you are most of the way there -- showing these in tabs does not require a totally different implementation, just use the fragments to populate the different tabs.

If you are targeting Android 3.0 or later, this is easy because the action bar already has simple mechanisms to have its tabs allow switching between fragments. This API demo provides a very simple example of its use:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/ActionBarTabs.html

If you want to run on older versions of the platform, that is what the fragment APIs in the v4 support library are for. These allow you to write your app using fragments and still be able to run all the way down to Android 1.6 (API 4) of the platform. In MR2 we also added some sample code showing how you can use the support lib fragments with the old tab API.

Sample code for this is included in the now deprecated TabActivity class:

http://developer.android.com/reference/android/app/TabActivity.html

hackbod
  • 90,665
  • 16
  • 140
  • 154
3

There's no single answer to this - it's a broad topic.

One thing to keep in mind before you start, is that there's currently a lot of code floating around the web that assumes you can judge a screen size by the platform version: IE, everything up to Gingerbread is a phone, and Honeycomb (and up) is a tablet. This is a bad idea. Don't do this :) Also try to avoid a massive switch case that just executes different blocks of code based on width/height values you pull from the system. I'm not going to describe how to do that, others have already given that answer.

One thing to investigate is the Android Compatibility Package - It lets you use Fragments and Loaders (which were introduced in Honeycomb) in earlier versions of the Android platform. One common pattern this enables is to let you set up a nav fragment & content fragment which sit side-by-side on a Tablet (where there's room) but have the nav fragment switch over to the content fragment when an item is selected on a phone or smaller screen.

If you're just trying to have images that grow and shrink with screen size (that you're not drawing programmatically), the answer is 9-patches.

You also have the option, if it's appropriate for your application, of using Android Market's Multiple APK support. The linked page dives into how to set that up, and gives you a pretty solid description of when to use it and when you're better off with a single APK.

Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
2

Try splitting your app in seperate parts, one trunk containing core features that you develop as a Library, then two separate apps using the same core functionality with specific UI design, one for Tablet version of Android (>3.0 HoneyComb) and one for "phone" versions (<3.0).

My experience with GalaxyTab-likes (basically a tablet with a 'phone' non-HoneyComb version of the OS) is that phone layouts do stretch nicely to fit the 7 inch screen. So it might be enough to add a specialized layout.xml specifically made for big screen. However I didn't even found it necessary on my own app...

EDIT: also not that if a tablet is running a non-honeycomb OS, 2.2 or 2.3, like is the case with GalaxyTab and many 7 inch tablets, the functionality is exactly the same as on phones, so no specific redesign is needed if not for the layout of UI components.

EDIT: read this page for specific and detailed information on how to support different screen size in your app. You can specify in your manifest the size of the supported screen for your app with

<manifest ... >
    <supports-screens android:requiresSmallestWidthDp="600" />
    ...
</manifest>

So given the market now supports uploading several apk for the same app, you could compile different version of the app seperately for each screen size, and the market would filter them out according to the sizes in Manifest, so you should even never have to actually check for screen size in your code.

Smugrik
  • 850
  • 7
  • 22
  • 1
    Only thing to add, I think, would be: You're limiting the app by the difference in UIs. Your activities (and the libraries) are controlled and initiated, for the most part, by direct user input - so not showing a button in the different layout states will control your app's functionality for you. – jlindenbaum Aug 16 '11 at 19:14
2

You can retrieve the screen size using:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

You will have to have decided which screen sizes are required for the different features, such as setting the minimum tablet height and width to min_tab_height and min_tab_width, accordingly. Then do a check for if this screen size qualifies:

boolean is_tablet = false;
if (width >= min_tab_width && height >= min_tab_height) {
    is_tablet = true;
}

Now you can check if (is_tablet) before allowing tablet functionality to your app (or if (!is_tablet) for non-tablet functionality.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • display.getWidth() dates back to 1.0 and is deprecated, in HoneyComb this is replaced by getSize(point) but again this is not fit for the purpose here, see [Android reference](http://developer.android.com/reference/android/view/Display.html#getSize(android.graphics.Point)) for details. Anyway with this you would only get the size of the display in pixels, which says nothing about its physical size. – Smugrik Aug 16 '11 at 19:24
  • Could whoever downvoted explain why? This is a valid answer. It has code and is well explained. – Phil Aug 16 '11 at 19:34
  • OK, you're right on that one it is well edited, so I reverted. I voted down because I believe this is really not the way to go as you would end up with your code all spaghettied with checks for all the different screen-sizes you want to support, and there are many of them on Android – Smugrik Aug 16 '11 at 19:39
  • @Smugrik, you have a good answer, but it's complicated. This response is a simple way to do the same thing. There are several approaches to the problem - hence the reason this site allows multiple answers. – Phil Aug 16 '11 at 19:45
  • but this doesn't allow to make the distinction between tablets like the archos 7 inch with a resolution of 480x800 and high-res phone like the HTC sensation or Moto Atrix with 540x960 – Smugrik Aug 16 '11 at 20:00
  • This is example *is* broken, and very broken, because it is not adjusting for screen density. Nobody should follow this as-is, because using this code will result in your app mis-behaving on all kinds of devices with higher densities. Fortunately the solution is simple: divide by the density scale so you are doing your measurement in dp units. – hackbod Aug 21 '11 at 23:52