How to adjust button size according to screen size in Android Studio

You have to use layout_weight in order to achieve what you want, and that will be used by wrapping each row of buttons inside a LinearLayout and all that LinearLayouts plus the TextView will be wrapped inside one big LinearLayout instead of your RelativeLayout like the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000"
    android:orientation="vertical"
    android:weightSum="6" >

    <TextView
        android:id="@+id/disp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:inputType="none"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:textSize="32sp"
        android:textStyle="normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4" >

        <Button
            android:id="@+id/clear"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/clear_btn" />

        <!-- then the three remaining buttons -->

        <LinearLayout />

        <!-- then the four remaining rows -->


        <!-- in the last row the "0" button will have layout_weight="2" NOT "1" -->
    </LinearLayout>

</LinearLayout>

UPDATE:

use ImageButtons instead of Buttons for better scalability, and this is how you must use it:

    <ImageButton
            android:id="@+id/clear"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@drawable/clear_btn"
            android:scaleType="fitCenter" />

    <!-- and so for all Buttons -->

Note here we changed:

1- layout_height : from wrap_content to match_parent .

2- background : removing the drawable(clear_btn) and making it transparent.

and we added:

1- src : assigning to it the drawable(clear_btn).

2- scaleType : making it fitCenter to make it scales in all the available area without ruining the Image scalability ratio.

P.S: in case you are want the Button to take all the area weather it is scalable or not, change scaleType from fitCenter to fitXY (may be you will need that for the "0" Button).


While setting the height/width of a button in android, using fixed density pixel values will result in exact sizes on all screens. Using width values as wrap_content, fill_parent, weights etc. will make the button look according to the screen densities.

Try going through this link once:- Best Practices for User Interface


Solution 1: please use dimens.xml for specifying with and height for buttons.

for tablet,please create folder values-sw600dp,values-sw720dp and put your dimens.xml inside.

Solution 2:

Use linearlayout and layout_weight property.

If you want to support multiple screens never drag and drop and resize views manually.

You can check both the android:layout_weight attribute and LinearLayout