Question

I want to add x rows when user press the add button and in one of the row user enters no of types i.e. in edittext then according to that i need to populate that many no.of rows in two columns below that .. these columns alignment is missing..

LAYOUT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add requirefields" />

    <ScrollView
        android:id="@+id/scrolllayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="10dp" >

        <TableLayout
            android:id="@+id/tablelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TableLayout>
    </ScrollView>

</RelativeLayout>

ACTIVITY:-

public class MainActivity extends Activity {

    Button addBtn;
    TableLayout mTable;
    TableLayout timingsTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addBtn = (Button) findViewById(R.id.button1);
        mTable = (TableLayout) findViewById(R.id.tablelayout);

        addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "btnonclicklistener",
                        Toast.LENGTH_LONG).show();
                createTable();

            }
        });
    }

    public void createTable() {
        // mTable.setColumnShrinkable(1, true);
        mTable.setStretchAllColumns(true);
        mTable.setShrinkAllColumns(true);

        TableRow rowMName = new TableRow(MainActivity.this);

        TableRow rowDescription = new TableRow(MainActivity.this);
        TableRow rowType = new TableRow(MainActivity.this);
        TableRow rowTimesADay = new TableRow(MainActivity.this);
        TableRow rowTimings = new TableRow(MainActivity.this);
        TableRow rowNoofTimings = new TableRow(MainActivity.this);

        TextView mNametv = new TextView(MainActivity.this);
        mNametv.setText("MNAME");
        TextView descriptiontv = new TextView(MainActivity.this);
        descriptiontv.setText("Description");
        TextView typetv = new TextView(MainActivity.this);
        typetv.setText("TYPE");
        TextView timesAdaytv = new TextView(MainActivity.this);
        timesAdaytv.setText("TimesADay");
        TextView timingstv = new TextView(MainActivity.this);
        timingstv.setText("Timings");

        rowMName.addView(mNametv);
        rowDescription.addView(descriptiontv);
        rowType.addView(typetv);
        rowTimesADay.addView(timesAdaytv);
        rowTimings.addView(timingstv);

        EditText etMName = new EditText(MainActivity.this);
        EditText etDescription = new EditText(MainActivity.this);
        EditText etType = new EditText(MainActivity.this);
        final EditText etTimesAday = new EditText(MainActivity.this);
        // EditText etTimings = new EditText(MainActivity.this);
        timingsTable = new TableLayout(MainActivity.this);
        TextView dosagetv = new TextView(MainActivity.this);
        dosagetv.setText("Dosage");

        rowMName.addView(etMName);
        rowDescription.addView(etDescription);
        rowType.addView(etType);
        rowTimesADay.addView(etTimesAday);
        rowNoofTimings.addView(timingsTable);
        rowTimings.addView(dosagetv);

        etTimesAday.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                if (!s.toString().equals("")) {
                    int length = Integer.parseInt(s.toString());
                    if (length >= 1) {
                        timingsTable.removeAllViews();
                        timingsTable.setVisibility(View.VISIBLE);
                        dynamicEditBox(etTimesAday.getText().toString());
                        Toast.makeText(getBaseContext(), "if condition",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getBaseContext(), "else condition",
                            Toast.LENGTH_SHORT).show();
                    timingsTable.setVisibility(View.GONE);
                    timingsTable.removeAllViews();
                    if (s.equals("")) {
                        etTimesAday.setText("");
                    }
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
        mTable.addView(rowMName);
        mTable.addView(rowDescription);
        mTable.addView(rowType);
        mTable.addView(rowTimesADay);
        mTable.addView(rowTimings);
        mTable.addView(rowNoofTimings);

    }

    public void dynamicEditBox(String id) {
        timingsTable.setStretchAllColumns(true);
        timingsTable.setShrinkAllColumns(true);

        for (int i = 0; i < Integer.parseInt(id); i++) {

            TableRow tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            TableRow.LayoutParams parms = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            TextView ed = new TextView(getApplication());
            ed.setInputType(InputType.TYPE_CLASS_NUMBER);
            ed.setLayoutParams(parms);
            ed.setId(i);
            ed.setHint("enter number");
            ed.setBackgroundColor(Color.DKGRAY);
            tr.addView(ed);

            EditText b = new EditText(getApplication());
            b.setLayoutParams(new TableRow.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            b.setId(i);
            b.setBackgroundColor(Color.LTGRAY);
            b.setText("dosage");
            tr.addView(b);

            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 10, 10, 0);

            timingsTable.setLayoutParams(layoutParams);
            timingsTable.addView(tr);

        }
    }

}

Currently i'm getting as follows..

enter image description here

I need to adjust those timings and dosage columns accordingly.help me in setting this alignment..

Was it helpful?

Solution

After making below changes in the dynamicEditBox method i got the alignment

public void dynamicEditBox(String id) {
    timingsTable.setStretchAllColumns(true);
    timingsTable.setShrinkAllColumns(true);

    for (int i = 0; i < Integer.parseInt(id); i++) {

        TableRow tr = new TableRow(MainActivity.this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.WRAP_CONTENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
        TableRow.LayoutParams parms = new TableRow.LayoutParams(0,
                TableRow.LayoutParams.WRAP_CONTENT, 1f);
        parms.setMargins(5, 5, 5, 5);
        TextView tv = new TextView(getApplication());

        tv.setLayoutParams(parms);
        tv.setWidth(50);
        tv.setPadding(20, 5, 5, 5);
        tv.setId(i);
        tv.setHint("10:25am");
        tv.setBackgroundColor(Color.LTGRAY);
        tr.addView(tv);

        EditText et = new EditText(getApplication());
        et.setLayoutParams(parms);
        et.setPadding(5, 5, 5, 5);
        et.setGravity(Gravity.CENTER);
        et.setRawInputType(Configuration.KEYBOARD_12KEY);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);

        et.setId(i);
        et.setBackgroundColor(Color.LTGRAY);
        et.setHint("dosage");
        tr.addView(et);

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(0,
                TableRow.LayoutParams.WRAP_CONTENT, 1f);
        layoutParams.setMargins(-15, 10, 0, 0);

        timingsTable.setLayoutParams(layoutParams);
        timingsTable.addView(tr);

    }
}

i got in the below way.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top