2

I am programmatically creating a dynamic TableLayout with a mixture of TextView and EditText. This TextViews are fetched from the sqlite database of application. The last column of the TableLayout consists of the EditText whose state is set to Invisible. I want to set the state of the EditText of the particular row of TableLayout to Visible based on the onClick over a row of TableLayout. After onClick, EditText should be visible and I want to store the entry made in that editText within the database.

My code of attempt made for this is below:

        List<Details> customerDetailList = db.getAllCustomers(routeId);

        Details customerDetails;

        TableLayout stk = (TableLayout) findViewById(R.id.tableLayout1);

        stk.removeAllViews();

        TableRow tbrow0 = new TableRow(this);
        TextView tv0 = new TextView(this);
        tv0.setText("Sl.No.");
        tv0.setTextColor(Color.BLACK);
        tv0.setTypeface(null, Typeface.BOLD);
        //tv0.setTextSize(20);
        tv0.setPadding(5, 5, 5, 5);
        tbrow0.addView(tv0);

        TextView tv1 = new TextView(this);
        tv1.setText(" Patron ID ");
        tv1.setTextColor(Color.BLACK);
        tv1.setTypeface(null, Typeface.BOLD);
        //tv1.setTextSize(20);
        tv1.setPadding(5, 5, 5, 5);
        tbrow0.addView(tv1);

        TextView tv2 = new TextView(this);
        tv2.setText(" Name ");
        tv2.setTypeface(null, Typeface.BOLD);
        //tv2.setTextSize(20);
        tv2.setTextColor(Color.BLACK);
        tv2.setPadding(5, 5, 5, 5);
        tv2.setGravity(Gravity.CENTER);
        tbrow0.addView(tv2);

        TextView tv3 = new TextView(this);
        tv3.setText(" Requested ");
        tv3.setTextColor(Color.BLACK);
        tv3.setTypeface(null, Typeface.BOLD);
        //tv3.setTextSize(20);
        tv3.setPadding(5, 5, 5, 5);
        tv3.setGravity(Gravity.CENTER);
        tbrow0.addView(tv3);

        TextView tv4 = new TextView(this);
        tv4.setText(" Supplied ");
        tv4.setTypeface(null, Typeface.BOLD);
        //tv4.setTextSize(20);
        tv4.setTextColor(Color.BLACK);
        tv4.setPadding(5, 5, 5, 5);
        tbrow0.addView(tv4);


        tbrow0.setBackgroundResource(R.color.colorLightGray);
        stk.addView(tbrow0);


        for (int row = 0, j = 1; row < customerDetailList.size(); row++, j++) {
            customerDetails = customerDetailList.get(row);
            TableRow tbrow = new TableRow(this);
            tbrow.setClickable(true);
            final TextView t1v = new TextView(this);
            t1v.setText("" + j);
            t1v.setTextColor(Color.BLACK);
            t1v.setGravity(Gravity.CENTER);
            t1v.setPadding(5, 5, 5, 5);
            tbrow.addView(t1v);
            TextView t2v = new TextView(this);
            t2v.setText("" + customerDetails.getcId());
            t2v.setTextColor(Color.BLACK);
            t2v.setGravity(Gravity.CENTER);
            t2v.setPadding(5, 5, 5, 5);
            tbrow.addView(t2v);
            TextView t3v = new TextView(this);
            t3v.setText("" + customerDetails.getName());
            t3v.setTextColor(Color.BLACK);
            t3v.setGravity(Gravity.CENTER);
            t3v.setPadding(5, 5, 5, 5);
            tbrow.addView(t3v);
            TextView t4v = new TextView(this);
            t4v.setText("" + customerDetails.getRequested());
            t4v.setTextColor(Color.BLACK);
            t4v.setGravity(Gravity.CENTER);
            t4v.setPadding(5, 5, 5, 5);
            tbrow.addView(t4v);
            final EditText t5v = new EditText(this);
          //  t5v.setHint(0);
            t5v.setId(j);
            t5v.setVisibility(View.INVISIBLE);
            t5v.setTextColor(Color.BLACK);
            t5v.setGravity(Gravity.CENTER);
            t5v.setPadding(5, 5, 5, 5);
            tbrow.addView(t5v);

            tbrow.setBackgroundResource(R.color.colorWhite);

        tbrow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                TableRow tr1=(TableRow)v;
                TextView tv1= (TextView)tr1.getChildAt(0);
                //Get the integer value of tv1 to make the corresponding edittext to be visible
                t5v.setVisibility(View.VISIBLE);

                Toast.makeText(getApplicationContext(),tv1.getText().toString(),Toast.LENGTH_SHORT).show();

            }
        });
            stk.addView(tbrow);


        }

I found similar kind of questions being asked in How to Set Id in EditText programmatically in Android and Android: Set Edit text or text view id Programmatically wherein, solution provided to make use of editText.setId(i) where i is any integer value. but it did not help me with my situation.

Community
  • 1
  • 1
Param
  • 49
  • 8

2 Answers2

1

You can create a custom id in an xml file and set that id using setId() method. Here are the steps to do that:

1) Create the file ids.xml under res/values directory.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_custom_id" type="id" />
</resources>

2) Then create your view programmatically and set its id:

EditText et = new EditText(getContext());
et.setId(R.id.my_custom_id);

That should do the trick.

EDIT: You can also do the following if you do not know the number of views to be created and cannot use id's from xml file:

et.setId(View.generateViewId());

The generateViewId() method creates a random unique id for you.

yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • Thanks for the reply. But I want to create list of EditText and set ID to it programmatically. I cannot make use of xml file to set the id for the EditText. As I have no knowledge about the number of EditText that can may be created since it depends on the number of record existing in the table from which I am fetching to display record using table layout. – Param May 12 '17 at 07:20
  • @Param then instead of using ids from xml file, you can use `et.setId(View.generateViewId())`. Please let me know if that works. – yrazlik May 12 '17 at 07:28
1

I followed the below steps to set the Id's for the programmatically created EditText and to retrieve data entered in the edittext.

Step 1: Make global declaration for edittext and int variable as

int idOfEditText;
EditText t5v;

Step 2: Setup the Id for editText as

 t5v = new EditText(this);
 t5v.setId(Integer.valueOf(j)); //I tried with just j being declared as t5v.setId(j); but it was showing up error with the suggestion as Expected resource of type id in android studio. I have made int variable **j** to loop till list.size() 

Step 3: Get the id of the row onclick over the table using

 tbrow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                TableRow tr1=(TableRow)v;
                TextView tv1= (TextView)tr1.getChildAt(0);
                //Get the integer value of tv1 to make the corresponding edittext to be visible
                idOfEditText = Integer.valueOf(tv1.getText().toString());
                t5v = (EditText) findViewById(idOfEditText);

            }
        });

Step 4: The data entered in the EditText can be retrieved by

t5v = (EditText) findViewById(idOfEditText);
String value = t5v.getText().toString();
Toast.makeText(this, "You entered: "+value, Toast.LENGTH_SHORT).show();
Param
  • 49
  • 8