1

I am new in Android development.I created a simple project with a simple button.I thought that the button will look the same on different screen sizes but when i previewed all screens eclipse displayed this https://i.stack.imgur.com/txOH5.jpg

This is the xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.businessideas.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="116dp"
    android:text="Button" />

</RelativeLayout>

Can you please help me and tell me how can i design that button look the same on all screen sizes? Thanks

  • possible duplicate of [Android Layout Same Relative Size on ALL Screen Sizes](http://stackoverflow.com/questions/11638197/android-layout-same-relative-size-on-all-screen-sizes) – Semih Eker Nov 20 '14 at 14:45

3 Answers3

1

You'll need to use the weight attribute when the button's parent is LinearLayout. That way it will force the button to have a size proportional to the screens width:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="10"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:text="Button"
        android:layout_weight="2"
        android:layout_width="0dp"/>
</LinearLayout>

Notice that if the LinearLayout's orientation is horizontal, width will be adjusted by android (implicitly) therefore the children have width of 0dp.

Also note that now the button will take 20% (2/10) of the LinearLayout's (the parent's) width.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Apparently it is now recommended to use ContraintLayout instead if the LinearLayout is nested (for performance issue) : https://developer.android.com/training/constraint-layout/index.html#constrain-chain – ymoreau Oct 23 '19 at 19:16
0

On the button part change:

android:layout_width="20dp"
android:layout_height="20dp"

Use specific DP sizes to keep items the same physical size on different screen (replace 20 with your desired size).

Leo Starić
  • 331
  • 1
  • 4
  • 16
0

If you want your button to be of the same size in all different screen than you have to manually give the DP size.

like

andoird:layout_width = "20dp"
android:layout_height = "20dp"

You can also place your button at specific location by changing your relative layout to linear layout.

Vikas Chandra
  • 565
  • 1
  • 9
  • 22