CardView Screen Height Setting

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

 CardView catCard = new CardView(getActivity()); catCard.setBackgroundColor(Color.green(0)); catCard.setMinimumHeight(10); catCard.setBottom(0); catCard.setExpanded(false); 
+8
android
source share
2 answers
 myCardView.setLayoutParams(new RelativeLayout.LayoutParams(20, 20)); 

it depends on the fact that your type of map is the child from whom the layout is made, in which case it is located in the Relative Layout section

+5
source share

CardView extends FrameLayout, so you must install LayoutParams. Try something like this:

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

Do not forget that setting the 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); 
+4
source share

All Articles