android kotlin - Spinner text align center right programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private lateinit var context:MainActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the context
        context = this

        // Get the widgets reference from XML layout
        val spinner = findViewById<Spinner>(R.id.spinner)

        // list of spinner items
        val list = mutableListOf(
            "Amber",
            "Atomic tangerine",
            "Air superiority blue",
            "Black",
            "Barbie Pink",
            "Almond"
        )

        // initialize an array adapter for spinner
        val adapter:ArrayAdapter<String> = object: ArrayAdapter<String>(
            context,
            android.R.layout.simple_spinner_dropdown_item,
            list
        ){
            override fun getDropDownView(
                position: Int,
                convertView: View?,
                parent: ViewGroup
            ): View {
                val view:TextView = super.getDropDownView(
                    position,
                    convertView,
                    parent
                ) as TextView

                // set item text bold and sans serif font
                view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD)

                // set item padding
                view.setPaddingRelative(48,0,48,0)

                // spinner item text color
                view.setTextColor(Color.parseColor("#2E2D88"))

                // set selected item style
                if (position == spinner.selectedItemPosition){
                    view.background = ColorDrawable(
                        Color.parseColor("#F5F5F5"))
                }


                // spinner item text alignment center
                view.textAlignment = View.TEXT_ALIGNMENT_CENTER

                // uncomment any line to test another alignment
                // spinner item text alignment right end
                //view.textAlignment = View.TEXT_ALIGNMENT_TEXT_END

                // spinner item text alignment left start
                //view.textAlignment = View.TEXT_ALIGNMENT_TEXT_START

                // spinner item text alignment as gravity
                //view.gravity = Gravity.CENTER
                //view.textAlignment = View.TEXT_ALIGNMENT_GRAVITY

                return view
            }
        }

        // finally, data bind spinner with adapter
        spinner.adapter = adapter
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>