How to change user counter style in android

My opinion is just like this, I use a custom adapter for spinner

I have my counter design at 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 how it looks enter image description here

How to do it in Custom spinner? in advance for help

+6
source share
5 answers

EDIT 3

These examples are only from scratch, now there is no IDE to test.

If you do not need a special adapter, you can work with the standard ArrayAdapter. Then set the dropDownViewResource parameter 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 option but to build your own spinner element layout:

Create 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> 

Create your instance of Spinner 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 an array of strings with some input, create your own adapter and install the adapter on your counter.

  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; } } } 
+1
source

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 it to 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); 
+1
source

You can take a look at this simple tutorial like this . But I'm really sure that you do not want to do this.

So, look How to create a custom adapter with a layout , the tutorial is for listView, but this is an operation for spinner.

You can create your own layout containing TextView and CheckBox :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&#8221; android:id="@+id/linearLayout1android:layout_width="fill_parent" android:layout_height="fill_parent" > <CheckBox android:id="@+DropDownList/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+DropDownList/SelectOption" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

and in your function:

 @Override public int getItemViewType(int position) { return R.layout.YourLayout; } 
0
source

Look at this tutorial

You can change the background image of the counter:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Spinner android:drawSelectorOnTop="true" android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/select"> </Spinner> </LinearLayout> 

by adding:

android:src="@drawable/your_image"

on your spinner.So it will look:

 <Spinner android:drawSelectorOnTop="true" android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/your_image" android:prompt="@string/select"> </Spinner> 
0
source

this is getDropDownView

 @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) convertView = inflater.inflate(R.layout.simple_spinner_dropdown_item, null, true); CheckedTextView text = (CheckedTextView) convertView.findViewById(R.id.text1); text.setText("TEXT"); return convertView; } 

And add this after creating the adapter

 myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
0
source

Source: https://habr.com/ru/post/945163/


All Articles