Development issue/problem:
In a nutshell:
How do I determine the color states of the CardView CardBackgroundColor property (here in the ListView layout)?
(I use RC1 from Android L developer preview, installed on a phone running under 4.4, and com.android.support: cardview-v7: 21.0.0-rc1 in build.gradle).
Longer:
In the CardView layout we define the corner radius and the background color of CardView using CardCornerRadius and CardBackgroundColor.
However, the background colour does not display the selected reports, i.e. if, for example, B. clicks on an item in the list.
If you set the background color and the corresponding states observed in the internal CardView, it will be displayed above the corners you set in the CardView.
So how can we ensure that the BackgroundColor card statuses are respected by CardView?
Here is the color used for theeBackgroundColor folder, color_with_states.xml :
And a layout that uses CardView :
How can I solve this problem?
Solution 1:
This is not ideal because the edges are not rounded, but it allows you to add tactile feedback to CardView:
// Nestled view …
Android: foreground and android: clickable attributes added to CardView.
It also has the negative side effect that the android:clickable attribute replaces a clickListeners, so these clickListeners are not activated.
Update
I have several examples of how to implement CardView.
Walk (https://github.com/lawloretienne/Loop) –
https://github.com/lawloretienne/Loop/blob/master/app/src/main/res/layout/category_card.xml
QuickReturn (https://github.com/lawloretienne/QuickReturn) – https://github.com/lawloretienne/QuickReturn/blob/master/sample/src/main/res/layout/activity_quick_return.xml
Update 2
After further research I found a good solution for CardViews on all versions of the API, including Lollipop previews.
@etiennelawlor/lay-out-tips-for-pre- and post-lollipop-bcb2e4cdd6b2#.9h0v1gmaw
Solution 2:
Sometimes you need visual tactile feedback from CardView. The android:foreground=?android:attr/selectableItemBackground solution is perfect for this.
However, you may want to consider using drawSelectorOnTop(true) with your ListView. This does not require any changes to your CardView.
Let me know if you need any more explanations.
Solution 3:
This is my way of solving your problem.
First create a custom class called CustomCardView expands CardView
Then we overwrite the drawableStateChanged() method, change the background color of the card by calling the setCardBackgroundColor() method when the card clicks on state.
Finally, replace CardView with this CustomCardView in your layout file.
The only drawback of this solution is that the map view on Android 5.0 and higher cannot show the ripple effect when typing.
Here’s my code:
The CustomCardView public class expands CardView {
public CustomCardView(Context) {
super(context);
// TODO Automatically generated design heel
}
public CustomCardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor tube
}
public CustomCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO auto-generation constructor stub
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (isPressed()) {
this.setCardBackgroundColor(getContext())).getResources().getColor(R.color.card_view_pressed)));
} otherwise {
this.setCardBackgroundColor(getContext())).getResources().getColor(R.color.card_view_normal));
}
}
}
Solution 4:
One solution I used was to customize the programmatic user interface by replacing the View.OnTouchListener OnTouch() event handler in my custom ViewHolder.
@Override
public boolean onTouch (View v, event MotionEvent)
{
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN)
{
mCardView.setCardBackgroundColor(STATE_PRESSED_COLOR);
}
if (action == MotionEvent.ACTION_UP | action == MotionEvent.ACTION_CANCEL)
{
mCardView.setCardBackgroundColor(DEFAULT_COLOR);
}
returns falsely;
}
Solution No 5:
**simply add lines in the map view**.
android:clickable=true
android:focusableInTouchMode=true
android:foreground=?android:attr/selectableItemBackground
Solution No 6:
You can use the color status in the code and it works well.
cardView.setCardBackgroundColor(context.getColorStateList(R.color.card_view_selectable)))
in color > card_view_selectable.xml
Then use isActivated because the background color has changed to state_activated or default.
cardView.isActivated = true // or false
Solution No 7:
I used a rectangular shape with the same angular radius as the map view. And then used the xml drawing as background for the internal map view.
The background does not appear above the corner of the map, although I always get some padding between the map and the inside view.
Solution No 8:
If you look at the definition of the carBackgroundColor property, at least the Android support library has it :
It says CardBackgroundValue only needs one colour. I think this means that the selector is ignored, but it reaches the default value, which is the color at the bottom of the selector.
Solution No 9:
Use android: foreground instead of android: background in your . example CardView code below.
// other navigation elements
Good luck!
Related Tags:
checkable cardview android,cardview colorstatelist,custom cardview android,cardview transparent background,android style card view,android-cardview selectableitembackground,cardbackgroundcolor not working,card view background color not working,how to change cardview elevation color in android,change cardview background color programmatically,change cardview background programmatically,android set cardview background color programmatically,card view background is black,card view background color programmatically,card background color not working,cardview corner background not transparent