How to change spinner text Alignment/Gravity in android programmatically

How to set spinner inside text items gravity to left, right, center.

Spinner text alignment setting is some times required into android applications because if we have a larger size spinner with minimum size of 200dp width and items are single word then there are pretty much space remaining on both sizes. So app developer can easily set spinner text gravity using gravity attribute inside textview layout xml. So here is the complete step by step tutorial for How to change spinner text Alignment/Gravity in android programmatically.

android-project-download-code-button

How to change spinner text Alignment/Gravity in android programmatically.

Code for MainActivity.java file.

 package com.setspinnertextalignment_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

Spinner SpinnerWithPadding;
 
 String[] spinnerItems = new String[]{
 "ANDROID",
 "I-PHONE",
 "WINDOWS",
 "MOBILE"
 
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SpinnerWithPadding = (Spinner)findViewById(R.id.spinner1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.spinner_textview_align,spinnerItems );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_textview_align);
 
 SpinnerWithPadding.setAdapter(spinnerArrayAdapter);
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 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"
 tools:context="com.setspinnertextalignment_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />
 
</RelativeLayout>

 

Code for spinner_textview_align.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="200dp"
 android:layout_height="wrap_content"
 android:padding="15dp"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textColor="#838986"
 android:textSize="16dp"

 android:textAlignment="gravity"
 android:gravity="center"
 
 />

Screenshots:

spinner-with-right-align

spinner-with-left-align

How to change spinner text Alignment/Gravity in android programmatically

Click here to download How to change spinner text Alignment/Gravity in android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *