Sameer Shabbir Sameer Shabbir - 22 days ago 7
Android Question

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 edittexts created by that image button everytime the onclick Listener calls that function.

Setting its Gravity didn't change anything i would love to get some help on this.

Answer

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>
Comments