Search code examples
androidandroid-layoutpixeltablet

The dp is suitable for phone but too small for tablet in android


I worked on a app that fit on the phone, but now I need to make it fit for the tablet. The problem is , for the same dp , it works well on phone, however, for the tablet , it is too small , I have searched for the solution like this:

http://developer.android.com/design/style/metrics-grids.html

Found that the dp on tablet is double / triple of the phone. So , if I would like to define a custom actionbar size like the following, how can I fit it to the tablet as well? 60 dp is suitable for phone 4"~5" but too small for 7"~10" tablet

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
    <!-- API 11 theme customizations can go here. -->
    <item name="android:actionBarSize">60dp</item>
</style>

Solution

  • The way Android handle resources allows developers to specify different dimensions based on screen sizes. In your XML layouts you could have:

    <FrameLayout
        android:layout_width="@dimen/subtitle_container_width"
        android:layout_height="wrap_content">
    
    ...
    
    </FrameLayout>
    

    And that value will be specified in different folders, each one specific for your needs.

    Considering that your res directory have a similar structure like the one below:

    res/
        values
        values-sw400dp
        values-sw720dp
    

    you can create a dimens.xml file in each of these folders containing:

    <resources>
        <dimen name="subtitle_container_width">(VALUE IN DP)</dimen>
    </resources>
    

    Android will consider the correct value in runtime for you, automatically. In this example, you can have a specific dimension for devices with the smallest width of 720dp and 400dp. If a device does not have at least 400dp, the default value in the values folder will be used.

    You can create more folders to suit your needs, with different sizes or modifiers (smallest dimension of X, minimum height of Y and so on).

    You can find more info here: http://developer.android.com/guide/topics/resources/providing-resources.html