Android: Add Two Textviews To Actionbar Spinner
Is there anyway to add two text views or strings inside of the Actionbar Spinner? Similar to how the Android GMAIL app does it.
Solution 1:
You could use a custom ArrayAdapter:
publicclassspinnerAdapterextendsArrayAdapter {
private LayoutInflater inflater;
publicspinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
inflater = LayoutInflater.from(context);
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = newViewHolder();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder2 holder;
if(convertView == null){
holder = newViewHolder2();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder2)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
staticclassViewHolder{
TextView text1;
TextView text2;
}
staticclassViewHolder2{
TextView text1;
TextView text2;
}
}
spinner_text_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" >
<TextView
android:id="@+id/spinnerText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="16dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/spinnerText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
Solution 2:
ActionBar.setListNavigationCallbacks(...)
It's just a SpinnerAdapter
. If you are familiar with adapters, you can make your own class extending BaseAdapter
and have a custom layout. If you aren't familiar with adapters, I suggest you watch The World of ListView.
Post a Comment for "Android: Add Two Textviews To Actionbar Spinner"