Question

I have a horizontal line created with a view like this:

<View
            android:layout_width="fill_parent"
            android:id="@+id/led_connection"
            android:layout_height="5dip"
            android:background="#fff"
            android:layout_marginBottom="15dp" />

I want to know how can I change the color programatically. Because I am trying to use setBackground and setBackgroundDrawable but SDK sais me that it cannot applied to a View.

I'm getting the view with this:

View led_connection = (View)v.findViewById(R.id.led_connection);
Was it helpful?

Solution

Try:

led_connection.setBackgroundColor(0xFF00FF00);

See docs for details

OTHER TIPS

If you have html colors, can try this to solve your problem.

led_connection.setBackgroundColor(Color.parseColor("#679456"));
led_connection.setBackgroundColor(Color.parseColor("html_code_colors"));

As Marcin Orlowski sais, the solution is use setBackgroundColor with an hex color with the 2 first digits that are alpha level.

I'm using for GREEN:

led_connection.setBackgroundColor(0xFF008000);  // GREEN

and for RED:

led_connection.setBackgroundColor(0xFFFF0000);  // RED

Thank you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top