3

I have this button on GridLayout called addnewTask. When you create this button, it will create an EditText.

private GridLayout gridLayout;

int rowIndex = 3;
int colIndex = 1;

int i=0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_task);

    gridLayout = (GridLayout) findViewById(R.id.taskLayout);

}

This function to create EditText when the button is clicked -->

public void addView(View view) {
    i++;
    String tname = "task" + Integer.toString(i);
    EditText editText = new EditText(this);
    GridLayout.LayoutParams param = new GridLayout.LayoutParams();
    param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    param.width = GridLayout.LayoutParams.MATCH_PARENT;
    param.rowSpec = GridLayout.spec(rowIndex);
    param.columnSpec = GridLayout.spec(colIndex);
    editText.setLayoutParams(param);
    if (rowIndex > 3) {
        editText.setTag(tname);
    }

    gridLayout.addView(editText);
    rowIndex++;
}

My problem is that i want to set the android:id of EditText i created.

like this: When the button is clicked, EditText is created, in row 3, column 1 and id name task1.

When the button is clicked again, another EditText is created, in row 4, column 1 and id name task2.

When the button is clicked again, another EditText is created, in row 5, column 1 and id name task3.

ANS SO ON.....

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
Ruby
  • 43
  • 1
  • 2
  • 10

2 Answers2

5

Ids in android aren't strings - they are always numbers. Even if you write in xml @+id/textId, a number is generated for this text. You can see that in your R file.

What you can do is assign id to your edit texts by using editText.setId(int) method. If you want to be able to easily refer to the edit texts, you can either:

  1. assign the ids sequentially: 1, then 2, 3 etc. Then id of the item would be (row-1) * <columnsCount> + column) (so if you have 3 columns, then second item in fifth row would have id 4 * 3 + 2)

  2. create a map field of type Map<String, Integer>, and again assigns ids sequentially, and save them in.

String tname = "task" + Integer.toString(i);   
EditText editText = new EditText(this);
editText.setId(i);
idsMap.put(tname, i);

You then get edittext's id by calling idsMap.get("task3")

Third option is to just keep reference to your EditText in a map: you'd then have a Map<String, EditText> map, and then call

String tname = "task" + Integer.toString(i);   
EditText editText = new EditText(this);
editTextsMap.put(tname, editText);
wasyl
  • 3,421
  • 3
  • 27
  • 36
  • is it ok that i use the second option only.? i dont understand the first one. – Ruby Sep 26 '15 at 18:29
  • i should implement both of them or is it ok to use the second one ONLY? – Ruby Sep 26 '15 at 18:31
  • No problem - if this solves your problem feel free to accept/upvote the answer – wasyl Sep 26 '15 at 18:33
  • assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components. – Rahul Tiwari Sep 26 '15 at 18:40
  • Yes, although I don't think you will get assigned ids in such low range. See [View#generateViewId method](http://developer.android.com/reference/android/view/View.html#generateViewId%28%29), which basically assigns IDs starting with 1 and going up by one. – wasyl Sep 26 '15 at 18:47
  • wasyl, it doesnt recognize idsMap. uhm, where do u get it?? – Ruby Sep 26 '15 at 18:48
  • @Ruby you'd need to add it as a field, `Map idsMap = new HashMap();` – wasyl Sep 26 '15 at 18:49
  • is editTextsMap from this? -> Map editTextsMap = new HashMap(); – Ruby Sep 26 '15 at 18:53
  • `editTextsMap` would be declared like this: `Map editTextsMap = new HashMap<>()`. But this is separate answer, third option - you can either compute your id, or you can have a map from string to id, or you can have a map from string to your edit text – wasyl Sep 26 '15 at 18:55
  • @wasyl valid argument :) – Rahul Tiwari Sep 26 '15 at 19:01
  • @wasyl, but how to get the value in every EditText i created? i have to get the values to insert in a database – Ruby Sep 26 '15 at 19:08
  • sample = (EditText) findViewById(R.id.task1); – Ruby Sep 26 '15 at 19:09
  • The question was to assign ID to the edit text. If you have problems further along the way please post new question – wasyl Sep 26 '15 at 19:09
  • String task = sample.getText().toString(); is this correct? – Ruby Sep 26 '15 at 19:10
0

You can keep references of these edit text in an array representing cells of your grid. declare arraylist like this:

ArrayList<EditText> etArray = new ArrayList<>();

and keep reference to your EditText in array list at the end of your addView method like this:

etArray.add(i,edittext);

now refer these view like this:

etArray.get(i);

this way you will be able to refer them for accessing text.

assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • what? i dont understand – Ruby Sep 26 '15 at 18:27
  • but how do you set the id to every edittext created? – Ruby Sep 26 '15 at 18:50
  • it is not required to set id to every `EditText`, doing that may cause problems. Referring them with their original references stored in an array will help you get all the data from `EditText` as described in answer. – Rahul Tiwari Sep 26 '15 at 18:55