Reputation: 2370
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;
}
but i want to show as like this
how to do this in Custom spinner ? thanks in advance for help
Upvotes: 6
Views: 22761
Reputation: 489
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);
Upvotes: 0
Reputation: 9870
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;
}
}
}
Upvotes: 2
Reputation: 23
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);
Upvotes: 2
Reputation: 220
You can take a look to this a simple tutorial like this. But I'm really sure you don't want to do this.
So take a look on How to make a custom adapter with layout, tutorial is for listView but it's the same operation for spinner.
You can create your own layout that contain a TextView
and a CheckBox
like that :
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/linearLayout1″
android: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;
}
Upvotes: 0
Reputation: 138
You can change background image of spinner :
<?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"
to your spinner.So it will looks like :
<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>
Upvotes: 0