How to use a Spinner in an AlertDialog?

3

I'm in doubt how to put a spinner in an 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>

AlertDialog Method:

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();
}
    
asked by anonymous 24.03.2016 / 09:44

1 answer

2

You need to create a ArrayAdapter that links < in> string-array with Spinner .

The class ArrayAdapter has the static method createFromResource() that creates an adapter from a string-array .
Assign the adapter created to Spinner using the setAdapter () .

Change the chamarDialog() method 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();
}
    
24.03.2016 / 20:31