How do I insert a button after a listview?

0

Hello, I need to insert a button right after my listview. That is, when I finish sliding my listview will have a button at the end.

I'm trying to do as follows:

<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"
    tools:context="com.example.gerdaumanagement.gerdaumanagement.tipoMensal"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true">
    </ListView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Enviar"
        android:textAllCaps="true" />



</LinearLayout>

But it does not appear at the end.

My listView is being populated:

public List<AvaliacaoMensal> todosMensal() {
        List<AvaliacaoMensal> dados = new ArrayList<AvaliacaoMensal>();
    dados.add(new AvaliacaoMensal("As máquinas e equipamentos possuem selo de liberação por um líder Gerdau e está dentro do prazo de validade?", 'A', "Condição Fisica"));
    
asked by anonymous 08.06.2017 / 20:20

4 answers

1

You can use a RelativeLayout, align the Button at the bottom, cause the ListView to occupy all the space above Button, as below:

<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"
tools:context="com.example.gerdaumanagement.gerdaumanagement.tipoMensal">

<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/botao"
    android:layout_alignParentStart="true">
</ListView>
<Button
    android:id="@+id/botao"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enviar"
    android:textAllCaps="true"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>
    
08.06.2017 / 20:33
1

Since you are using ListView , you can use the addFooterView method, in which you will add a View to the bottom of your list, as if it were the last item. See:

Button btnFooter = new Button(this);
btnFooter.setText("Botão Rodapé");
listview.addFooterView(btnFooter);

Another option if you wanted this button to be fixed in the layout footer, you should use a RelativeLayout , which would be David's suggestion. You can adjust your ListView so it does not stay below the button.

    
09.06.2017 / 15:58
0

Adds the button as a ListView item, you can detect when the button was clicked in two ways by seeing "int id" and "int position" of onItemClick, or you can set "setOnClickListener" on the button.

Basic example:

ListView listView = new ListView(context);

Button button = new Button(context);

button.setText("Enviar");

button.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
));

/* Opção 1: Evento diretamente no botao */
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        /*[...]*/
    }
});

listView.addView(button);

/* Opção 2: Detectar se o item clicado é o botão */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

    }
});

I've done the example programmatically without xml just so you can understand

    
08.06.2017 / 21:04
0

You can do another approach as well. Demoting for your ListView to finish before Buttom and using a scrollView to display the full content of the list would look something like this:

<?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">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttom">
    </ListView>

    <Buttom
        android:id="@+id/buttom"
        android:text="Teste"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </Buttom>
</RelativeLayout>

In this example the button will be below your list, but it will need a scroll to be seen by the whole, another approach is to have a full-screen scroll and leave the ListView with content size, something like this:

<?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">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/buttom">
            </ListView>

            <Buttom
                android:id="@+id/buttom"
                android:text="Teste"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true" >
            </Buttom>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

09.06.2017 / 15:06