Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change style of custom spinner in android

my view is as like this, I am Using Custom adapter for spinner

I have design my spinner from this link http://rimpv.blogspot.in/2013/05/bind-spinner-dropdown-in-android.html

@Override
public View getView(int position, View convertView, ViewGroup parent) {
            TextView v = new TextView(getApplicationContext());
            v.setTextColor(Color.BLACK);
            v.setText(data.get(position).name);
            return v;
        }

enter image description here

but i want to show as like this enter image description here

how to do this in Custom spinner ? thanks in advance for help

like image 870
stackoverflow Avatar asked May 16 '13 12:05

stackoverflow


People also ask

What is spinner How do you create spinner and its adapter explain with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


2 Answers

You can create a new "doubleline_spinner.xml" with source code

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ellipsize="none" 
android:textSize="15dp"

/>

And add that into your spinner

        ArrayAdapter<CharSequence> PolarityAAdapter = ArrayAdapter.createFromResource(this,
        R.array.polarity_arrays,android.R.layout.simple_spinner_item);
        PolarityAAdapter.setDropDownViewResource(R.layout.doubleline_spinner);
        PolarityAspinner.setAdapter(PolarityAAdapter);
like image 70
sachin Avatar answered Nov 15 '22 00:11

sachin


EDIT 3

This examples are just from scratch, got no IDE to test it now.

If you don´t need a custom adapter, you can work with standard ArrayAdapter. Then set the dropDownViewResource for Your adapter.

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    Spinner s = (Spinner) this.findViewById(R.id.spinner);

    ArrayList<String> list = new ArrayList<String>();
    list.add("Germany");
    list.add("USA");
    list.add("Nairobi");
    list.add("Japan");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {

            Toast.makeText(getApplicationContext(),
                    "CLICKED:"+parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    });
}

But if You need a customAdapter, then there is no other than build Your own spinner item layout:

Build Your main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    >
   <Spinner 
      android:drawSelectorOnTop="true"
      android:id="@+id/example_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"  > 
   </Spinner>
  </LinearLayout>

Build Your Spinner item xml: spinner_item.xml:

    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/spinner_item_linear_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"
       android:orientation=”vertical”>


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

    <RadioButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/spinner_radio_button” >
   </RadioButton>

 </LinearLayout>

Define a string array with some input, create your custom adapter and set the adapter to your spinner.

    public class CustomSpinnerExample extends Activity {


  String []countries ={"Germany","USA","Nairobi","Japan"};

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner mySpinner = (Spinner)findViewById(R.id.example_spinner);
    mySpinner.setAdapter(new CustomAdapter(this, R.layout.spinner_item, countries));
   }


    public class CustomAdapter extends ArrayAdapter<String>
   {

        public CustomAdapter(Context context, int resourceId,
                    String[] objects) {
              super(context, resourceId, objects);
              // TODO Auto-generated constructor stub
        }
        @Override
     public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
     }

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

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

        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.spinner_item, parent, false);
        TextView label=(TextView)row.findViewById(R.id.spinner_textView);
         label.setText(countries[position]);


        RadioButton radioButton    =(RadioButton)row.findViewById(R.id.spinner_radio_button);
       radioButton.setOnClickListener(new OnClickListener(){

   public void onClick(View v){

Toast.makeText(CustomAdapter.this,”CLICKED:”+label.getText(),Toast.LENGTH_LONG).show();

  }
    });

        return row;
        }

   }
}
like image 45
Opiatefuchs Avatar answered Nov 15 '22 01:11

Opiatefuchs