29

I need resize this part size to full display. How can i do this?

Example image

My adapter:

String[] navigations = getResources().getStringArray(R.array.actionBar);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.custom_spinner_title_bar,
                android.R.id.text1, navigations);
        adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setListNavigationCallbacks(adapter, navigationListener);

custom_spinner_title_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dip"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

</RelativeLayout>
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
WOLVERINE
  • 769
  • 3
  • 12
  • 28
  • try having your spinner the only view in its horizontal parent layout. I mean try removing the red button next to it to see if it helps – arianoo Aug 14 '13 at 15:54

2 Answers2

98

Add Attribute in xml file in Spinner tag

android:dropDownWidth="150dp"
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Umar Nafeez
  • 1,011
  • 7
  • 3
  • 5
    works for default spinnermode, but how to make it work with android:spinnerMode="dialog"? – live-love Jul 12 '18 at 18:20
  • While we're at it maybe someone knows how to set the drop down height? Maybe one can set a limit to how many items are displayed I suppose. – matthias_b_nz Sep 25 '20 at 12:18
6

What you need to do is use a custom adapter for the drop-down instead of the default one, where you set the "minimumWidth" of each 'row' to whatever you want:

private class myCustomAdapter extends ArrayAdapter{
    private List<String> _navigations;
    private int _resource;
    private int _textViewResourceId;

    public myCustomAdapter (Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        _navigations = objects;
        _resource = resrouce;
        _textViewResourceId = textViewResourceId;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent){
        View row;
        LayoutInflater inflater=getLayoutInflater();            
        row = inflater.inflate(_resource, null);
        TextView _textView = row.findViewById(_textViewResourceId);
        _textView.setText(_navigations.get(position));


        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int _width = size.x;

        row.setMinimumWidth = _width;
        return row;
    }
}

You can of course play with "minimumWidth" to be whatever else you want; In this example it's just set to match the screen width (though a smarter approach would be to measure your app's container frame and match it to that).

Then to set the adapter:

myCustomAdapter adapter = new myCustomAdapter(getBaseContext(), R.layout.custom_spinner_title_bar,android.R.id.text1, navigations);   
Elad Avron
  • 1,411
  • 1
  • 18
  • 28
  • 1
    thank you, but now i can't accept your answer, because I refused to implement this – WOLVERINE Nov 14 '13 at 11:03
  • 3
    You COULD in theory target the spinner itself and usr setDropDownWidth, but that would still require a custom adapter and will only work on Jellybean and above. – Elad Avron Nov 14 '13 at 14:47