Search code examples
androidandroid-cardview

How to set CardView's elevation programmatically?


Hello I just want to set the elevation of cardview programmatically but seems it does not work , here's my Code :

CardView cardView = new CardView(this);
    LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    cardView.setLayoutParams(params);
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear);
    cardView.setCardBackgroundColor(Color.WHITE);
    cardView.setCardElevation(1000);
    linearLayout.addView(cardView);

even though setCardEleveation is set to 1000 there is no changes to cardview, what's wrong ? there is some related question out there but i can't got the answer.


Solution

  • Ok , i had a tricky problem , my Cardview wrapped its Parent up so there is no space available to show its shadow. finally I added margin to the Cardview and now It works !!