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

Community
  • 1
  • 1
Abhishek Patel
  • 587
  • 2
  • 8
  • 25
  • 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. – Bö macht Blau Aug 27 '18 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? – Abhishek Patel Aug 27 '18 at 17:21
  • Maybe [this post](https://stackoverflow.com/questions/17965784/android-set-layout-gravity-programmatically-for-linearlayout) is helpful – Bö macht Blau Aug 27 '18 at 17:27
  • Yes, that worked! thanks! – Abhishek Patel Aug 27 '18 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 – Abhishek Patel Aug 27 '18 at 17:33

1 Answers1

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;
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61