Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android spinner prompt

I have a problem with android:prompt for a spinner. I used this code in the XML file but it doesn't work:

<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="120dp"
    android:prompt="@string/club_type">
</Spinner>

I also tried to use this code in my main activity but this doesn't work either:

spinner.setPrompt("Select club");

While I was using the second case I didn't use android:prompt; in other words, I tried them individually. Could someone help me?

like image 410
atapi19 Avatar asked Oct 24 '14 07:10

atapi19


People also ask

What are android spinners?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.


1 Answers

There are two ways you can deal with that:

Static way:

add one line code in XML's Spinner tag

android:spinnerMode="dialog"

and then set:

android:prompt="PROMPT"

In dynamic way:

use

Spinner spinner = (Spinner)findViewById(R.id.spnner); 
String[] myItems= getResources().getStringArray(R.array.spinner1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, 
android.R.layout.select_dialog_item, myItems);

spinner.setPrompt("PROMPT");

when you set and initialize your adapter

hope that can help you! :)

like image 103
RobotCharlie Avatar answered Sep 18 '22 16:09

RobotCharlie