0

I am using three spinners in my code, I assigned an single array to three spinners but I want to change the default text of the three spinners.I getting array details in the spinners.

Spinner sp1,sp2,sp3;
ArrayAdapter<CharSequence> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carrer_seq);
    apply = (Button) findViewById(R.id.button3);
    sp1 = (Spinner) findViewById(R.id.spinner);
    sp2 = (Spinner) findViewById(R.id.spinner2);
    sp3 = (Spinner) findViewById(R.id.spinner3);

    adapter = ArrayAdapter.createFromResource(this,R.array.current, R.layout.spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp1.setPrompt("Developers(2-3 years)");
    sp1.setAdapter(adapter);
    sp2.setPrompt("Senior Developers(4-5 years)");
    sp2.setAdapter(adapter);
    sp3.setPrompt("Project Lead(6-7 years)");
    sp3.setAdapter(adapter);

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textColorHint="@color/text"
    android:textColor="#808080"/>
sun
  • 1,832
  • 4
  • 19
  • 31
  • have you tried these http://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color – Ravindra Kushwaha Dec 21 '15 at 12:27
  • yes i tried but here i want to change my three spinners default text only. – sun Dec 21 '15 at 12:35
  • Did u try above link....Or you are not getting on it?? – Ravindra Kushwaha Dec 21 '15 at 12:36
  • yes its not getting on it. it getting the contents in array – sun Dec 21 '15 at 12:37
  • have you create the custom layout for the spinner? – Ravindra Kushwaha Dec 21 '15 at 12:39
  • There is no prompt displayed on a spinner on Honeycomb and later, that's why you can't see any effect of the `setPrompt` method. If you want to change the "default text", you will have to incorporate them into the spinner's value array. You'll have to implement it yourself, see e.g. [this](http://stackoverflow.com/questions/17063611/show-default-value-in-spinner-in-android#answer-17063675). – Ace_McIntosh Dec 21 '15 at 12:44
  • @HeamanthVarma ... DId u resolve your problem?? – Ravindra Kushwaha Dec 21 '15 at 13:50
  • getting an error on sp1.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_item, R.array.current, "Developers")); at R.array.current it is a int resource and in customer spinner adapter it is a java.lang.string – sun Dec 21 '15 at 13:59
  • I think after Icecream sandwich version, setPrompt is not working in spinnerMode "dropdown", it is working in spinnerMode "dialog". – Srikanth Dec 21 '15 at 14:38
  • hey after adding sp1.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_item, getResources().getStringArray(R.array.current), "Developers(2-3 years)")); this code in my class i got it thank you @ravindra. – sun Dec 24 '15 at 07:49

2 Answers2

0

Use these below line of code

  Spinner sp1,sp2,sp3;
ArrayAdapter<CharSequence> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carrer_seq);
    apply = (Button) findViewById(R.id.button3);
    sp1 = (Spinner) findViewById(R.id.spinner);
    sp2 = (Spinner) findViewById(R.id.spinner2);
    sp3 = (Spinner) findViewById(R.id.spinner3);

   sp1.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, YOUR_ARRYLIST, "Developers(2-3 years)"));

   sp2.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, YOUR_ARRYLIST, "Senior Developers(4-5 years)"));

   sp3.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, YOUR_ARRYLIST, "Project Lead(6-7 years)"));

And than after create the custom adapter for it

public class CustomSpinnerAdapter extends ArrayAdapter<String>{

Context context;
String[] objects;
String firstElement;
boolean isFirstTime;

public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.objects = objects;
    this.isFirstTime = true;
    setDefaultText(defaultText);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if(isFirstTime) {
        objects[0] = firstElement;
        isFirstTime = false;
    }
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) { 
    notifyDataSetChanged();
    return getCustomView(position, convertView, parent);
}

public void setDefaultText(String defaultText) {
    this.firstElement = objects[0];
    objects[0] = defaultText;
}

public View getCustomView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.spinner_row, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_text);
    label.setText(objects[position]);

    return row;
}

}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0

Maybe this will help:

    current.add("value1");
    current.add("value2");
    current.add("value3");

    current.add("Choose Size"); //adding String at the end of ArrayList
    Collections.reverse(current); //Last Item Will Be Shown As A Spinner Title

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, sizes);
    spinner.setAdapter(adapter);

*** You have to add the value at last as :

 current.add("Choose Size");

and then reverse the array before set adapter.

Vishal Raj
  • 1,755
  • 2
  • 16
  • 35