'Custom Button: How to apply TextView color selector programmatically
I have custom button View made from LinearLayout which contains TextView. This custom Button has style defined in styles.xml. I made selector for background which is working fine and also I made android:textColor attribute where is text color selector, which I wanna set for that TextView inside that custom Button.
This is function inside custom button:
private fun setupTextColorStyle(buttonStyle: Int){
val attrs = intArrayOf(android.R.attr.textColor)
val typedAttrs = context.obtainStyledAttributes(buttonStyle, attrs)
val textColor = typedAttrs.getColorStateList(0)
buttonText.setTextColor(textColor)
typedAttrs.recycle()
}
Default color from selector is set successfully, but if I press or disable that button, only background color is changing according to selector. Text inside Button stays same.
Button View:
<merge
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height">
<LinearLayout
android:id="@+id/buttonParent"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="@+id/buttonIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:clickable="true"
android:textSize="@dimen/text_medium"
android:textStyle="bold"
android:layout_gravity="center"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:indeterminate="true"
android:layout_gravity="center"
android:gravity="center"/>
</merge>
Style.xml
<style name="custombutton.secondary">
<item name="android:background">@drawable/buttons_button_secondary</item>
<item name="android:textColor">@color/texts_button_secondary</item>
</style>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
