Anurag Goel
Anurag Goel

Reputation: 468

Android:How To set set deafult value of Spinner?

I am creating a Dropdown(spinner) in which all days have to shown,but i want to set different Default value.Presently default and first value of spinner are same..Please help me how to do this...Code snippets are appreciated.... My is =>

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
            R.array.days, android.R.layout.simple_spinner_item);
   spinner.setAdapter(adapter);
   spinner.setOnItemSelectedListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    TextView tv =(TextView)arg1;
    Toast.makeText(this, "YOu have selected"+tv.getText(), Toast.LENGTH_SHORT).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

 }

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

simple_spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

String.xml

'

<string name="app_name">Spinner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="days">
    <item>Choose A day</item>
    <item>Sunday</item>
    <item>Monday</item>
    <item>Tuesday</item>
    <item>Wednesday</item>
    <item>Thrusday</item>
    <item>Friday</item>
    <item>Saturday</item>
</string-array>

'

Upvotes: 0

Views: 3954

Answers (3)

Nando G.C.
Nando G.C.

Reputation: 1

I have developed a little component to get this. Just add a hint text in AutoCompleteTexview and build CompleteSpinner with it.

https://github.com/FernandoGaliay/CompleteSpinner

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Define your R.array.days with the hint as the last element.

<string-array name="days">
  <item>Sunday</item>
  <item>Monday</item>
  <item>Tuesday</item>
  <item>Wednesday</item>
  <item>Thursday</item>
  <item>Friday</item>
  <item>Saturday</item>
  <item>Choose a Day</item> <!-- Hint -->
</string-array>

Now, instantiate and set your ArrayAdapter as

spinner = (Spinner) findViewById(R.id.spinner1);
String[] dayValues = getResources().getStringArray(R.array.days);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   android.R.layout.simple_spinner_item, dayValues) {

    @Override
    public int getCount() {
         int c = super.getCount();
         if (spinner.getSelectedItemPosition() < c - 1) return c;
         return c > 0 ? c - 1 : c;
    }
};
spinner.setAdapter(adapter);

Finally, set your hint as the default spinner value

spinner.setSelection(dayValues.length - 1);

Upvotes: 1

JavaJade
JavaJade

Reputation: 199

You can to it by using the function : setSelection(Integer)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
        R.array.days, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
    spinner.setSelection(positionSpinner);
}

EDIT : The only solution I think is to create your own spinner adapter and but the text as the last item and using the getCount in order to not display it.

class MySpinnerAdapter extends ArrayAdapter<String> {
    public MySpinnerAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public int getCount() {
        return super.getCount() - 1; // This makes not show the last item
    }

    @Override
    public SpinnerItem getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

}

Finally :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayList itemsSpinner = new ArrayList(); 
    itemsSpinner.add("Day 1"); 
    itemsSpinner.add("Day 2"); 
    itemsSpinner.add("Choose a day"); // Last item

    MySpinnerAdapter adapterSpinner = new MySpinnerAdapter(this, android.R.layout.simple_spinner_item, itemsSpinner );
    adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapterSpinner);
    spinner.setSelection(items.size() - 1);
}

Upvotes: 1

Related Questions