Search code examples
androidandroid-linearlayoutandroid-scrollviewandroid-imagebutton

How do I make an ImageButton rest at the bottom of a LinearLayout


XML FILE

<include
    android:id="@id/my_toolbar"
    layout="@layout/toobar"
    />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/LinearEditTexts"
        >



        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Title_input"
            />

        <ImageButton
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_input_add"
            android:id="@+id/imageButton2"
            android:layout_width="50dp"
            android:layout_gravity="bottom"
            android:onClick="AddNewTextFields"


            />


    </LinearLayout>

</ScrollView>

My Java file where i make EdtTexts Programmatically List alledittexts = new ArrayList();

public void AddNewTextFields(View view) {
    TotalEdittexts++;
    if (TotalEdittexts > 100)
        return;
    EditText editText = new EditText(this);
    Containerlayout.addView(editText);
    editText.setGravity(Gravity.TOP);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
    layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
    layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

    editText.setLayoutParams(layoutParams);
    //if you want to identify the created editTexts, set a tag, like below
    editText.setTag("AddedEditText" + TotalEdittexts);

    alledittexts.add(editText);

}

I'm trying to get my image button rest underneath the EditText created by that image button every-time the onClickListener() calls that function.
Setting its Gravity didn't change anything I would love to get some help on this.


Solution

  • Try this:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        androidd:orientation="vertical">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/linearlayout">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_rew"/>
    </LinearLayout>