6
votes

In my project, i need to draw rows of textview dynamically according to the data received to show it. To align them properly, i have used Table Row n xml, and called it in java code. Now, in their LayoutParam i have given MATCH_PARENT, but it wraps the text according to the length of data received. Now, i want to fix width of the fields for a tabular view. I do all this process in postExecute method. In this method, i used setWidth function to set it according to the width of header row element.Here, Sno is a view, while size is array containing width of all elements of headerRow.

 Sno.setTag(patient);
 Sno.setWidth(size[0]); 

But it didn't solve this problem, when i tried getWidth to see its width, it was showing its value 0. Then i tried to set this width using LinearLayout.LinearParams

LinearLayout.LayoutParams params_sno = new LinearLayout.LayoutParams(size[0],
LinearLayout.LayoutParams.MATCH_PARENT);
Sno.setLayoutParams(params_sno);

but still no benefit, secondaly, if i remove MATCH_PARENT from width of LayoutParams, its width got increased from width of header row element. fields without data are invisible.

4

4 Answers

4
votes

Try this

TableRow row = new TableRow(activityContext);

            TableLayout.LayoutParams td_tr = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            row.setWeightSum(10);
            row.setLayoutParams(td_tr);
            row.setGravity(Gravity.CENTER);
            row.setBackgroundColor(Color.parseColor("#D2D2D2"));

            TextView tv= new TextView(activityContext);
            tv.setText(String.valueOf(i));
            tv.setTextAppearance(activityContext, style.tvBoldRow);
            tv.setLayoutParams(new TableRow.LayoutParams(0 , LayoutParams.WRAP_CONTENT, 1)); // Here you can set weight to your TextView.
            tv.setPadding(2, 2, 2, 2);
            tv.setGravity(Gravity.CENTER);
            row.addView(tv);
0
votes

try this:

      LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
      tv.setLayoutParams(Params1);  
0
votes

Add the following parameter

android:weight = "1"

in xml to the textview and set

android:layout_width = "0dp".

I think this should work for you.

Yes you can set weight from the java code.

To set weight from java code :

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT,
                             LayoutParams.MATCH_PARENT, 1.0f);

The last parameter is the weight.

0
votes

As tsp said

LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);

then while adding it in table layout you have overrided add methode which takes view and layout params.

You can also consider TypedValue.applyDimention() to give values in DP

LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 15, objDeisplayMetrics),TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 50, objDeisplayMetrics));