I have a spinner in my layout & set the prompt with android:prompt="@string/year"
.
The default prompt looks like:
Now my questions are:
My custom Adapter class
private class MyCustomSpinnerAdapter extends ArrayAdapter<ArrayList> {
private ArrayList<String> objectsList = new ArrayList<String>();
@SuppressWarnings("unchecked")
public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
ArrayList objects) {
super(context, textViewResourceId, objects);
this.objectsList = objects;
}
@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 getCustomView1(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
false);
LinearLayout rowTitle = (LinearLayout) rowView
.findViewById(R.id.row);
rowTitle.setBackgroundResource(R.drawable.spinner_row_focused);
TextView textView = (TextView) rowView.findViewById(R.id.title);
textView.setTypeface(typeFace);
textView.setText(objectsList.get(position).toString().trim());
return rowView;
}
public View getCustomView1(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
false);
TextView textView = (TextView) rowView.findViewById(R.id.title);
textView.setText(objectsList.get(position).toString().trim());
textView.setTypeface(typeFace);
return rowView;
}
}
Here is your solution:
Create simple layout for header (file R.layout.spinner_head
):
<ImageView
android:id="@+id/spin_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:padding="5dip"
android:src="@android:drawable/btn_star"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/title"
android:textColor="#f00"
android:gravity="center"
android:text="Year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
Create custom spinner:
class MySpinner extends Spinner {
public MySpinner(Context context) {
super(context);
}
@Override
public boolean performClick() {
boolean handled = false;//super.performClick();
if (!handled) {
handled = true;
Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
CharSequence mPrompt = getPrompt();
if (mPrompt != null) {
builder.setCustomTitle(getLayoutInflater().inflate(R.layout.spinner_head, null));
}
builder.setSingleChoiceItems(new DropDownAdapter(getAdapter()),getSelectedItemPosition(), this).show();
}
return handled;
}
@Override
public void onClick(DialogInterface dialog, int which) {
setSelection(which);
dialog.dismiss();
}
class DropDownAdapter implements ListAdapter, SpinnerAdapter {
private SpinnerAdapter mAdapter;
public DropDownAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
}
@Override
public int getCount() {
return mAdapter == null ? 0 : mAdapter.getCount();
}
@Override
public Object getItem(int position) {
return mAdapter == null ? null : mAdapter.getItem(position);
}
@Override
public long getItemId(int position) {
return mAdapter == null ? -1 : mAdapter.getItemId(position);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return mAdapter != null && mAdapter.hasStableIds();
}
@Override
public boolean isEmpty() {
return getCount() == 0;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return mAdapter == null ? null : mAdapter.getDropDownView(position, convertView, parent);
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int arg0) {
return true;
}
}
}
Create instance of your spinner
MySpinner theSpin = new MySpinner(this);
theSpin.setPrompt("Select Item");
ArrayAdapter<String> theAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[]{"item1","item2","item3","item4","item5","item6" });
theSpin.setAdapter(theAdapter);
Now use this custom spinner which will show custom header. You can modify that header as per your needs.
I think u better to prepare custom spinner like custom ListView and check this link http://android-er.blogspot.in/2010/12/custom-spinner-with-icon.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With