Create a button in a Listview

Asked

Viewed 2,371 times

2

I am making an application and at the end of the data inserts the user will view a summary with a listView. So far so good, but I want to create, right down after the listview, a finish button, but I can’t put it, if I put it inside the XML, it inserts the button inside all the items. How do I do this?

My XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
    <TextView 
       android:id="@+id/tema"
       android:layout_width="wrap_content" android:layout_height="wrap_content"
       android:textColor="#000000"
       />
    <TextView 
       android:id="@+id/palavras"
       android:layout_width="wrap_content" android:layout_height="wrap_content"
       android:textColor="#000000"
       />
</LinearLayout>

My code:

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        String [] tema = new String[] {"Tema1", "Tema2", "Tema3"};
        String [] tempo = new String[] {"1:20", "2:32", "1:10"};
        String [] palavras = new String[] {"Palavras, curió, celular, computador","Camisa, mochila, Sara, SBC","Tunts, Tunts, Quero, ver"};

        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();

        for(int i=0; i<3; i++ ) {
            HashMap<String,String> item = new HashMap<String,String>();
            item.put("tema", tema[i]+": "+tempo[i]);
            item.put("palavras", palavras[i]);
            list.add(item);
        }

        // Simpler Adapter

        String[] from = new String[] {"tema","palavras"};
        int[] to = new int[] {R.id.tema, R.id.palavras};
        setListAdapter(new SimpleAdapter(this,list,R.layout.activity_main,from,to));

    }
  • 2

    You would have been able to display your XML?

  • Your problem is with your LinearLayout or RelativeLayout you are using to maintain the ListView, probably the property Orientation its layout is incorrect...

  • I added the code. How can I put the button there, because if I add it in xml, it creates a button for each pair of items.

  • That one XML that you passed is the ListView, just do what the answer proposes and insert the button at the end of the XML who is the ListView.

3 answers

1


There is more than one way to solve this problem, including through fragments.

This form below is one of the simplest and I believe will suit you.

Through the attribute

android:layout_alignParentBottom="true"

the layout will remain fixed at the bottom of the screen. The list in turn "will pass" below the bottom of the layout which can partially or fully hide some item. To avoid this include

android:paddingBottom="48dp"

in your list (Adjust the values according to your need). This way the end of the scroll of the list will coincide with the beginning of the layout in which the button is inserted.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/lista"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adicionar"
        android:divider="@null"
        android:listSelector="@null" 
        android:paddingBottom="48dp"
        />
    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="@android:color/holo_blue_light"
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</RelativeLayout>

1

You need to create a View which contains the Listview and Button. Something like this should work.

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
    <ListView android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

0

<?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"
android:layout_margin="7dp" >

<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/adicionar"
    android:divider="@null"
    android:listSelector="@null" />

<Button
    android:id="@+id/adicionar"
    android:layout_width="match_parent"
    android:text="@string/adicionar"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

</RelativeLayout>
  • It would be nice to have a android:layout_below="@id/lista" in the Button so that one does not overlap the other in case the list gets too large.

  • John, the code I gave you is for the list XML, you used the code in the list Adapter XML ("line/list item").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.