Search code examples
androidxmlandroid-layoutbuttonandroid-layout-weight

Square buttons Screen Width and Weight Matching


I have a layout which contains only a LinearLayour with 3 buttons inside.

I would want this 3 buttons to fill the screen vertically so I used "weigth=1" but I should also want the buttons to be square, that is, that their width is equal to their height.

Any way to do it?

Here's the .xml file

<?xml version="1.0" encoding="utf-8"?>

<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="android.jose.se.Prueba"
    android:background="@drawable/bg"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

    <Button
        android:id="@+id/bHet"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer1"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bGay"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer2"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bLesbi"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer3"
        android:onClick="cambiarBoton"/>

</LinearLayout>
</RelativeLayout>

Solution

  • The best way to do this, is to make a new Class called Square button and this class should extend Button. Then in the onMeasure of this class, simply make your heightSpec as WidthSpec.

    public class SquareButton extends Button {
    
    public SquareButton(Context context) {
        super(context);
    }
    
    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    protected void onMeasure(int WidthSpec, int HeightSpec){
        int newWidthSpec = HeightSpec ;
    
        super.onMeasure(newWidthSpec, HeightSpec);
     }}
    

    Now you can use this SquareButton instead of a normal Button in your xml like this. The value of android:width attribute will be ignored.

    <com.example.package.SquareButton
         android:height = "0dp"
         android:weight = "1"
         android:width = "0dp"
    />