1

Is there any way of generating weight to textviews for linearlayout in java file.

This is the code im using but its not alligned properly

java file:

LinearLayout afternoonLayout = (LinearLayout) findViewById(R.id.afternoonLayoutId);

    if(afternoonSession !=null && afternoonSession.size() >= 0){

        TextView txtAfternoon = new TextView(this);
        txtAfternoon.setText("Afternoon");
        txtAfternoon.setTextSize(22);
        txtAfternoon.setGravity(Gravity.LEFT);
        txtAfternoon.setTypeface(null, Typeface.BOLD);
        txtAfternoon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        if(afternoonSession.has("Vitals")){

            txtAfternoonVitals = new TextView(this);
            txtAfternoonVitals.setText("Vitals " + "- " + "                      " + afternoonSession.get("Vitals").toString().replace("\"", ""));
            txtAfternoonVitals.setTextSize(16);
            txtAfternoonVitals.setGravity(Gravity.START);
            txtAfternoonVitals.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }else{
            txtAfternoonVitals = new TextView(this);
            txtAfternoonVitals.setText("Vitals " + "- " + "                      " + "Not Done");
            txtAfternoonVitals.setTextColor(Color.RED);
            txtAfternoonVitals.setTextSize(16);
            txtAfternoonVitals.setGravity(Gravity.START);
            txtAfternoonVitals.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }

        if(afternoonSession.has("Lunch")){

            txtAfternoonLunch = new TextView(this);
            txtAfternoonLunch.setText("Lunch " + "- " + "                     " + afternoonSession.get("Lunch").toString().replace("\"", ""));
            txtAfternoonLunch.setTextSize(16);
            txtAfternoonLunch.setGravity(Gravity.START);
            txtAfternoonLunch.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }else{
            txtAfternoonLunch = new TextView(this);
            txtAfternoonLunch.setText("Lunch " + "- " + "                     " + "Not Done");
            txtAfternoonLunch.setTextColor(Color.RED);
            txtAfternoonLunch.setTextSize(16);
            txtAfternoonLunch.setGravity(Gravity.START);
            txtAfternoonLunch.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }

        if(afternoonSession.has("Oral Medication")){

            txtAfternoonOral = new TextView(this);
            txtAfternoonOral.setText("Oral Medication " + "- " + "    " + afternoonSession.get("Oral Medication").toString().replace("\"", ""));
            txtAfternoonOral.setTextSize(16);
            txtAfternoonOral.setGravity(Gravity.START);
            txtAfternoonOral.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }else{
            txtAfternoonOral = new TextView(this);
            txtAfternoonOral.setText("Oral Medication " + "- " + "    " + "Not Done");
            txtAfternoonOral.setTextColor(Color.RED);
            txtAfternoonOral.setTextSize(16);
            txtAfternoonOral.setGravity(Gravity.START);
            txtAfternoonOral.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }

        afternoonLayout.setPadding(10,10,10,10);
        afternoonLayout.addView(txtAfternoon);
        afternoonLayout.addView(txtAfternoonVitals);
        afternoonLayout.addView(txtAfternoonLunch);
        afternoonLayout.addView(txtAfternoonOral);


    }

I have alligned the code using blankspace, this is not the proper way of doing since im getting different allignment in different mobile versions

Output needed:

  Afternoon
  vitals            02:00pm 
  Lunch             03:00pm
  Oral Medication   04:00pm

But i am getting unalligned textview in different mobile versions.Is there any way of adding weight to java file.Since im doing string concatination i am unable to use weights

This is the output i am getting

enter image description here

  • 1
    Why don't you use recyclerview for this purpose and create a proper row with weights that will be easy for this requirement ? – Faraz Ahmed Mar 07 '17 at 12:09
  • You can also assign weights to each textView and just ensure that the width and height is both set to match_parent – Janwilx72 Mar 07 '17 at 12:09
  • Add third parameter to setLayoutParams like this - txtAfternoon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f)); – Ragini Mar 07 '17 at 12:12
  • @FarazAhmed : need to change logic as well, so its better to implement using this format –  Mar 07 '17 at 12:13
  • I think the guy needs more of a monospace font adjustment, than adjustment of the weights of the view. – H.A.H. Mar 07 '17 at 12:16
  • Possible duplicate of [How to set layout\_weight attribute dynamically from code?](http://stackoverflow.com/questions/4641072/how-to-set-layout-weight-attribute-dynamically-from-code) – Umesh Sonawane Mar 07 '17 at 12:42

2 Answers2

0

You can set layoutweight and weightSum from java code like below:

LinearLayout layout = (LinearLayout)findViewById(R.id.afternoonLayoutId);
layout.setWeightSum(10F);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); 
//you can  create new LayoutParams too ...
layoutParams.weight = 0.5f;
txtAfternoonVitals = new TextView(layout.getContext());
txtAfternoon .setLayoutParams(layoutParams);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • Still it is not alligned properly. I tried setting layoutweight and weightsum. –  Mar 08 '17 at 10:50
  • Since i am doing a string concatenation i am not able to allign properly –  Mar 08 '17 at 10:58
0

Weights can adjust the textview to be with different heights. I seriously doubt that this is what you want. If I understand your "align with blankspace", you are looking how to apply monospace font, so that the hours are aligned. Good luck.

H.A.H.
  • 2,104
  • 1
  • 8
  • 21