1

I have a simple layout. it's a button with the following code:

<Button
                android:layout_width="100dip"
                android:layout_height="100dip"
                android:text="Y" />

I run it on a two devices, one is smaller then the other. i noticed no change in size of button. I though when using dip, the view's size changes according to the screen size. Am i wrong or what?

Usman Riaz
  • 2,920
  • 10
  • 43
  • 66
  • 1
    I think the dip==dp no difference between them! you can check this http://developer.android.com/guide/topics/resources/more-resources.html#Dimension – Maheera Jazi - new account - Jan 17 '15 at 13:38
  • Yes @MaheeraJazi-newaccount- i know. but i see no difference in the button. button that is displayed on the larger screen should be large as compared to the button that is displayed on the smaller screen. right ?? – Usman Riaz Jan 17 '15 at 13:40
  • No cause you use fixed size ,, you can use (FILL_PARENT, WRAP_CONTENT, MATCH_PARENT) for the layout_width & layout_height to see the change on Button size !! ex. android:layout_width="match_parent" android:layout_height="match_parent" – Maheera Jazi - new account - Jan 17 '15 at 13:43
  • I want button to adjust the size automatically according to screen size. ex. When i run the app on smaller screen i see a button but when i run the app on larger screen i want to see that button a little bit bigger. – Usman Riaz Jan 17 '15 at 13:46
  • You have defined size in dip and by reference of official documentation, it should adjust the size itself on different screens. Would you please upload a screenshot so everybody can see how exactly it looks like. – Apurva Jan 17 '15 at 14:56

1 Answers1

1

If you had set that in pixels, size would change because of different screen densities. dp are used to overcome this situation, so that size is fixed.

If you want your dimensions to change proportionally with the screen size, I think you should use android:weight= inside a LinearLayout. I.e.:

<LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:weightSum="3"
     ... >

    <Button
            android:layout_height="100dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Button 1" />

    <Button
            android:layout_height="100dp"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="Button 2" />

</LinearLayout>

In this case, Button 1 will always be 1/3 of the LinearLayout width, while Button 2 will always be 2/3 .

See this pretty popular question.

Community
  • 1
  • 1
natario
  • 24,954
  • 17
  • 88
  • 158
  • i want my button to resize it self according to screen size. can't be it a requirement? it is not a good practice to do?? – Usman Riaz Jan 17 '15 at 13:58
  • It's up to you and how you want your UI to be. If you want to, you need to use `0dp` and `layout_weight` as shown. I can say that hardcoding values (like `100dp`, as in your example) might be bad practice in some cases. – natario Jan 17 '15 at 14:04
  • but there is an image view in my layout. actually that's the profile picture. so how can i handle it on different screen sizes?? – Usman Riaz Jan 18 '15 at 06:24
  • A picture should probably keep its ratio, so LinearLayout is not a good idea. You should put different sized pictures in different folders of your project - take a look [here](http://developer.android.com/training/multiscreen/index.html) – natario Jan 18 '15 at 10:43