Open In App

How to Change Spinner Text Style in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

A Spinner in Android is a UI element used to display the list of items in a drop-down menu. The spinner is depicted as a downward arrow on the layout. Each of these items is selectable and can be used as user input. In this article, we will show you how to change the Spinner item text styles in Android.

 Change Spinner Text Style in Android

More about Spinner in Android can be found at Spinner in Android with an Example. Follow the below steps once the IDE is ready.

Step-by-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML files

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Spinner as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">
  
    <Spinner
        android:id="@+id/spinner_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
</RelativeLayout>


Step 3: Create a layout for the spinner (spinner_list.xml)

Navigate to the app > res > layout > and create a layout file. Name it spinner_list.xml and add a TextView with text attributes as shown below.

XML




<TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="@android:color/holo_red_light"
    android:textSize="16sp"
    android:textStyle="bold|italic"/>


Step 4: Working with the MainActivity file

Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing the Spinner from the layout file
        val mSpinner = findViewById<Spinner>(R.id.spinner_1)
  
        // Create a list to display in the Spinner
        val mList = arrayOf<String?>("Delhi", "Mumbai", "Chennai", "Kolkata", "Bengaluru")
  
        // Create an adapter as shown below
        val mArrayAdapter = ArrayAdapter<Any?>(this, R.layout.spinner_list, mList)
        mArrayAdapter.setDropDownViewResource(R.layout.spinner_list)
  
        // Set the adapter to the Spinner
        mSpinner.adapter = mArrayAdapter
  
    }
}


Java




import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.Arrays;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Declaring and initializing the Spinner from the layout file
        Spinner mSpinner = findViewById(R.id.spinner_1);
  
        // Create a list to display in the Spinner
        List<String> mList = Arrays.asList("Delhi", "Mumbai", "Chennai", "Kolkata", "Bengaluru");
  
        // Create an adapter as shown below
        ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, R.layout.spinner_list, mList);
        mArrayAdapter.setDropDownViewResource(R.layout.spinner_list);
  
        // Set the adapter to the Spinner
        mSpinner.adapter = mArrayAdapter;
    }
}


Output:

Output



Last Updated : 09 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads