0

I need to create CardViews to a RelativeLayout. Inside the CardViews, there are other Views, like ImageViews, and TextViews. They are added, but seems they are not respecting the rules I am adding to the LayoutParams. This is my code:

CardView infoCardView=new CardView(HotelActivity.this);
        infoCardView.setId(2);
        RelativeLayout.LayoutParams cardViewParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams iconLp=new RelativeLayout.LayoutParams(20,20);
        RelativeLayout.LayoutParams textLP=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //imagen is another ImageView that is being drawn ok in the RelativeLayout. Also, the cardView is drawn below the ImageView, which is ok.
        cardViewParams.addRule(RelativeLayout.BELOW,imagen.getId());
        infoCardView.setLayoutParams(cardViewParams);
        iconLp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        iconLp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
        iconLp.setMarginStart(20);
        //Icono del cardView
        ImageView icono=new ImageView(HotelActivity.this);
        icono.setId(3);
        //icono.setLayoutParams(iconLp);
        icono.setBackground(getDrawable(android.R.drawable.ic_dialog_info));
        infoCardView.addView(icono,iconLp);
        //TextView título del CardView
        TextView tvInfo=new TextView(HotelActivity.this);
        tvInfo.setId(4);
        tvInfo.setTextSize(17);
        tvInfo.setText(getString(R.string.info));
        tvInfo.setTypeface(tvInfo.getTypeface(), Typeface.BOLD);
        textLP.addRule(RelativeLayout.END_OF,icono.getId());
        textLP.setMarginStart(20);
        tvInfo.setTextColor(getColor(R.color.white));
        //tvInfo is NOT added to the END of icono, instead is overlapping it.
        infoCardView.addView(tvInfo, textLP);
         infoCardView.setRadius(9);
        infoCardView.setBackgroundColor(Color.parseColor(almacen.getEvento().getBgColor()));
        infoCardView.setElevation(9);
        rl.addView(infoCardView,cardViewParams);

What I am doing wrong?

Thanks.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

1 Answers1

0

Solved. The problem was, I was setting some rules to a CardView that are not available for it, like CENTER_VERTICAL. So, I added another RelativeLayout as a son of the CardView, add the Views to that RelativeLayout, add that RelativeLayout to the CardView, and now the Views are drawn correctly.

Fustigador
  • 6,339
  • 12
  • 59
  • 115