I am trying to change the CardView color programmatically.
This is my CardView:
<android.support.v7.widget.CardView
android:id="@+id/createsub_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
and this is how I set the background color:
CardView card = (CardView) findViewById(R.id.createsub_card);
card.setCardBackgroundColor(sub.getColor());
where sub.getColor()
returns in this specific case this color:
<color name="color_black">#000000</color>
which should be pitch black. Still my CardView looks like this:
any idea on why this happens and how to fix it?
I assume problem arising from sub.getColor() . At first properly return Color code .
You can try with this
cardView.setCardBackgroundColor(Color.parseColor("#000000"));
Or
cardView.setCardBackgroundColor(getResources().getColor(R.color.color_black));
The problem is sub.getColor() , you return color id(R.color.color_black) not color code. see my code below
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/createsub_card"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="10dp">
</android.support.v7.widget.CardView>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Click"
android:layout_below="@+id/createsub_card"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp" />
</RelativeLayout>
SampleActivity.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;
/**
* Created by Magesh on 5/4/2017.
*/
public class SampleActivity extends AppCompatActivity implements View.OnClickListener
{
private CardView mCardView;
private Button mBtnClick;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
mCardView = (CardView) findViewById(R.id.createsub_card);
mBtnClick = (Button) findViewById(R.id.button);
mBtnClick.setOnClickListener(this);
// mCardView.setCardBackgroundColor(getResources().getColorWrongWay(R.color.color_black));
setColor(R.color.color_black);//hex color code id #000000
mCardView.setCardBackgroundColor(getColorWrongWay());// you set like this
}
private int mColor = 0;
private void setColor(int color)
{
mColor = color;
}
private int getColorWrongWay()
{
return mColor;
}
private int getColorCrtWay()
{
return getResources().getColor(mColor);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button:
{
mCardView.setCardBackgroundColor(getColorCrtWay());// should be like this.
}
break;
}
}
}
Wrong Way :
private int getColorWrongWay()
{
return mColor;
}
Correct Way :
private int getColorCrtWay()
{
return getResources().getColor(mColor);
}
Output :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.