Dragos Rachieru Dragos Rachieru - 7 months ago 20
Java Question

How do I change alignment of a button in java?

So, i want to make an adding activity which is used for 3 different tables.
In the last one i want the date EditText to be gone.

I know how to make it disappear but i want the layout o change too and this is how it looks when date EditText is present

3 EditTexts and Button with alignbottom set to date EditText

And this is how it looks when I set date EditText to gone

2 EditTexts and Button with alignbottom set to the gone EditText

Answer

LinearLayout with orientation of vertical containing the EditText, wrapped in a horizontal LinearLayout containing the Button too. The key is in the weights.

enter image description here

enter image description here

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView2" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView3" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_weight="1"
        android:background="#3e3e3e" />
</LinearLayout>