Does Android TextView support state_pressed state?

112 Views Asked by At

The real quest is to have a custom ButtonBar(as below) in Android with TextViews acts as buttons and each should reflect their state, pressed/selected.

enter image description here

The TextView is set with the following background for different states to reflect. What all states supported by Android TextView? Say e.g., if it supports states like pressed/selected, how to update the TextView from XML/Code?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#ffffff">
        <shape android:shape="rectangle">
            <corners android:radius="16.5dp" />
            <size android:width="168dp" android:height="34dp" />
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:color="#000000" >
        <shape android:shape="rectangle" >
            <corners android:radius="16.5dp" />
            <size android:width="168dp" android:height="34dp" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</selector>

TextView code snippet below for reference

        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:background="@drawable/tv_background"
            android:gravity="center"
            android:maxLines="1"
            android:onClick="@{() -> viewModel.onTvClicked()}"
            android:text="@string/TVTEXT"
            android:textSize="16sp"
            app:layout_constraintBaseline_toBaselineOf="@+id/something"
            app:layout_constraintEnd_toStartOf="@+id/otherthing"
            app:layout_constraintStart_toStartOf="parent" />

Thanks for your time :)

0

There are 0 best solutions below