How do I insert a button after a listview?

Asked

Viewed 645 times

0

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

I’m trying to do it this way:

<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 he doesn’t show up 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"));
  • It’s a simple list or this using custom Adapter?

  • @Custom acklay Adapter.

4 answers

1

How are you wearing ListView, you can use the method addFooterView, in which you will add a View at 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 at the bottom of the layout, you should use a RelativeLayout, which would be David’s suggestion. It is possible to adjust your ListView so she doesn’t get below the button.

  • PERFECT! Now how do I customize this button? @acklay

  • @Lucascharles this is already another question. I suggest you check if there are here stackoverflow ways to customize a button programmatically. If there is not, you can ask a new question. If there is any further question regarding this question, you can ask here.

1

You can use One Relativelayout, align the Button at the bottom, make Listview take up 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>
  • I’ve tried that, but it’s on my listview, I wanted it to appear only at the end of listview. @David Carvalho

  • Is there really a need for it to be done that way? It is not very recommendable considering that if the list is long, the user has the trouble of scrolling the screen numerous times just to perform the button click action. If indeed it is, the example of Mystic will be satisfying.

0

You can take another approach too. Demarcating for your Listview to finish before the Buttom and using a scrollView to display the full content of the list, would look 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 in full, another approach is to have a scroll on the full screen and leave Listview with content size, it looks 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>

Outside these can put as the last element of your Listview the button, but that you will not be able to do straight in xml, it is easier to follow the example of friend Mystic.

0

Add the button as a Listview item, you can detect when the button has been clicked in two ways by seeing "int id" and "int position" from 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 did the example programmatically without xml just so you can understand

  • And how I implement button.setLayoutParams/[...]/? @Could Mystic give me an example with XML? Please?

  • @Lucascharles I added text on the button and the parameters, tell me how you are adding items in this Listview

  • I edited it as I am adding items to my list. I can’t just add the button. It asks for other parameters.

Browser other questions tagged

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