Why set setBackgroundColor is not working in my custom listView
Asked Answered
E

6

22

I have a custom listView. The main layout xml is something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ListView android:layout_height="wrap_content" 
              android:id="@+id/lv_clientes"
              android:layout_width="0dp">
    </ListView>
<!-- From this part there are not problems -->
</LinearLayout>

The list item XML is this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rlo_elemento"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:id="@+id/tv_nombre"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content">
    </TextView>
    <!-- From this part there are not problems -->
</RelativeLayout>

Now the adapter is like this:

public class AdapterListaClientes extends BaseAdapter
{
    private Cliente[] data;
    Context context;
    LayoutInflater layoutInflater;
    int itemSelected = -1;

    public void setSelected(int valor)
    {
        itemSelected = valor;
    }

    public AdapterListaClientes(Context context, ArrayList<Cliente> data)
    {
        this.data = data.toArray(new Cliente[0]);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }
/*Mandatory things and so...*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
            //All the things that we should put in this point.. I'm using the list14 example
        //HERE IS THE POLEMIC CODE
        if(position == itemSelected)
            convertView.setBackgroundColor(R.color.rojo);
        else
            convertView.setBackgroundColor(R.color.blanco);

        return convertView;

} }

The setBackgroundColor() method is not working. I know that is doing something because when I use this method the background color of the pressed item changes to a opaque version of the default color when a listview item is pressed.

This problem happen only with the background color, I can change everything else without problems...

Thanks!

Elemi answered 9/11, 2011 at 19:8 Comment(0)
K
76

Use

setBackgroundResource(R.color.rojo);

R.color.rojo is a resource, it is not color..

Kioto answered 9/11, 2011 at 19:12 Comment(3)
Woah! it works!... Why they use setBackgroundResource() instead of setBackgroundColor() ...? It almost against common sense...Elemi
I had to use .setImageResource(R.color.color_placeholder); otherwise it wouldn't update the image that's on there previously.Molt
bro! you're a lifesaver. I don't know why setBackground and setBackgroundColor is not workingGrecism
A
4

You could also use setBackgroundColor() but you'll need to understand that is expects an object not a resource id. So you'd have to convert the resource to a color object, like so:

setBackgroundColor(getResources().getColor(R.color.rojo));
Arliearliene answered 14/1, 2014 at 12:48 Comment(0)
S
2

To set color by setBackgroundColor method do this:-

setBackgroundColor(Color.parseColor("#e7eecc"));

either by

setBackgroundResource(R.color.<Your-Color>)
Speedometer answered 21/4, 2016 at 10:15 Comment(0)
T
1
.setBackgroundColor(getResources().getColor(R.color.raj));
Tart answered 16/2, 2019 at 4:18 Comment(0)
D
0
@RemotableViewMethod
public void setBackgroundColor(@ColorInt int color) {
    if (mBackground instanceof ColorDrawable) {
    ((ColorDrawable) mBackground.mutate()).setColor(color);
    computeOpaqueFlags();
    mBackgroundResource = 0;
    }else {
    setBackground(new ColorDrawable(color));
    }
}

Its declaration is like this.

Daiseydaisi answered 3/1, 2021 at 7:49 Comment(0)
B
0

When calling setBackgroundColor on a view you need to set the alpha value to a non-zero value (e.g. 0xFF), otherwise the color will not show up.

TextView tv = (TextView)findViewById(R.id.myTextview);
int rgb = 0xF05922; // Orange
tv.setBackgroundColor(0xFF000000|rgb); // Use bitwise OR to add alpha to RGB value
Bowshot answered 21/1, 2023 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.