1

I would like to add an EditText dynamically into a Fragment. I would like also, adding a String id to this EditText. The following code is called after pressing a Button:

int number_of_editTexts; //At the beginning=0
Context context = getActivity();
EditText editText = new EditText(context);
editText.setId("NofET"+number_of_editTexts);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_HORIZONTAL);
editText.setLayoutParams(params);
RelativeLayout rel=(RelativeLayout) getView().findViewById(R.id.list);
rel.addView(editText);
number_of_editTexts++;

It adds the EditText, but i can't write editText.setId("NofET"+numer_of_editTexts); but only editText.setId(numer_of_editTexts);

Is there a way to do what I want? And also, how can i do something like params.addRule(RelativeLayout.BELOW,R.id.DYNAMIC_ID)?

SctALE
  • 509
  • 2
  • 10
  • 30

1 Answers1

0

Element IDs are pure integers, they can't be set as strings. IDs assigned to elements created in XML are converted into an integer internally and stored as an int.

Dynamically created elements always have a ID of -1 by default. They can be manually assigned an ID through setID() but there is a chance of collision with other IDs created automatically by the system.

To prevent such a collision, the method given in this answer may be used to manually assign an ID.

EDIT: Basically, the link says that if you have API level 17+, you use View.generateViewId() else if you do it manually, you don't go above 0x00FFFFFF as an ID as these are reserved for statically created elements. Other than that, avoid conflicts among IDs created through your code.

However, in the case of this question, a LinearLayout may be a better way to go.

Suppose this is your XML.

<LinearLayout
      android:id="@+id/list"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:orientation="vertical"
      android:gravity="center_horizontal"/>

The Java code to add an EditText to this may be something like the following:

List<EditText> edittexts;
...
LinearLayout rel=(LinearLayout) getView().findViewById(R.id.list);
...

void addEditText(Context myContext,int edittextno)
{
    EditText ed=new EditText(myContext);
    ed.setText("EditText"+edittextno);
    LayoutParams lParamsMW = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    ed.setLayoutParams(lParamsMW);
    edittexts.add(ed);
    rel.addView(ed);
}

You can modify all the created EditTexts through the List edittexts.

The vertical LinearLayout automatically gives the vertical list format required by the OP. A margin can be added to each added element if required. If required to add more elements to the left or right of the EditText, a horizontal LinearLayout may be dynamically created, elements added to it and the horiz. LinearLayout added to the static one in a similar manner as in the code above.

Community
  • 1
  • 1
supersonic_ht
  • 354
  • 4
  • 15
  • I feel like an ArrayAdapter that inflates EditTexts would still be a better solution – OneCricketeer Jun 11 '16 at 15:11
  • In any case, you might want to set LayoutParams on the views that you add – OneCricketeer Jun 11 '16 at 15:13
  • An ArrayAdapter would be complex to implement if each row (I'll call it a row for now) in the layout had more than one element. Also, I think only the LayoutParams relevant to RelativeLayout require IDs. `RelativeLayout.BELOW`, `RelativeLayout.RIGHT_OF` – supersonic_ht Jun 11 '16 at 15:18
  • I don't think it would be complex at all. You load a list of strings into an ArrayAdapter, the ArrayAdapter renders a simple EditText, and you are free to dynamically add and remove any element from it. No one has said anything about complex views, so using a LayoutInflater isn't necessary. Regarding the LayoutParams, how would the EditTexts expand to the "row", then? They do not have a wrap_content height and match_parent width by default – OneCricketeer Jun 11 '16 at 15:24
  • Sorry, I misunderstood. I thought you were referring to one still requiring IDs. Editing my answer with the LayoutParams. I don't have much experience with ArrayAdapters other than having used it to populate a Spinner long ago. I'll read more about it. – supersonic_ht Jun 11 '16 at 15:32
  • @supersonic_ht thank you for your answer, but if i want to do something like before, using Layout_Below ID_OF_NEW_EDITTEXT, what should i write to refer to that edittext in particular? – SctALE Jun 11 '16 at 15:35
  • @Ale1794 Read the part where I wrote about manually assigning an ID. All you need to do is avoid conflicts. The link I referred to in that section will help. – supersonic_ht Jun 11 '16 at 15:38
  • @Ale1794 This answer doesn't use ids. There is an Arraylist of the added EditTexts that you can get the previous one when you add a new one. You don't need to "layout below" anything with a LinearLayout – OneCricketeer Jun 11 '16 at 15:38