How to add padding to spinner text in android programmatically

How to set android:paddingLeft,
android:paddingTop,
android:paddingRight,
android:paddingBottom on spinner text.

Padding attribute is used to set automatically text margin inside a view, so text will automatically adjust itself by getting space. So we can also set padding on spinner text by modifying its textview_with_padding.xml layout file. So here is the complete step by step tutorial for How to add padding to spinner text in android programmatically.

android-project-download-code-button

How to add padding to spinner text in android programmatically.

Code for MainActivity.java file.

 package com.addpaddingspinnertext_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[]{
 "APPLE",
 "MANGO",
 "BANANA",
 "PAPAYA",
 "WATER MELON",
 
 };
 
 @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.textview_with_padding,spinnerItems );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_padding);
 
 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.addpaddingspinnertext_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 textview_with_padding.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="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textColor="#000301"
 android:paddingLeft="10dp"
 android:paddingTop="12dp"
 android:paddingRight="10dp"
 android:paddingBottom="12dp"
 android:textSize="25dp" />

Screenshot :

How to add padding to spinner text in android programmatically

Click here to download How to add padding to spinner text in android programmatically project.

Leave a Reply

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