Use of Spinner on Android

Asked

Viewed 152 times

1

I would like to know how to put the values that will appear in Spinner. I already have the values, they will always be the same, without the need to come from a database. The problem (or not) is that this Spinner is inside a Dialog that I created to perform the filter action of a particular list.

Below is the predefined Spinner values:

    <string-array name="spinner">
    <item>título</item>
    <item>descrição</item>
</string-array>

Below the Spinner XML:

<Spinner
    android:id="@+id/dlg_filtro_spn_filtro"
    style="@style/spinner_style"
    android:layout_margin="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/dlg_filtro_lb_filtro" />

And below the Dialog class where I use to make the filter (where is located the due Spinner):

package br.com.alura.blocodenotas.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import br.com.alura.blocodenotas.R;

public class DialogFiltro extends AlertDialog.Builder {

    private String cancelar;
    private String filtrar;

    @SuppressLint("NewApi")
    public DialogFiltro(Context context) {
        super(context);
        this.setView(R.layout.dialog_filtro);
        this.setCancelable(true);
        this.setCancelar("CANCELAR");
        this.setFiltrar("FILTRAR");
    }

    public DialogFiltro setCancelar(String cancelar) {
        this.cancelar = cancelar;
        return this;
    }

    public DialogFiltro setFiltrar(String filtrar) {
        this.filtrar = filtrar;
        return this;
    }

    public DialogFiltro setOnCancelarListener(DialogInterface.OnClickListener onClickListener) {
        this.setNegativeButton(this.cancelar, onClickListener);
        return this;
    }

    public DialogFiltro setOnFiltrarListener(DialogInterface.OnClickListener onFiltrarListener) {
        this.setPositiveButton(this.filtrar, onFiltrarListener);
        return this;
    }

    public Dialog build(){
        return this.create();
    }
}
  • What is your specific difficulty? The mode(s) (s) of filling a Spinner are the same regardless of where it is done(Activity, Fragment or other).

  • @ramaral, I call the Dialog class (where it contains the spinner) in my main class, but if I try to make a findViewById within the class that extends the Dialog, I can’t. And if I do findViewById in the main Activity, the object is null. My question was how to use it in the right way.

1 answer

1

Whoa, that’s all right!?

If I understand correctly, you want to create a spinner within a dialog, correct?

Follow the example I have here:

My activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:layout_margin="50dp"
    android:gravity="center"
    >

    <Button
        android:id="@+id/botaoDialogo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Abrir dialogo"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

My Mainactivity.java, is where I create a Alertdialog with a spinner, where it is possible to capture the result of spinner (or edit and leave your need).

public class MainActivity extends AppCompatActivity {

    Button botaoDialogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        botaoDialogo = findViewById(R.id.botaoDialogo);

        botaoDialogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.spinner, null);
                mBuilder.setTitle("Dialog com spinner");
                final Spinner mSpinner = mView.findViewById(R.id.dlg_filtro_spn_filtro);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item,
                        getResources().getStringArray(R.array.listaSpinner));
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinner.setAdapter(adapter);

                mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Selecione…")) {
                            Toast.makeText(MainActivity.this,
                                    mSpinner.getSelectedItem().toString(),
                                    Toast.LENGTH_SHORT)
                                    .show();
                            dialog.dismiss();
                        }
                    }
                });
                mBuilder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                mBuilder.setView(mView);
                AlertDialog dialog = mBuilder.create();
                dialog.show();;
            }
        });
    }
}

xml strings.

<resources>
    <string name="app_name">teste</string>

    <string-array name="listaSpinner">
        <item>Selecione…</item>
        <item>título</item>
        <item>descrição</item>
    </string-array>
</resources>

And finally, the layout of spinner.xml (creating a separate layout is possible to customize it and leave with a friendly face)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Spinner
        android:layout_height="45dp"
        android:layout_width="match_parent"
        android:id="@+id/dlg_filtro_spn_filtro"
        android:layout_margin="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</LinearLayout>

Well, and that’s the end result, I hope it helped you. Have a good day!

inserir a descrição da imagem aqui

Browser other questions tagged

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