set Selector for Button Programmatically issues

白昼怎懂夜的黑 提交于 2019-12-06 02:29:53

I think that you are experiencing a mutate-related issue (please take a look here, it's extremly useful)

You need to call mutate() on your drawable before assingning it to the View if yout don't want to share the common state across the various instances:

Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.btn);
buttonDrawable.mutate()
btnA.setBackgroundDrawable(buttonDrawable);

In your code you are using the same Drawable for more then one View, so you need to adopt the approach I've described above to avoid the state-sharing.

Dude You have to set the selector From The XML File See below:

<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="@drawable/button_selector"
android:text="@string/button_t"
android:textSize="22sp" />

Here The property android:background="@drawable/you_drawable_selector" is where you have to set The Selector.

Hope I helped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!