This question already has answers here:
How to make an Android Spinner with initial text "Select One"?
(36 answers)
Closed 9 years ago.
I'm trying my Spinner to display "Select City" before the Spinner has itself been clicked by the user.
How can I do this?
My current XML code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/page_background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="#string/rate_card"
android:textColor="#color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="#+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:prompt="#string/selectCity" />
</LinearLayout>
Also, what does android:spinnerMode exactly do. I tried changing its value to dropdown but nothing happened and the application still showed a popup dialogue.
My activity that implements this XML file is:
public class RateCardActivity extends OlaActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
you can do like this .. Create a custom adapter to insert..
Try the following code
In your resources
<string-array name="listarray">
<item>Select One</item>
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</string-array>
In your onItemSelected Listener:
TextView selection;
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
}else {
// Your code to process the selection
}
}
});
For more info go through this Link & Link
CHange :
extends OlaActivity to extends Activity
Related
I want to create Three spinner. In first Spinner, I have to display countries name and according to selection of country name, I have to load the state name of that country in second Spinner and according to selection of state, I have to load cities name of that state in Third Spinner. Can any one post any example and please suggest which technique is better defining spinner data in Java or Xml.
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, locations);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
android:entries="#array/acc_type" />
<!-- Select Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblSubAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
Resource xml should be like following
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner Example</string>
<string name="action_settings">Settings</string>
<string name="lblAcc">Select Account Type</string>
<string name="lblSubAcc">Select Account Head</string>
<string-array name="acc_type">
<item>Income</item>
<item>Expense</item>
</string-array>
</resources>
And the Java class to use
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");//You should add items from db here (first spinner)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
With if else ladder you can add more spinner like 3rd spinner depending on 2nd one and so on.
Just to clarify first, I'm a newbie with Java and Android, and I'm still in a heavy learning process.
Anyway...
I'm working on a simple Parking SMS app, and I'm stuck with Spinner and a TextView.
Basically, I'm trying to do this: when I select a Parking zone from a Spinner (it has five parking zones), I want to populate a Parking zone description to a TextView below Spinner in the Layout.
Spinner has its own StringArray, while Parking zone descriptions are each in its own Strings.
So it looks like this:
activity_glavni.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Glavni">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txGrad"
android:id="#+id/txOdaberiteGrad"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnergradovi"
android:layout_below="#+id/txOdaberiteGrad"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:id="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txZona"
android:text="#string/txZona"
android:layout_below="#+id/spinnergradovi"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerZona"
android:layout_below="#+id/txZona"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txPrikazZone"
android:layout_below="#+id/spinnerZona" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextRegistracija"
android:layout_below="#+id/txPrikazZone"
android:layout_alignParentStart="true"
android:hint="Unesite broj registracije" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btn_posalji"
android:id="#+id/btn_posalji"
android:layout_below="#+id/editTextRegistracija"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:layout_alignParentEnd="false"
/>
strings.xml
<resources>
<string name="app_name">ParkingZagreb</string>
<string name="action_settings">Settings</string>
<string name="txGrad">Odaberite grad</string>
<string name="txZona">Odaberite zonu</string>
<string name="btn_posalji">Pošalji SMS!</string>
<string-array name="gradovi">
<item>Zagreb</item>
<item>Velika Gorica</item>
<item>Zaprešić</item>
</string-array>
<string-array name="zone">
<item>Prva zona</item>
<item>Druga zona</item>
<item>Treća zona</item>
<item>Četvrta zona (1)</item>
<item>Četvrta zona (2)</item>
</string-array>
<string-array name="opisi_zona">
<item>6 kn/h, maksimalno 2 h</item>
<item>3 kn/h, maksimalno 3 h</item>
<item>1,5 kn/h, bez ograničenja</item>
<item>5 kn/dan, 7-16 h</item>
<item>10 kn/dan, 7-20 h</item>
</string-array>
Glavni.java
public class Glavni extends ActionBarActivity {
public TextView txPrikazZone;
private Button btn_posalji;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavni);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
btn_posalji = (Button) findViewById(R.id.btn_posalji);
btn_posalji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Glavni.this).create();
alertDialog.setTitle("SMS poslan!");
alertDialog.setMessage("Provjerite SMS aplikaciju. Poruka bi trebala doći za nekoliko trenutaka.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
// the first city selection Spinner
Spinner spinner_gradovi = (Spinner) findViewById(R.id.spinnergradovi);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gradovi, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_gradovi.setAdapter(adapter);
// the second Spinner with parking zones
final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.zone, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_zona.setAdapter(adapter2);
// this is where I'm trying to populate the TextView with the String-Array by selecting an item from the second Spinner
final String[] opisi_zona = getResources().getStringArray(R.array.opisi_zona);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(opisi_zona[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_glavni, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for any help...
The reason for the null pointer is that you have not initialized txPrikazZone and you are trying to call setText.
You need to initialize it in your onCreate method like this:
txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);
I hope this helps.
Your adapter it´s the same for both Spinners
EDIT
First try wrapping your ZonaX strings into a string-array
<string-array name="zonas">
...
</string-array>
And now in the code change the set of the ItemSelectedListener
final String[] zonas = getResources().getStringArray(R.array.zonas);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(zonas[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I was trying to implement an android spinner, a button, if I click should open a dropdown menu showing three things: "bluetooth, speak, headphones"
but all i see now is the button, (no text), and when I click on it, nothing happens. what am i missing?
here is my code:
in the xml i have:
<RelativeLayout
android:id="#+id/audio_routing"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp" >
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:contentDescription="#string/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
</RelativeLayout>
in the strings file i have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="audio_routing_desc">Audio Routing Button</string>
<!-- Audio Routing Drop Down List -->
<string-array name="audio_routing_array">
<item>Bluetooth</item>
<item>Headphones</item>
<item>Speaker</item>
</string-array>
in my java file i have:
void audio_routing() {
kcLogger.info("audio_routing", "audio_routing function is called");
Spinner spinner = (Spinner) mKCSWindowView.findViewById(R.id.audio_routing_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(service,R.array.audio_routing_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onItemSelected");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onNothingSelected");
}
});
}
Change android:contentDescription to android:entries
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:entries="#array/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
android:contentDescription is for accessibility features.
android:entries is used to set array data to UI like spinner, listview, gridview,etc.
Is there a way to set the dropDown background for a MultiAutoCompleteTextView in the xml layout? Or must it always be done programmatically?
In the adapter you have created for the MultiAutoComplete, use a custom layout file instead of android's simple_list_item_1.
In that way you will be able to change the layout of the DropDown menu and to add the background you want.
find below a clear example of a MultiAutoComplete using a custom xml layout :
Main Activity, MainActivity.java:
public class MainActivity extends Activity {
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the string-array defined in strings.xml
String[] items = getResources().getStringArray(R.array.itemlist);
//set your custom xml file to be able to edit it
adapter = new ArrayAdapter<String>(this,android.R.layout.custom_drop_down_menu,items);
//multiAutoComplete field
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);
// set adapter for the multi auto complete fields
multiAutoComplete.setAdapter(adapter);
// specify the minimum type of characters before drop-down list is shown
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
//toast message when the user clicks an item of the drop-down list
multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(),"you added: "+arg0.getItemAtPosition(arg2),Toast.LENGTH_LONG).show();
}
});
}
}
custom MultiAutoComplete layout, custom_drop_down_menu.xml :
<!-- add your background image here -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="6dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#000"
android:background = "#drawable/imagename"/>
Strings file ( to add your string-array items), Strings.xml :
<resources>
<string name="multi_auto_complete_items">Multi Auto Complete items</string>
<string-array name="itemlist">
<item >item1</item>
<item >item2</item>
<item >item3</item>
<item >item4</item>
<item >item5</item>
</string-array>
</resources>
Main Activity layout, activity_main.xml :
<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=".MainActivity" >
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/multi_auto_complete_items"
android:textAppearance="?android:attr/textAppearanceMedium" />
<MultiAutoCompleteTextView
android:id="#+id/multiAutoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:layout_marginTop="20dp"
android:ems="13" />
</RelativeLayout>
I have a simple layout (two TextViews and one Spinner), the array and the MainActivity.java which in the onCreate method, through an adapter populates the Spinner.
When I execute the app I can click on the Spinner but nothing happens in android 4.3(API level 16) and a empty, white row is displayed in android2.3.3(Api level 10).
Shouldn't I at least see and select one of the strings?
main.xml:
<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" >
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/question" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/question"
android:text="#string/one" />
<Spinner
android:id="#+id/nationalities_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/question"
android:layout_toRightOf="#+id/textView1"
android:entries="#array/nationalities_array"
android:tag="#string/select_string" />
</RelativeLayout>
The array for the Spinner:
<string-array name="nationalities_array">
<item>Italian</item>
<item>German</item>
<item>British</item>
<item>Dutch</item>
</string-array>
and the MainActivity.java:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner nationalities_spinner = (Spinner) findViewById(R.id.nationalities_spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, R.array.nationalities_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nationalities_spinner.setAdapter(adapter);
}
public void onResume(){
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You don't see something in your Spinner because you use an incorrect constructor for the ArrayAdapter which results in an empty ArrayAdapter for your Spinner. You use this ArrayAdapter constructor and provide something different than what it wants(a layout file resource and an id of a TextView from that layout file). Instead you probably want this :
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.nationalities_array, android.R.layout.simple_spinner_item);