Create a button in a ListView

2

I'm making an application and at the end of the data inserts the user will see a summary with a listView . So far so good, but I want to create, right after listview , a finish button, but I can not seem to 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));

    }
    
asked by anonymous 30.01.2014 / 17:04

3 answers

1

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

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

Through the

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 may hide partially or completely any item. To avoid this include

android:paddingBottom="48dp"

in your list (Adjust the values according to your needs). In this way, the end of the list scroll 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>
    
31.01.2014 / 19:37
1

You need to create a View that contains the ListView and Button. Something like that 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>
    
30.01.2014 / 17:30
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>
    
31.01.2014 / 12:33