0

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);
    }
}
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Ankush
  • 6,767
  • 8
  • 30
  • 42

2 Answers2

0

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

Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
  • I'm guessing that the default Spinner text should be declared in the `onNothingSelected` method. What should the code be in order to achieve this? – Ankush Oct 09 '12 at 09:11
0

CHange : extends OlaActivity to extends Activity

Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44