Android Universal App Approach

后端 未结 2 606
青春惊慌失措
青春惊慌失措 2020-12-30 09:27

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<

相关标签:
2条回答
  • 2020-12-30 09:42

    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.

    0 讨论(0)
  • 2020-12-30 09:55

    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.

    0 讨论(0)
提交回复
热议问题