How to use a Spinner in a Alertdialog?

Asked

Viewed 1,152 times

3

I’m in doubt how to put a spinner in a Alertdialog, I already have the codes ready, just need to insert the spinner.

Array:

<string-array name="categoria_array">
    <item>Aeroporto</item>
    <item>Bar</item>
    <item>Casa Noturna</item>
    <item>Cinema</item>
    <item>Colégio</item>
    <item>Loja</item>
    <item>Museu</item>
    <item>Parque</item>
    <item>Restaurante</item>
    <item>Rodoviárias</item>
    <item>Shoppings</item>
    <item>Supermercado</item>
    <item>Teatro</item>
    <item>Universidade</item>
    <item>Outro</item>
</string-array>

Layout:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/titulo"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="NOVA LOCALIZAÇÃO"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:textStyle="bold"
        />

    <EditText
        android:id="@+id/edt_nome"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/titulo"
        android:layout_margin="15dp"
        android:background="@drawable/border_transp_black_redond"
        android:hint="  Nome do Local"
        android:padding="5dp"
        android:textColorHint="@color/colorPrimaryText"
        android:textSize="20sp"
        android:textStyle="italic" />

    <EditText
        android:id="@+id/edt_endereco"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/edt_nome"
        android:layout_margin="15dp"
        android:background="@drawable/border_transp_black_redond"
        android:hint="  Endereço"
        android:padding="5dp"
        android:textColorHint="@color/colorPrimaryText"
        android:textSize="20sp"
        android:textStyle="italic" />

    <EditText
        android:id="@+id/edt_website"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/edt_endereco"
        android:layout_margin="15dp"
        android:hint="  WebSite"
        android:padding="5dp"
        android:inputType="textWebEditText"
        android:textStyle="italic"
        android:textColorHint="@color/colorPrimaryText"
        android:textSize="20sp"
        android:background="@drawable/border_transp_black_redond"/>

    <EditText
        android:id="@+id/edt_telefone"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/edt_website"
        android:layout_margin="15dp"
        android:hint="  Telefone"
        android:padding="5dp"
        android:inputType="number"
        android:textStyle="italic"
        android:textColorHint="@color/colorPrimaryText"
        android:textSize="20sp"
        android:background="@drawable/border_transp_black_redond"/>

    <Spinner
        android:id="@+id/spinner_catogorias"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edt_telefone"/>

</RelativeLayout>

Metodo Alertdialog:

private void chamarDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.layout_newcheckin, null));
    //Dialog dialog = builder.show();
    //dialog.dismiss(); fecha o dialog
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Cliked OK!", Toast.LENGTH_SHORT);
            return;
        }
    });
    builder.show();
}
  • What exactly is your doubt?

  • If I need to create a class for the spinner or if I have to call it in Dialog, how would I do it. I don’t have much experience in android, to start now

  • That’s correct. You’re wondering how to get the spinner value is this?

  • Like I did the array with the values, but as I will make this values appear in the spinner, in case my doubt is how to load the spinner with the array values.

1 answer

2


Needs to create a Arrayadapter make the call from string-array with the Spinner.

The class Arrayadapter has the static method createFromResource() which creates a Adapter from a string-array.
Attribute the Adapter raised to the Spinner using the method setAdapter().

Change the method chamarDialog() in this way:

private void chamarDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();

    //Cria a view a ser utilizada no dialog
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.layout_newcheckin, null);

    //Obtém uma referencia ao Spinner
    Spinner spinner = (Spinner) view.findViewById(R.id.spinner_catogorias);

    //Cria o Adapter
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.categoria_array,
    android.R.layout.simple_spinner_dropdown_item);//ou android.R.layout.simple_spinner_item

    //Atribui o adapter ao spinner
    spinner.setAdapter(adapter);

    builder.setView(view);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Cliked OK!", Toast.LENGTH_SHORT);
            return;
        }
    });
    builder.show();
}
  • Gave runtime error: "Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.Spinneradapter)' on a null Object Reference at br.com.projeto.caminhossembarreiras.Mainactivity.onCreate(Mainactivity.java:57)"

  • Yeah, it can’t be on onCreate() because the spinner is in the dialog. I’ll edit the answer.

  • Okay, thanks for all the help you’re going through!

  • 1

    Have you seen the answer after the change? Is this what you were looking for? If you see it here how to proceed when a reply has been useful to you.

Browser other questions tagged

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