I would like to create an array of EditText for android but it seems that finding the id has been a very challenging task.Can someone reach out?
My code for the array:
EdiTText[] mEditText = new EditText[20];
for(int i =0;i<mEditText.length;i++){
mEditText[i] = (EditText) findViewById(i);
}
You cannot set id to view in runtime. Read more about android id here.
Just use constructor to initialize EditText:
mEditText[i] = new EditText(this);
There's two approaches to UI elements like EditText:
a. Implement them in your layout xml like this:
<EditText android:id="#+id/myedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Here the EditText would have the id "myedittext". You can then do stuff with that EditText by referencing it from your code with
EditText et = (EditText)findViewById(R.id.myedittext);
b. Programmatically create them like
EditText et = new EditText(context)
With this approach you will also have to manually add this EditText to your UI / layout.
You mixed those two approaches in a way that won't work.
If you don't know beforehand how many UI elements (in your case EditTexts) you need, it's cool to create them programmatically, but you should definitely read up on how to do that properly.
I'm not going to ask why you're trying to do this. However...
The resource IDs needed by findViewById will not start at zero as implied by your code snippet which uses the loop index for id.
If the EditText are created in XML, then you need the resource IDs of the form R.id.xxxx. You could create an int[] of the IDs and pass the corresponding id to findViewById.
If you create the EditText in code, then you won't have IDs, so will have to store the EditText objects at creation time.
Related
I am building an app where the users can add editText dynamically so that I can send those values into my database. Almost all websites I have come across only add views but they don't retrieve the values.
While add view in you set tag edittext useing below method
LinearLayout linLayout = (LinearLayout)findViewWithTag("layout1");
Also for getting text from the edittext you can use below code
EditText etText = (LinearLayout)findViewWithTag(“Tag1”);
String text=etText.getText().ToString();
How to retrieve the value "topRight" on this function on the layout? I need to get this value in order for me to know the name of the container so that i can set the the arrangement of my child. I tried to use getId() but it returns an integer of course not the value "topRight". Please help guys.
android:id="#+id/topRight
Thanks.
When you define an ID in your layout, it simply modifies the auto-created R file with a new field. The name you specified will be the name of the variable (R.id.topRight).
I guess you might be able to access the name of the variable via some reflection magic, but I feel like you're going about this in a wrong way.
IDs are used to reference the view from the layout and create instances during runtime.
If you want to have some string stored in the layout elements which you can use to determine what view you'd like to use, I would probably go with the "Tag" property rather than ID.
Hope this helps.
Look at this, I have a textView with editTextUserName as an id so I get its value like the one I posted. Try it, I hope it works for you.
EditText editTextUserName = (EditText) findViewById(R.id.editTextUserName);
<EditText
android:id="#+id/editTextUserName"
android:hint="User Name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
I'm trying to create a dynamic text field that will take multiple fields of user input during a single activity, do a calculation in the java file, and then display the resulting value within the SAME activity in a text field.
Is there any way of doing this? I just figured out that I can't edit strings.xml dynamically, so are there any structures I can use that will allow me to constantly change the values?
Thanks all.
This will be pretty straight forward:
String yourTextValue = "text";
TextView myTextView = (TextView) findViewById(R.id.textView1);
myTextView.setText(yourTextValue);
Just call setText() with your String value (whatever it may be) each time you want to change the value of your TextView.
I have a XML tag like "Yes,No,Dontknow " and I am parsing the XML file and getting the data. Now I need to display each option in separate TextView, i.e: 'yes' should be displayed in one TextView, 'No' should be displayed in another TextView, and 'Dontknow' should be displayed in another TextView, but how can I do this, can anyone give me some idea (I am new to Android).
Use setText() method of TextView to load text into it.
You can use string tokenizer:
StringTokenizer tokens = new StringTokenizer(theString, ",");
while( tokens.hasMoreTokens() ){
String token = tokens.nextToken();
// here you must have the reference to the text view...
textView.setText(token);
}
If you are creating the text views programmatically, then you must create or reference those text views inside the loop. Other wise, if the text views are static, you better put each token inside an array or something (words[0] will be Yes, word[1] will be No, etc) and then you set those strings manually.
You can just declare 3 separate TextView in you Activity layout file. Using attribute android:text you can assign the text for the TextView.
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
/>
parse xml file store that in a string.take an array like String[] array = parsedstring.split(","); then take 3 text views ,put array[0],array[1],array[2] on to textview
If you want to split comma-separated strings, take a look at using java.util.StringTokenizer. You can tell it to use , as the token separator.
What is the best way to read the text from an EditText into code, and to write some text from code to the EditText?
Sorry I have ment not the TextView but the EditText
Hi all
I am a new to android I wish to write automatically from code to EditText and read in code from EditText
What is the best way to do it.
Java classes usually expose readable attributes with a get* method, and writable attributes with a set* method. In the case of a EditText these are:
getText
and
setText
see here and here (they are inherited from TextView)
Note: Scroll around a bit. You will see that they are defined multiple time. With different parameters. Pick the one you need.
A simple example. Let's assume you have a TextView with the id myTextField:
EditText myText = (EditText) this.findViewById(R.id.myTextField);
// Setting the text:
myText.setText( "Hello World!" );
// "Reading" the text (printing it to stdout):
System.out.println( myText.getText() );