How to change the width and height of a Relativelayout by Java code

Asked

Viewed 622 times

0

I want to know how I adjust the width and height values of each relative layout, independently of my XML through the respective Java class. I already know how to adjust the width, height and other parameters of the buttons present in my layout in the desired way, but the buttons are inside relatives layouts and these relative layouts need to be adjusted tbm and I don’t know how to do this. Follow the XML code:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="227dp">

    <Button
        android:id="@+id/btnSAE"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:background="@drawable/btn_barcode2"
        android:textColorLink="?attr/colorPrimary"
        android:layout_marginBottom="12dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="244dp"
    android:layout_height="75dp">

    <Button
        android:id="@+id/btnBuscarNome"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/btn_busca" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="244dp"
    android:layout_height="75dp">

    <Button
        android:id="@+id/btnSocorro"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/btn_socorro" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="383dp"
    android:layout_height="113dp"
    >

    <Button
        android:id="@+id/btnAtualizar"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/btnSobre"
        android:layout_marginEnd="14dp"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/btnFecharApp"
        android:layout_toStartOf="@+id/btnFecharApp"
        android:background="@drawable/reload" />

    <Button
        android:id="@+id/btnConfig"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="19dp"
        android:layout_marginRight="19dp"
        android:layout_marginTop="11dp"
        android:layout_toLeftOf="@+id/btnSobre"
        android:layout_toStartOf="@+id/btnSobre"
        android:background="@drawable/config" />

    <Button
        android:id="@+id/btnFecharApp"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="67dp"
        android:layout_marginRight="67dp"
        android:background="@drawable/fechar"
        android:layout_alignBottom="@+id/btnAtualizar"
        android:layout_alignTop="@+id/btnAtualizar" />

    <Button
        android:id="@+id/btnSobre"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"

        android:layout_alignTop="@+id/btnConfig"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:layout_toLeftOf="@+id/btnAtualizar"
        android:layout_toStartOf="@+id/btnAtualizar"
        android:background="@drawable/sobre" />

</RelativeLayout>

In Java code I use the following to configure button parameters:

    RelativeLayout.LayoutParams layoutParams1= (RelativeLayout.LayoutParams) btnSAE.getLayoutParams();

    //alguns comandos para cálculos envolvendo as métrica do display (DisplayMetrics)
    //após os cálculos tenho as variáveis indicando os pixels necessários
    //para ajustar meu layout ao celular do usuário

    layoutParams1.setMargins(l,t,r,b);
    layoutParams1.setMarginEnd(e);
    layoutParams1.setMarginStart(s);
    btnSAE.setLayoutParams(layoutParams1);
    btnSAE.setHeight(h);
    btnSAE.setWidth(w);

But this only adjusts the button and not the Relative Layout it is included in (width and height). How do I?

1 answer

1


It is not normally necessary/advisable to assign absolute values to android:layout_width and android:layout_height of layouts.

The normal is to assign match_parent or wrap-content.

However you can change, via java code, these values in the same way you change in any other view. You just need to assign a @id to be able to reference and obtain it with findViewById().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.