Android Layout with Listview and Buttons

Android Layout with ListView and Buttons

I think this is what you are looking for.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/testbutton"
android:text="@string/hello" android:layout_alignParentBottom="true" />

<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/list"
android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout>

How to create Listview items + Button in each row?

You have to create a custom layout xml which having a single item then you will add your button to this layout along with any other items.

CustomLayout.Xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />

</RelativeLayout>

Now after creating custom item layout you need listview which holds all items.

MainActivity.xml

.
.
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
.
.

Now in java file just set adapter with our custom layout xml

.
.
list = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
listview.setAdapter(new MyCustomAdapter(list, context) );
.
.

Custom adapter Class

public class MyCustomAdapter extends BaseAdapter implements ListAdapter { 
private ArrayList<String> list = new ArrayList<String>();
private Context context;

public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int pos) {
return list.get(pos);
}

@Override
public long getItemId(int pos) {
return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.CustomLayout, null);
}

//Handle TextView and display string from your list
TextView tvContact= (TextView)view.findViewById(R.id.tvContact);
tvContact.setText(list.get(position));

//Handle buttons and add onClickListeners
Button callbtn= (Button)view.findViewById(R.id.btn);

callbtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something

}
});
addBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
.
}
});

return view;
}
}

Android Layout with ListView overlaps Buttons

Did you try to use Relative Layout ?

Change Linear Layout to Relative Layout that's all.

As Linear Layout Doesn't support align parent..

Edited

set List Height to mach parent.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/list"
android:layout_below="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >

<Button
android:id="@+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />

<Button
android:id="@+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>

Buttons at the bottom of the list view in Android

If you want to place buttons at the bottom of the list items, then you can use the addFooterView() method of ListView. For instance, this will work for one button added in the footer of the list.

Button btn = new Button(this);
btn.setText("Done");
ListView lst = (ListView) findViewById(R.id.yourlist);
lst.addFooterView(btn);
// * your view containing the 3 buttons *//

This will put the view containing the 3 buttons you mentioned in your image after the last item in the List.

How to Place a Button below a ListView in a Scrollable Activity?

For the behaviour you want, it will be better if you used a RecyclerView instead of a ListView and let it be inside a NestedScrollView, then enable nestedScrolling attribute on the RecyclerView. This way, you RecyclerView will behave like a long list and all views below it will only be visible after the list has been exhausted.
Check the answers in this link to see how others implemented it.



Related Topics



Leave a reply



Submit