user3460821
user3460821

Reputation: 7

How do I create a view that has a edit text as well as a button?

My code below shows what I want to make. I want to insert a view as a footer to a ListView. However It seems that I can only enter an edit text or a button separately, not as one all together view. I know this sounds confusing. Thanks for any help.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@drawable/bg_card"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >

<EditText
    android:id="@+id/txtTitle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:textColor="#000000"
    android:textSize="22dp"
    android:textStyle="bold" />


<Button
    android:id="@+id/newButton"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" />

</LinearLayout>

Upvotes: 0

Views: 66

Answers (2)

Goran Horia Mihail
Goran Horia Mihail

Reputation: 3645

Inflate the layout:

View v = LayoutInflator.from(context).inflate(R.layout.footer, null);

And add it as footer to the listview:

listview.addFooterView(v);

Edit: If you are targeting api level < kitkat, you have to call addFooterView before calling

listview.setAdapter();

Upvotes: 3

Bruce
Bruce

Reputation: 2377

I'm not aware of any problem with making a LinearLayout be your footer view. Your XML does have some issues, however.

  • You had some RelativeLayout params although the parent is LinearLayout.
  • Both child views are fill_parent width. Better to make them both wrap_content width and use layout_weight to fill any extra space.
  • Since you have padding in the parent, you probably don't also need top/bottom margins in the EditText.
  • You had gravity as an attribute on your LinearLayout, but it does not apply to that view. You also have layout_gravity, which is fine: it says how this LinearLayout will fit inside the parent ListView. It will put the header in the center.

Here is a version with those fixes:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:background="@drawable/bg_card"
    android:orientation="horizontal"
    android:padding="10dp" >

    <EditText
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textColor="#000000"
        android:textSize="22dp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/newButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />
</LinearLayout>

If you are still seeing problems, maybe you can describe what shows up.

Upvotes: 0

Related Questions