Dynamically add editextext to android app and retrieve values - android

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();

Related

Finding Id when using array for EditText

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.

android: how to retrieve dynamic edittext

How to get the id of the dynamically created edit text in android for example i have created 3 editext dynamically with same name edtex and different id. how can i retrieve particular edittext from that.. thanks in advance
for(int i=0;i<toke.length;i++)
{
Log.d("","toke="+" "+toke[i]);
if(toke[i].contains("AnswerBox"))
{
Log.d("","####### answerbox present ########");
multied=new EditText(addition.this);
multied.setId(did);
multied.setWidth(80);
multied.setHeight(40);
// tl.addView(multied);
relay.addView(multied);
did++;
}
}
from the above code how can i retrive the edittext created at second iteration..
Please check below code to retrieve edittext
EditText edtTxt=(EditText ) findViewById(did);
In android you can't get (or set) a Resource ID for a dynamically created View. But for what you are trying to do, you could extend a TextView and add the id property.

Dynamic Text Field - Android

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.

How can i get text from text field and place text in another text field in android

Hi
I am new to android,
How can i get text from a text field and store into variable and how can i place text in a text field.
Whati mean is, in C# or Flex or other languages we have functions or properties like if there is some text field txtName, and we do txtName.text or txtName.getText(), it returns text, and if we do like txtName.text ="abc", it assigns a value to it,
but i have not found any thing like that in android yet, please help me.
Regards
Atif
Use getText() and setText() method of TextView.
For example:
TextView tf=new TextView(this);
tf.setText("Hello");
String s=(String) tf.getText();
assuming in your XML layout your EditText has the id text you can get it by
final EditText text = (EditText) findViewById(R.id.text);
String textInEditText = text.getText().toString();
or you can set it by:
text.setText("Some text");
You can find more information about EditText and general Android Development (e.g. what is a layout?) in the normal guides.
You should first get ids for the EditTexts:
EditText et1=(EditText) findViewById(R.id.edit_text1); //replace edit_text1 with your id
EditText et2=(EditText) findViewById(R.id.edit_text2); //replace edit_text2 with your id
et2.setText(et1.getText());

Reading from and writing to EditText

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() );

Categories

Resources