Why set setBackgroundColor is not working in my custom listView

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

  • Do users have to grant the permission com.android.vending.BILLING on upgrade/install?
  • What are the aspect ratios for all Android phone and tablet devices?
  • Android TimePickerDialog material design color
  • What's the difference between @android: and ?android:
  • Set app background to be the same as home screen wallpaper
  • Latest C++11 features with Android NDK
  • <?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!

    Related posts:

    Android play services 6.5: LocationClient is missing
    How to make XML strings bold, underlined etc?
    Jumping markers on Android Maps API v2
    How to store(bitmap image) and retrieve image from sqlite database in android?
    Can we instantiate an abstract class?
    Double handled slider android
  • RxJava Async task in Android
  • Unbelievably high rate of “customer's card was declined” on subscription API
  • Could not find com.google.android.gms:play-services:7.0.0
  • transformClassesAndResourcesWithProguardForRelease FAILED
  • Android twitter tweet with image
  • Android, gestures over clickable widgets
  • 3 Solutions collect form web for “Why set setBackgroundColor is not working in my custom listView”

    Use

    setBackgroundResource(R.color.rojo);
    

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

    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));
    

    To set color by setBackgroundColor method do this:-

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

    either by

    setBackgroundResource(R.color.<Your-Color>)
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.