Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CardView height programmatically

Tags:

android

I am new to Android dev and I'm having trouble trying to set the min height of a it.gmariotti.cardslib.library.view.CardView programmatically. I create a new instance of a CardView and set all the xml elements in code, but it does not affect the cardView. I do not inflate the cardview from xml.

CardView catCard = new CardView(getActivity());

    catCard.setBackgroundColor(Color.green(0));

    catCard.setMinimumHeight(10);
    catCard.setBottom(0);
    catCard.setExpanded(false);
like image 361
Mark Harper Avatar asked Feb 27 '15 08:02

Mark Harper


People also ask

How to set height of CardView programmatically in android?

setMinimumHeight(10); catCard. setBottom(0); catCard. setExpanded(false); android.


1 Answers

CardView extends FrameLayout so you should be able to set LayoutParams. Try something like this :

 CardView.LayoutParams layoutParams = (CardView.LayoutParams)
            catCard.getLayoutParams();
    layoutParams.height = 10;

, dont forget that setting Width is also required. Or create new LayoutParams like this (not tested) :

CardView catCard = new CardView(getApplicationContext());
// sets width to wrap content and height to 10 dp ->
catCard.setLayoutParams(new CardView.LayoutParams(
     CardView.LayoutParams.WRAP_CONTENT, 10));

catCard.setMinimumHeight(10);
like image 170
A Honey Bustard Avatar answered Sep 26 '22 17:09

A Honey Bustard