Issue
I want to change a CardView
's elevation and reset it later.
Currently, I keep the default value of elevation (called resting elevation) as a field value in onCreateView
of a Fragment
. Later when I want to reset, set the value with setCardElevation
.
private CardView cardView;
private float restingElevation;
private float pickedUpState = 50.0f;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
cardView = (CardView) view.findViewById(R.id.table_top);
restingElevation = cardView.getCardElevation(); // keep the default
Log.d("elevation", "resting: " + restingElevation);
return view;
}
private void pickUp() {
cardView.setCardElevation(pickedUpState);
}
private void rest() {
cardView.setCardElevation(restingElevation);
}
But, I wonder if there are any direct way to set a CardView
's elevation to its default value (resting elevation) without keeping the value which has not been changed yet in onCreateView
. Is there a resource parameter (or style) for the android.support.v7.cardview:cardElevation
of Material Design theme?
(It seems that the resting elevation of CardView
is about 2.66 with LogCat. Actually, in the official Material Design Guide, the resting elevation of Card is indicated between 2dp and 3dp.)
Solution
I have managed to find the default elevation (resting elevation) of CardView
.
float elevation = getResources().getDimension(R.dimen.cardview_default_elevation);
Log.d("elavation", "this is the default: " + elevation);
That is R.dimen.cardview_default_elevation
.
Additional inspection:
Actually the R.dimen.cardview_default_elevation
is just 2dp, that is described in the official Material Design Guide. While getDimension
returns the value in px which depends on devices. So my device is Nexus7 2012 which has screen density of tvdpi (x1.33 for mdpi) that the returned value is calculated as 2dp x 1.33 = 2.66px
.
Answered By - hata
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.