android table row need to wrap at percentage of row

129 views Asked by At

I have the following code and I need the second line of each table row to wrap at a percentage of the screen width...say 80%. Its the textview3 field need to wrap, while keeping row1 on the screen.

It should be


RowOneLeft RowOneRight

This line should wrap and again\n

and again and again

And again and again.


  //Layout
        RelativeLayout global_relativelayout = new RelativeLayout(getActivity());
        //atulsLayout.setBackgroundColor(Color.GREEN);

    RelativeLayout.LayoutParams tableDetails= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams tablerowDetails= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams linearvparam= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh1param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh2param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh3param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams textview1param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textview2param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textview3param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams textview4param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    TableLayout maintable = new TableLayout (getActivity());
    maintable.setStretchAllColumns(true);
    maintable.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    maintable.setWeightSum(1);

    ScrollView vscroll = new ScrollView(getActivity());
    vscroll.setFillViewport(true);

    maintable.removeAllViews();
    TableRow tablerow0 = new TableRow(getActivity());
    TextView textviewl0 = new TextView(getActivity());
    textviewl0.setText("TITLE ");
    //maintable.addView(tablerow0);

    LayoutParams tablerowparam10 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
    LayoutParams tablerowparam05 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f);
    for (int i = 0; i < 125; i++) {

        final TableRow tablerow = new TableRow(getActivity());

        tablerow.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT, 1.0f));

        final LinearLayout linearv = new LinearLayout(getActivity());
        linearv.setOrientation(LinearLayout.VERTICAL);
        final LinearLayout linearh1 = new LinearLayout(getActivity());
        linearh1.setOrientation(LinearLayout.HORIZONTAL);
        final LinearLayout linearh2 = new LinearLayout(getActivity());
        linearh2.setOrientation(LinearLayout.HORIZONTAL);

        final LinearLayout linearh3 = new LinearLayout(getActivity());
        linearh3.setOrientation(LinearLayout.HORIZONTAL);
        linearh3.setBackgroundColor(Color.BLUE);
        linearh3param.height = 10;
        //linearh3param.width = 600;
        linearh3.setGravity(Gravity.CENTER_HORIZONTAL);
        linearh3.setLayoutParams(linearh3param);

        //Row1
        TextView textview1 = new TextView(getActivity());
        textview1.setId(id_textview1);
        textview1.setText("RowOneLeft " + i);
        textview1.setTextColor(Color.BLACK);
        textview1.setGravity(Gravity.LEFT);
        textview1param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textview1.setLayoutParams(textview1param);
        linearh1.addView(textview1,tablerowparam05);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("RowOneRight " + i);
        textview2.setTextColor(Color.BLACK);
        textview2.setGravity(Gravity.RIGHT);
        textview2param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        textview2.setLayoutParams(textview2param);
        linearh1.addView(textview2,tablerowparam10);

        //Row2
        TextView textview3 = new TextView(getActivity());
        textview3.setText("This Needs To Wrap  This Needs To Wrap This Needs To WrapEnd " + i);
        //textview3.setText("RowtwoRight  End " + i);
        textview3.setTextColor(Color.BLACK);
        textview3.setGravity(Gravity.LEFT);
        textview3.setSingleLine(false);
        textview3param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textview3param.addRule(RelativeLayout.LEFT_OF,textview1.getId());
        textview3.setLayoutParams(textview3param);
        linearh2.addView(textview3,tablerowparam05);

        tablerow.setId(i);

        linearv.addView(linearh1,linearh1param);
        linearv.addView(linearh2,linearh2param);
        linearv.addView(linearh3,linearh3param);

        tablerow.addView(linearv);
        maintable.addView(tablerow);
    }

    vscroll.addView(maintable,tableDetails);
    view = vscroll;

    return view;
}

Screen Print

1

There are 1 answers

0
sylwano On

Check PercentRelativeLayout.

You can set percentage width in xml:

  app:layout_widthPercent="80%"

or in code:

PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.widthPercent = 80;