0

'Please find this attachment I have created an XML file with only one edit text. and in java class i'm dynamically inflating the XML file to create multiple edit text fields. now the problem is resource id are same for all the edit text fields. so is there any way to give different resource ids. please help me.

    import android.content.Context;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.widget.EditText;
    import android.widget.LinearLayout;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        EditText[] sample;
        Boolean Valid;
        ArrayList<String> obj_array = new ArrayList<>();
        LinearLayout root_layout;
        LayoutInflater inflater;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            root_layout = (LinearLayout) findViewById(R.id.root);
            init();
        }

        private void init() {
            obj_array.add("abc");
            obj_array.add("def");
            obj_array.add("ghi");
            obj_array.add("jkl");
            assignToValue();
        }

        private void assignToValue() {
            sample = new EditText[obj_array.size()];
            for (int i = 0; i < obj_array.size(); i++) {
       // here i'm dynamically creating edit text .
                View v= inflater.inflate(R.layout.edittext,null);
                sample[i] = (EditText) v.findViewById(R.id.edit);
                sample[i].setText(obj_array.get(i));
                root_layout.addView(sample[i]);
            }
        }
    }'
Chandana
  • 3
  • 4

2 Answers2

2

Does something like this could help you:

  1. create a file res/values/ids.xml with a content like this for example (with the ids, that will be set dynamically):

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

  2. Then you could add in your java code lines like these:

    edit1.setId(R.id.layout1); edit2.setId(R.id.layout2); edit3.setId(R.id.layout3); //where edit1, edit2 and edit3 are sample dynamically created EditTexts for example

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

Create a 16 Digit integer as base ID, say baseId = 1234567890123450 Set this as id for Edittext by method setId in your class (editTextVariable.setId(baseId)) When a new edittext is inflated add 1 to baseId and set it to the newly inflated edittext. For each newly added edittext add 1 to the previous id and set it. Keep a count of number of edittext added in a variable.

SKT
  • 1,821
  • 1
  • 20
  • 32
  • ok thank you for your answer. when I am going to test the app with UiAutomationViewer it takes id's only from xml file not from java(static only not dynamic). Then how I can get id's of the edittexts? – Chandana Nov 13 '15 at 10:10