android kotlin - Chip center text

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.view.View
import com.google.android.material.chip.Chip


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

        // get the widgets reference from XML layout
        val chipYellow = findViewById<Chip>(R.id.chipYellow)

        // set chip text alignment center programmatically
        chipYellow.textAlignment = View.TEXT_ALIGNMENT_CENTER
    }
}
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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="32dp">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!-- chip center text in xml -->
        <com.google.android.material.chip.Chip
            android:id="@+id/chipCrimson"
            android:layout_width="175dp"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="Crimson" />

        <com.google.android.material.chip.Chip
            android:id="@+id/chipYellow"
            android:layout_width="175dp"
            android:layout_height="wrap_content"
            android:text="Yellow" />

    </com.google.android.material.chip.ChipGroup>

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials