2

I want to know whether I can make a common app for mobile and tablet. Right now I have developed an app for mobile which has buttons. But when I go to tablet the button size remains the same. I want to increase button size as we go to tablet.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
Raj Suvariya
  • 249
  • 1
  • 4
  • 14
  • 1
    check this logic http://stackoverflow.com/questions/32860815/how-to-define-dimens-xml-for-every-different-screen-size-in-android/32861248#32861248 – IntelliJ Amiya Jul 09 '16 at 09:12

1 Answers1

10

Actually, you are encouraged to make better use of more space in tablet, e.g. use different layout with Fragments for different screen size.

However, if you just want to make things bigger for tablets you can simply use a resource qualifier in android. In your case, create another values directory in the res folder, e.g. values-sw600dp for tablet with smallest width from 600dp, and then put dimens.xml inside the folder.

Inside that res/values-sw600dp/dimens.xml

<resources>
    <!-- Customization of dimensions in res/values/dimens.xml -->
    <dimen name="button_layout_width">200dp</dimen>
    <dimen name="button_layout_height">100dp</dimen>
    <dimen name="button_text_size">20sp</dimen>   
</resources>

Also, put the same items into the default dimens.xml in res/values/dimens.xml

<resources>
    <!-- Default dimension for phone  -->
    <dimen name="button_layout_width">120dp</dimen>
    <dimen name="button_layout_height">48dp</dimen>
    <dimen name="button_text_size">12sp</dimen>        
</resources>

Then use them in your layout xml file

<Button
    android:layout_width="@dimen/button_layout_width"
    android:layout_height="@dimen/button_layout_height"
    android:textSize="@dimen/button_text_size"
    android:text="My Button"
    />

The system will use the correct values for you based on the qualifier. In your case, choose correct dimension based on the smallest width of the device your app run on.

Learn more about the resource qualifier here

Ken Ratanachai S.
  • 3,307
  • 34
  • 43