I am developing an application where navigation is done by using the spinner on the action bar. Spinner items needed to include icons as well so I created custom layouts for that.The problem is that spinner is too wide with respect to its items. I am using ActionBarSherlock. The codes I wrote so far:
navigation_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:padding="4dip"/>
<TextView
android:id="@+id/title"
style="?attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_gravity="center_vertical"/>
</LinearLayout>
navigation_list_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:adjustViewBounds="true"
android:padding="4dip"/>
<TextView
android:id="@+id/title"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true" />
</LinearLayout>
NavigationListAdapter.java:
public class NavigationListAdapter extends BaseAdapter{
private Drawable[] mIcons;
private String[] mTitles;
private Context mContext;
private LayoutInflater mInflator;
public NavigationListAdapter(Context context, Drawable[] icons, String[] titles){
mContext = context;
mInflator = (LayoutInflater)mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mIcons = icons;
mTitles = titles;
}
@Override
public int getCount() {
return mTitles.length;
}
@Override
public Object getItem(int position) {
return mTitles[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflator.inflate(R.layout.navigation_list_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflator.inflate(R.layout.navigation_list_dropdown_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);
return convertView;
}
private class ViewHolder{
public ImageView icon;
public TextView title;
}
}
onCreate method of the activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getSupportActionBar().getThemedContext();
TypedArray ta = getResources().obtainTypedArray(R.array.leagues);
Drawable[] logos = new Drawable[ta.length()];
for(int i = 0; i < logos.length; i++){
logos[i] = ta.getDrawable(i);
}
NavigationListAdapter adapter = new NavigationListAdapter(context, logos,
getResources().getStringArray(R.array.league_names));
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, null);
}
An illustration of the problem:
Maybe changing the width of your textview in navigation_list_dropdown_item.xml to wrap_content take effect (if changing width of LinearLayout does not work). You can see this to: http://dandar3.blogspot.com.es/2013/03/actionbarsherlock-custom-list-navigation.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