Search code examples
androidbuttonadtscale

Android: scale several buttons to screen size


i've got a problem. In my app I have some (3-4) buttons which a want to scale automaticly to the full screen size. (Not a only a single button but all together). At the moment i'm using LinearLayout for my app. Is it possible to do this with it? Sorry for my bad english.

Greetings Nils

That screen of the game "Dont't tap the White" is similar to like I'd like it:


Solution

  • You can use Weight concept for it. Your buttons will be look same in all screens.

    Firstly divide screen in 2 horizontal parts with width weight 50 50 each and height match parent. Then Add 3 buttons to each parts with weight in height as 33.33 as below:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
            <Button
                android:id="@+id/button2"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
            <Button
                android:id="@+id/button3"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/button4"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
            <Button
                android:id="@+id/button5"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
            <Button
                android:id="@+id/button6"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="33.33"
                android:text="Button" />
    
        </LinearLayout>
    
    </LinearLayout>