Search code examples
androidwidthandroid-actionbarspinnermenuitem

Two Spinner menu items in ActionBar width


I've manually built two spinners in the Action bar, by creating at first two menu items in the main.xml file. With the line

cSpinner.setAdapter( ArrayAdapter.createFromResource( this,
            R.array.category_data,
            android.R.layout.simple_spinner_dropdown_item )
            );

I set the Array Resource for them. These things actually work but the problem is, that the left spinners resource string is so big that just a small bit of the right spinner can be seen.

I've tried things like cSpinner.setLayoutParams(new Spinner.LayoutParams(60, 20)); or '

ViewGroup.LayoutParams params = pView.getLayoutParams();
         params.width = 100;
            cspinner.setLayoutParams(params);

But none of them work. The second one even lets the program crash. Do you now know a tip how I can solve this problem?


Solution

  • for this you have to add custom Layout in ActionBar like Below

    enter image description here

    enter image description here


    code is here

    public class MainActivity extends Activity {
    
        final String[] choices = { "Android", "iOS", "RIM" };
    
        private Spinner Spin1;
        private Spinner Spin2;
    
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    MainActivity.this, android.R.layout.simple_dropdown_item_1line,
                    choices);
            final ActionBar actionBar = getActionBar();
            actionBar.setCustomView(R.layout.actionbar_item);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayUseLogoEnabled(false);
            actionBar.setDisplayShowHomeEnabled(false);
    
            Spin1 = (Spinner) findViewById(R.id.spinner1);
            Spin2 = (Spinner) findViewById(R.id.spinner2);
    
            Spin1.setAdapter(adapter);
            Spin2.setAdapter(adapter);
        }
    
    }
    

    actionbar_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="5" >
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="my App name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />
    
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    
    </LinearLayout>