Search code examples
androidtextviewbackground-color

Android: Dynamically Change TextView Background color


I have following textview in my Activity. I want to change the background color of the textview dynamically.

My problem is I don't want to get the colors from Resouce file or other colors.RED method. I'm getting colors from webservie in websafe mode (i.e #FFF, #000 etc).

How can I pass these colors as background to TextView. Thanks in advance for your time.

<TextView
                android:id="@+id/colorCode"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" android:background="#FF0000" android:layout_marginRight="5dp"/>

Solution

  • Below is snippet might help you where txtChannelName is an object of TextView

     txtChannelName.setBackgroundColor(Color.RED);
    

    or

    txtChannelName.setBackgroundColor(Color.parseColor("#ffffff"));