5

I have to write an app for tablet and for phones too. The two app has the same functionality, but they have absolute different GUI.

For example the phone have 2 button on the main screen, but the tablet going to have 5, because we would like to use the space what we have. I know, I˜m able to define different layouts, depends on dpi, but how should I handle the layout-s in the Activiies? I think, to use if(sdkVersion >=11) bla..bla... is not will works through the hole code and the hole project! Is this the situation where I have to use multiple application support ?

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

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

http://developer.android.com/guide/market/publishing/multiple-apks.html

I don`t understand how should I handle this problem... please if you able help , thx

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
narancs
  • 5,234
  • 4
  • 41
  • 60

1 Answers1

6

Put your tablet layouts in res/layout-large/. Put your phone layouts in res/layout/. Name them the same. Your activities will load the right ones based upon the device they run on. When you call findViewById() to retrieve the extra buttons, and see that you get null back, ignore them.

You may need additional layouts in places like res/layout-large-land/ (landscape for tablets), res/layout-xlarge/ (if you want to handle 10+" tablets differently than stuff in the 5-9" range), res/layout-small/ (if you want to handle <3" screens), etc.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hello CommonsWare! Thanks for your comment, but "When you call findViewById() to retrieve the extra buttons, and see that you get null back, ignore them." is what I would like to dismiss. because we are going to start a large project with a lot of activities you know, and always doing an if(findViewById(R.id.btn7)==null) is makes the code unreadable so easily .... I looking for an elegant solution :) – narancs Sep 17 '11 at 18:04
  • 1
    @Karoly: "and always doing an if(findViewById(R.id.btn7)==null) is makes the code unreadable so easily" -- what "always"? Use one of those fancy Java things called "data members" to hold the widget or a `boolean`. Or reorganize your UI to take advantage of fragments, so on phones you are using one fragment per activity, yet your main activity loads a layout that brings up two fragments. – CommonsWare Sep 17 '11 at 23:28
  • but as I see the fregments comes with API level 11. For tablets, okay, but how can I use fregments if we want to run the app on phones (2.1,2.2.2.3) and talbet too – narancs Sep 18 '11 at 10:52
  • 1
    @Karoly: Fragments are part of the Android Compatibility Library and therefore are available back to Android 1.6. – CommonsWare Sep 18 '11 at 11:33