3

In my application I've got 3 spinners - one on the top, which when used provides the user with 2 more aligned below it.

I looked at this question where I found the answer I was looking for, but only half-way.. Now the two supplementary spinners have their text shown in the center, but the first one - no.

Looking at the .xml for the spinners I can see only one difference regarding to centering and it is that the two supplementary ones have the android:layout_gravity="center" attribute, which for some reason is not applicable to the first spinner. Why is this so? Could this be the reason? By not applicable I mean it is not in the properties for the spinner in the design view and when I add it manually in the .xml nothing changes.

So ideally the answer which I am looking for is why the 2 supplementary spinners have their text centered, but the first one - no? Note I am not interested in centering the elements in the popup, this is purely optional. I just want to have the selected option to be in the center.

Here is a screenshot of how it looks now with the problem in red:

problem

Here is the .xml I am using:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/some_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="packagename"
tools:showIn="@layout/specific_activity_layout"
android:gravity="center_horizontal"
android:background="#FF9800">

<Spinner
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/spinner1"
    android:entries="@array/spinner1entries"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp"
    android:gravity="center|top"
    android:background="@drawable/rounded_corners"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:popupBackground="#FFB74D"
    android:layout_alignParentEnd="false"
    android:textAlignment="center" />

<LinearLayout
    android:id="@+id/horizontal_layout_spinners"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="false"
    android:layout_alignParentStart="false"
    android:visibility="invisible"
    android:layout_below="@+id/spinner1">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:id="@+id/spinner2"
        android:visibility="visible"
        android:background="@drawable/rounded_corners"
        android:layout_marginRight="5dp"
        android:popupBackground="#FFB74D"
        android:textAlignment="center"
        android:gravity="center|top"
        android:layout_weight="0.5"
        android:layout_gravity="center" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:id="@+id/spinner3"
        android:visibility="visible"
        android:layout_marginLeft="5dp"
        android:background="@drawable/rounded_corners"
        android:popupBackground="#FFB74D"
        android:textAlignment="center"
        android:gravity="center|top"
        android:layout_weight="0.5"
        android:layout_gravity="center" />

</LinearLayout>
</RelativeLayout>

And the rounded_corners file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFB74D"/>
    <stroke android:width="0dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
Community
  • 1
  • 1
Phantomazi
  • 408
  • 6
  • 22

5 Answers5

2
  • Try adding : android:dropDownWidth="wrap_content" to the spinner.

  • You can try to set the text style programatically after an item is selected from the spinner. Set an setOnItemSelectedListener and inside it set TextView to be centred.

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
        @Override
        public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
            // Set the text style after item is selected.
            setSpinnerSelectedItemParams(spinner1, getActivity());
    }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    
    
    
    /**
     * Sets the spinner text style to a different one after one value has been chosen.
     * @param spinner Spinner currently in use
     */
    public void setSpinnerSelectedItemParams(Spinner spinner, Context context) {
        if (spinner.getChildCount() > 0){
            TextView tvSpinner = ((TextView) spinner.getChildAt(0));
            tvSpinner.setPadding(0, 0, 0, 0);
            tvSpinner.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            params.setMargins(0, 0, 0, 0);
            tvSpinner.setLayoutParams(params);
        }
    }
    

    If you want the text before selecting it in centre as well, try setting a custom resource when creating the adapter.

    In Activity or Fragment:

    ArrayAdapter<String> yourAdapter = new ArrayAdapter(activity, R.layout.default_spinner_adapter_view, yourDataToFillTheSpinner);
    

    "default_spinner_adapter_view.xml" file:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- This is the view for each individual item of the spinner-->
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv_default_spinner"
        style="?android:attr/spinnerItemStyle"
        android:layout_width="match_parent
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="marquee"
        android:gravity="center"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />
    
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • What about the default item? This way it will be inconsistent - if it works it will display it in the centre only after user selection, but the default item will still be stuck to the left. – Phantomazi Apr 19 '16 at 20:40
  • @Phantomazi The way I get around doing it is by setting a custom resource when creating the `ArrayAdapter`. Something like: `ArrayAdapter adapter = new ArrayAdapter(activity, R.layout.spinner_adapter_picker_view_center, yourData);`. Let me try to include it in answer. – Shobhit Puri Apr 19 '16 at 20:44
0

If you want to customize the dropdown list items you will need to create a new layout file. Let’s call it spinner_dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

Another change in the declaration of the spinner:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
Nirmal Shethwala
  • 268
  • 1
  • 15
0

Make your custom list item of spinner and set as you want and that should have a TextView id text1 and use this list_item.xml like below

Spinner spinner = (Spinner) findViewById(R.id.spinner);

// Create an ArrayAdapter using the string array and a default spinner layout

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.your_list_item);

// Specify the layout to use when the list of choices appears

adapter.setDropDownViewResource(android.R.layout.your_drop_down_list_item);

// Apply the adapter to the spinner

 spinner.setAdapter(adapter);
Shahab Rauf
  • 3,651
  • 29
  • 38
0

change relative layout to linear layout vertical and width of the spinner should be match_parent.

0

Make sure all spinners are using the same type of adapter—or at least another adapter using the same item layout—the adapter provides the layout.

totoro
  • 2,469
  • 2
  • 19
  • 23