Home » Android » set view text align at center in spinner in android

set view text align at center in spinner in android

Posted by: admin April 23, 2020 Leave a comment

Questions:

view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list

I want to align the view text of spinner to center. I google it but didn’t find anything, does anybody knows about this? any help or suggestion are appreciated

How to&Answers:

Create a adapter for your spinner like this,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.my_spinner_style,array_of_values) {

    public View getView(int position, View convertView,ViewGroup parent) {

        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);

        return v;

    }

    public View getDropDownView(int position, View convertView,ViewGroup parent) {

        View v = super.getDropDownView(position, convertView,parent);

        ((TextView) v).setGravity(Gravity.CENTER);

        return v;

    }

};

Now your layout R.layout.my_spinner_style,

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

Now set this adapter to your spinner,

spinner.setAdapter(adapter);

Answer:

You need to set your own layout for spinner item.

SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"A", "B", "C"});
spriner.setAdapter(adap);

Where R.layout.spinner_item is a layout with content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:text="Sample Text" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp"></TextView>

Answer:

This is very simple, just create a xml file named as spinner_item.xml with content as

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:padding="10dp"
android:textAlignment="center"
/>

Now in your java class, inspite of adding android’s default view resource add your custom view resource as

ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, R.layout.spinner_item, myList);
 adapter.setDropDownViewResource(R.layout.spinner_item);

The textAlignment line will do the trick.

Answer:

Setting textAlignment to center will place the text in center.

android:textAlignment=”center”

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_areas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:gravity="center"
            android:textAlignment="center"/>

Answer:

This works without layout changes

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               ((TextView) adapterView.getChildAt(0)).setGravity(Gravity.CENTER);
        }
 });

Answer:

Add this line in Spinner

android:textAlignment="center"

Voila!

Answer:

Late to the game, but I wanted to add this for future users just because I found it a simple solution for my problem. It allowed me to center simple text without creating a custom layout. The way I did this was using padding

<Spinner
    android:id="@+id/example_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" />

I was successful in centering my text within my spinner using this method. This will likely only work for simple cases, but you would probably want to use a custom layout for more complex cases anyway. This is just to keep your xml files down and avoid having to use a custom layout just to center a simple spinner.

Answer:

Let me complete answer. It is also important to make space in Spinner.

Wrong:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Right:

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>