Center Align Text - Spinner
Here is the code:
spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="12sp" />
Spinner:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
Java:
array = new ArrayAdapter<String>(Activity.this,
R.layout.spinner, ArrayList);
array .setDropDownViewResource(R.layout.spinner);
Spinner.setAdapter(array );
Spinner.setOnItemSelectedListener(this);
Problem: text is still left aligned
I followed this: http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html and it worked great
I noticed that I had a different spinner xml in the value-v11 Folder and this one was unchanged, I changed it and processed it
Just add the following two lines to your default app theme style:
<item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">
@style/spinnerDropDownItemStyle
</item>
create a new style:
<style name="spinnerItemStyle">
<item name="android:gravity">center</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:gravity">center</item>
</style>
Here it is!
Note that the base theme is used here: Theme.AppCompat.Light and this will be applied as the default stylesheets in your application.
source to share
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ellipsize="marquee"
android:textAlignment="center"
android:textSize="17sp"
tools:ignore="MissingPrefix" />
Use this text representation as the layout passed to your adapter. android:textAlignment="center"
this line is the one who does the magic
source to share
your layout is 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 install this adapter on your meter,
spinner.setAdapter(adapter);
Create an adapter for your meter, for example
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;
}
};
source to share
The best answer and was tested for Mouloud
, but in Marshmallow I had to add textAlignment
to keep the dropdowns centered. So the complete code based code is Mouloud
:
<!-- Spinner style -->
<style name="spinnerItemStyle">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>
<!-- Spinner style drop down style-->
<style name="spinnerDropDownItemStyle">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>
And add style to your theme style:
<!-- Change the spinner style-->
<item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>
source to share
In your layout, add "theme" attribute to Spinner like -
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/_10sdp"
android:theme="@style/CustomSpinnerTheme"
android:entries="@array/entries"
android:gravity="center"
/>
and add style (in values / style file) -
<!-- Spinner style -->
<style name="CustomSpinnerTheme">
<item name="android:textSize">@dimen/_15ssp</item>
<item name="android:textColor">@color/textMainColor</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment">center</item>
</style>
Enjoy :)
source to share
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
label.setGravity(Gravity.CENTER);
return label;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
label.setGravity(Gravity.CENTER);
return label;
}
source to share