Search code examples
androidandroid-actionbarandroid-spinner

ActionBar SpinnerAdapter Large Header followed by selection (spinner)


I'm trying to implement a spinner In the action bar that has brand Name above it. With the ActionBar setListNavigationCallbacks method if possible

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mSpinnerAdapter, null);

android

Can anyone give me an Idea of how to do this?

I would put some code here but I have no idea where to begin as I have not managed to find relevant information yet.

Edit: Using V4.0


Solution

  • Well I've manage to do It myself and am posting the answers just in-case someone wants the answer.

    If anyone wants to point out a more elegant way i'd love to hear it. Won't except my own answer for a couple of days in-case someone can come up with better.

    //In OnCreate method on my activity I've got
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle("");
            ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);      
            final SpinnerAdapter madap = new EntArrayAdapter(this,R.layout.ent_ddl_actionbar, new ArrayList<EntArrayAdapter.Data>(),allents);
            //ActionBar.OnNavigationListener nav will normaly be in here instead of null
            actionBar.setListNavigationCallbacks(madap, null);
    }
    

    My EntAdapter looks like this:

    public class EntArrayAdapter extends ArrayAdapter<com.SimpleEID.widget.EntArrayAdapter.Data> {
    private Context _context;
    private List<Data> _objects;
    
    public class Data implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1
    
        public Data( Ent e) {           
            this.Text = e.getName();
            this.DDText = e.getName();
            this.curEnt = e;
        }
        public Data()
        {
            this.DDText = "No Avalible Ent";
            this.curEnt = null;
        }
    
    
        public String Text;
        public String DDText;
        public Integer ID;
        public Ent curEnt;
    }
    
    private int textSize=14;
    private int ddLSize=20;
    
    public EntArrayAdapter(final Context context,final int textViewResourceId, List<Data> objects,ArrayList<Ent> entList) {
        super(context, textViewResourceId, objects);  
    
        this._context = context;
        this._objects = objects;                
    
        for(int i = 0; i< entList.size() ;i++)
        {           
            Ent e = entList.get(i);
            this.add(new Data(e));
        }
                //Empty case just adding blank
        if(entList.size() == 0)
        {
            this.add(new Data());
        }
    }
    
    //This part is the Dropdown part of the view.So in here I use layout simple spinner
    //As this only needs to show a word
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
    
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(_context);
            convertView = inflater.inflate(
                    android.R.layout.simple_spinner_item, parent, false);
        }
    
        String dropdownText = _objects.get(position).DDText;
    
        TextView tv = (TextView) convertView
                .findViewById(android.R.id.text1);
        tv.setText(dropdownText);
    
                // extra settings to my text on dropdown
        tv.setHeight(100);
        tv.setPadding(40, 20, 0,0);
        tv.setTextSize(ddLSize);    
        return convertView;
    }
        // This getView is the view that shows before you click to change into a dropdown
        // Also the part that shows at the top in the image case it is "BRAND NAME Ent1>"
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(_context);
                        //Chosen my own layout for this part which is basically 2 textViews inside of a LinearLayout
            convertView = inflater.inflate(
                    R.layout.ent_ddl_actionbar, parent, false);
        }
    
    
        TextView tv = (TextView) convertView
                .findViewById(R.id.text_spinner_1);
        tv.setText(_objects.get(position).Text);
        tv.setTextColor(Color.BLUE);
        tv.setTextSize(textSize);// Controls the Non List size
        return convertView;
    }
    }
    

    my Layout

    //R.layout.ent_ddl_actionbar is:
    <?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:orientation="vertical">
    
    <TextView 
        android:id="@+id/bar_title1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/default_brandName"
        android:textSize="16sp"/>
    
    <TextView
    android:id="@+id/text_spinner_1"
    android:layout_height="fill_parent"
    android:layout_width="100dip"
    android:text="test"
    />
    
    </LinearLayout>
    

    Hope this helps