'Android: change background color View onClick Button

How I can change the background color to a View this under a button when I click the button? I've tried a selector does not work because the View not change the color. What is the problem?

This is my code:

XML

    ...
          <View
            android:id="@+id/viewPlanos2"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_gravity="center" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@color/transparente"
            android:drawableTop="@drawable/buttonimage"
            android:gravity="center"
            android:paddingTop="50dp" />

        <View
            android:id="@+id/viewPlanos1"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_gravity="center" />
 ...

JAVA

View linea2 = (View)findViewById(R.id.viewPlanos2);
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));

linea_verde

<item android:state_pressed="true"><shape>
        <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" />
    </shape></item>
<item android:state_focused="true"><shape>
        <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" />
    </shape></item>
<item><shape>
        <solid android:color="@color/rojo" />
    </shape></item>

EDIT:

I have tried:

public void onClick(View v) {

    if(v == botonMetro) {

        linea2.setBackgroundResource(R.drawable.linea_verde);
                    and

        linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
    }
}

But the code not work



Solution 1:[1]

You are wrongly using drawable use this one one

 view.setBackgroundResource(R.drawable.linea_verde)

Solution 2:[2]

as you say change the background color to a View this under a button when I click the button

do like this

 button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

                  // change color of your view here  
           linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));

          }
    });

Solution 3:[3]

setBackgroundColor() is for colors only, but it seems your using a state list drawable. If you are sure you want to use a different color depending on the Button's state, set the state list drawable using this code:

view.setBackgroundDrawable(R.drawable.linea_verde);

Otherwise, just set the background color using

view.setBackgroundColor(getResources().getColor(R.drawable.yourcolor);

But in this case, use a click listener, and also make sure to actually use a color resource, e.g.:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="opaque_red">#f00</color>
    <color name="translucent_red">#80ff0000</color>
</resources>

Solution 4:[4]

You can also do like this.

android:background="@drawable/linea_verde"

Solution 5:[5]

If you would like to apply default background of selected item you could use background attribute with android ?attr.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="?attr/selectableItemBackground" />

selectableItemBackground attribute will use proper drawable according to Android version. On pre-lollipop it will use solid background, but on lollipop devices the ripple effect will be used.

Code from: com.android.support/appcompat-v7/23.0.0/res/values/values.xml

<item name="selectableItemBackground">@drawable/abc_item_background_holo_dark</item>

Solution 6:[6]

public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.button) {
        linea2.setBackgroundResource(R.drawable.linea_verde);
    }
}

I think you were comparing the view the wrong way. I just happened to stumble to this question and since it is not marked as answered, this might help others.

Solution 7:[7]

Create two separate files for your shapes and change the background of your view after button click:

private boolean isPressed = false;    

public void onClick(View v) {
    if(v == botonMetro) {
        if(isPressed){
           linea2.setBackgroundResource(R.drawable.pressed_shape);       
        } else{
           linea2.setBackgroundResource(R.drawable.un_pressed_shape);       
        }
        isPressed = !isPressed;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naveen Kumar Kuppan
Solution 2 MilapTank
Solution 3 FD_
Solution 4 Bhavik Kamdar
Solution 5
Solution 6 archvayu
Solution 7 Skullper