1
public void makeCard( Context context, View view, String time, String startpt1, String startpt2, String endpt1, String endpt2)
{
    CardView card = new CardView(context);
    LinearLayout ll = view.findViewById(R.id.rides);
    // Set the CardView layoutParams
    int w = getResources().getDimensionPixelSize(R.dimen._270sdp);
    int h = getResources().getDimensionPixelSize(R.dimen._120sdp);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);
    card.setForegroundGravity(17);
    card.setLayoutParams(params);


    card.setRadius(60);
    card.setCardElevation(60);

    Typeface latoregular = getResources().getFont(R.font.latoregular);
    Typeface latobold = getResources().getFont(R.font.latobold);
    // Initialize a new TextView to put in CardView

    TextView tv = new TextView(context);
    tv.setLayoutParams(params);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    try {
        Date date = format.parse(time);
        tv.setText(date.toString());
        Log.d("ASD", String.valueOf(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    tv.setTextSize(14);
    tv.setPadding(60,40,0,0);
    tv.setTextColor(Color.parseColor("#1fb286"));
    tv.setTypeface(latobold);
    tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
    card.addView(tv);
    ll.addView(card);
}

This cardview hugs the side, and no matter what I change about the layout gravity, the card does not move. I can set the padding on the left to make it seem centered, but on different sized screens it would not be perfectly centered

5
  • 1
    You're creating RelativeLayout.LayoutParams and using them for a LinearLayout and a CardView (which is a kind of FrameLayout). This is not likely to work well. As far as I know, one should use e.g. LinearLayout.LayoutParams for a View which you want to add to a LinearLayout. Aug 27, 2018 at 17:07
  • Yes, that was my mistake. I changed it to LinearLayout.LayoutParams, but then there is not class function called addRule (it is only for RelativeLayout) so what would I use to center it? Aug 27, 2018 at 17:21
  • Maybe this post is helpful Aug 27, 2018 at 17:27
  • Yes, that worked! thanks! Aug 27, 2018 at 17:32
  • Do you want to add it as an answer? LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h); params.gravity = Gravity.CENTER; is what fixed it Aug 27, 2018 at 17:33

1 Answer 1

1

You're creating RelativeLayout.LayoutParams and using them for a LinearLayout and a CardView (which is a kind of FrameLayout). This is not likely to work well. As far as I know, one should use e.g. LinearLayout.LayoutParams for a View which you want to add to a LinearLayout (Rule of thumb: use LayoutParams named after parent ViewGroup)

In order to have the CardView centered, you have to set its android:layout_gravity attribute programmatically:

int w = getResources().getDimensionPixelSize(R.dimen._270sdp);
int h = getResources().getDimensionPixelSize(R.dimen._120sdp);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);   
params.gravity = Gravity.CENTER;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.