0

I am creating a dynamic number of editTexts and want to eventually pull the ID for each to call .getText() on the editText.

However, I noticed that it is difficult to programmatically set the ID, so I am using the .setTag() method instead:

private void createAnswerChoice(int answerNumber) {
    ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_questions_answer_layout);
    EditText editText = new EditText(getActivity());
    editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber);
    editText.setSingleLine(true);
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber));
    editText.setTag(editTextID);
    Toast.makeText(getActivity().getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show();
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    editText.setLayoutParams(layoutParams);
    TextInputLayout newAnswer = new TextInputLayout(getActivity());
    newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    newAnswer.addView(editText, layoutParams);
    layout.addView(newAnswer);
}

How would I get the value of each editText if I know the tag and not the ID? Also, what is the purpose of the .setTag() method (how does it relate to .setID()?)

tccpg288
  • 3,242
  • 5
  • 35
  • 80
  • 2
    What is the issue with setting the ID? – pratnala Jul 08 '16 at 01:25
  • If you are using android, then it is advisable to use xml instead of programmatically creating views. It is lot easier to make changes and gives a cleaner code – suku Jul 08 '16 at 01:28
  • I am creating dynamic views based on a number that the user selects, therefore static XML (as far as I know) would not be adequate – tccpg288 Jul 08 '16 at 01:31

3 Answers3

2

create resource file(id.xml) in res/values/id.xml

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

and then set,

 editText.setId(R.id.edittext_hello);
Priya
  • 672
  • 1
  • 5
  • 21
0

I do not recommend using tags to get views from your hierarchy but if you must this SO post describes how to do it: https://stackoverflow.com/a/16262479/6526330.

Tags should be used when you need to cache some data in a view to grab out later when you have the view again (yes I know very generic). Some examples would be if you are using a holder in a list view or if you have a lot of views on a screen and want a global click listener vs listeners for each view. I stole both of these examples from the answers on this post which do a hell of a better job at describing the use cases than I can.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
0

findViewById() and findViewWithTag() are methods intended to obtain a reference to a View that was inflated from XML.

If you are creating the Views, you have them already then there's no need to use those methods. Keep the reference to the View somewhere.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134