android change view's background color when state changes

842 Views Asked by At

how to use color state list for background? I know android:background="@drawable/drawable_selector", but android:background="@color/color_selector" will cause exceptions. but android:background="#FFFFFF" works again, can anyone explains why?

now i want to change a layout's background color(not a drawable) when it's pressed, how to do it?

2

There are 2 best solutions below

0
On

put the color in res/values/colors.xml, like #FFFFFF, and then create a drawable xml in drawable directory, ,that is ok.

0
On

dynamically you can change like this. Use this if it is useful for you - textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));