I want to show a button under a ListView
. Problem is, if the ListView
gets extended (items added...), the button is pushed out of the screen.
in LinearLayout, just add android:layout_weight="1"
to you ListView
If you want the content below the list view to move down as elements are added to the list, try this solution:
Add positive padding to the bottom of the list view and negative padding to the top of the "footer". This will work in a linear layout or a relative layout.
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp"/>
The idea is outlined in the very good solutions by:
They both seem to work perfectly at least on v4.4.3.
I still had to spend some time writing test code to check it out and confirm each method. Below is the self contained, minimal test code required.
Layout is based on Sparkle's solution, as it contains fewer nested layout. Also has been updated to reflect the current LINT checks.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Grow List"
tools:ignore="HardcodedText" />
</LinearLayout>
Activity provides the boilerplate code to test the solution for yourself.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView)findViewById(R.id.listview);
final ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new ArrayList<String>());
adapter.setNotifyOnChange(true);
listview.setAdapter(adapter);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int count = adapter.getCount();
adapter.add(String.valueOf(count));
listview.setSelection(count);
}
});
}
}
Feel free to add or improve as this is "community wiki".
wrap content in linear layout
in List View add: android:layout_weight="1"
in your Button set fixed height
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</LinearLayout>
I solved this problem in code, setting height of listview only if it has more than 5 items in it:
if(adapter.getCount() > 5){
View item = adapter.getView(0, null, listView);
item.measure(0, 0);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, (int) (5.5 * item.getMeasuredHeight()));
listView.setLayoutParams(params);
}
Notice that I set the max height to 5.5 times the height of a single item, so the user will know for sure there is some scrolling to do! :)
Found a way to do this without using nested containers, inspired from AngeloS's solution.
I used a LinearLayout
with a vertical orientation, that has a ListView
and a button. I set the android:layout_weight="0.1"
for the ListView
.
It manages to get the button stick to the bottom of the list always. And the button does not get pushed off the screen when the list grows.
<ListView
android:id="@+id/notes_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
/>
<Button
android:id="@+id/create_note_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCreateButtonClicked"
android:text="@string/create_notes"
/>