How to get values from dynamically created EditText fields? - android

I am little confused with following scenario:
I have an add button which I use it to add a number of EditText fields, when I tap on the save button I should get the values from the EditTexts.
How can I get these values from all of the EditText fields?

You could do something like this:
Store all the EditText fields you create programmatically inside a List. So whenever you have viewGroup.add(myEditText); you would also have myList.add(myEditText);
Then when you press 'save' just loop on your list and use getText() to get the data from your EditText fields.
I'm sure there are also other ways to accomplish this ;)

btn_no_of.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1=edittext1.gettext.tostring();
String str2=edittext2.gettext.tostring();
}
});

Related

Getting text from a button

If I use multiple ids with same #Click event method like
#ViewById Button choice1;
#ViewById Button choice2;
#Click({R.id.choice1,R.id.choice2})
void choice(){
//String text = text of the clicked button
}
How will I get the text of the button that is being clicked?
As wrote on the wiki you can add a View parameter in your method signature, like this :
#Click({R.id.choice1, R.id.choice2})
void choice(View clickedView) {
[...]
}
Also you don't have to annotate your buttons with ViewById in order to use Click (unless you really need a reference to these instances, of course)

Clearing edit text data

I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.
But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.
I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.
I also tried using finish() and everything works except I have to go through all the activities again to enter the data.
Try this one also.
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid=editText1.getText().toString();
String pwd=editText2.getText().toString();
editText1.setText("");
editText2.setText("");
}
});
}
Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// As soon as button is clicked, set is as empty
editText.setText("");
}
Try and see if it makes a difference.
you can use this library to clear text by a clear icon
http://droidparts.org/widgets.html#clearableedittext
Use this:
editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or else
write this in XML file
EditText
android:hint="Enter Name" />
i did this way.try
You can use:
myEditText.setText(null);

How to put an Edittext with a button below that when I click, it recognizes if it's correct or wrong?

My intention is to put an Edittext, and a Button below, and when the user put an specific word and click the button, the value is correct, and all the other different words are wrong.. How to identify it with codes?
btn.onClickListiner(this);
#Override
public void onClick(View v) {
if (v == btn) {
// check word
String word = editText.getText().toString(); // don't forget "toString()" .
// checking word below :
}}

How to display a something from an EditText?

I thought making thing part of the app would be easy, however I was wrong. I wish to have a textView display whatever the user wrote in the editText. This is what I tried.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myTextView.setText(myEditText.getText().toString());
// of course I would use variables in place of the
// myTextView and myEditText
}
});
This is another way I tried to get this done.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//num1 is my String variable
num1 = myEditText.getText().toString();
myTextView.setText(num1);
}
});
Both times the textView comes up with nothing in it.
Thank you for any help!
onClickListener merely responds to user clicks. You need to implement a TextWatcher on your EditText. The most straightforward way of doing this is to implement TextWatcher in your class, then make a call to myEditText.addTextChangedListener(this).
I recommend adding something like the following to your onTextChanged method:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myTextView.setText(myTextView.getText()+s);//or something like this...
}
I usually use GetDlgItemText.
char Buffer[120];
GetDlgItemText(hwndDlg, (control), buffer, sizeof(buffer));
This will read it and store it in buffer.
In the EditText the getText call should you return the String, I don't believe you need to call the ToString method on it. The way you are using it in the onClickListener implies you have a button that should be calling a function to set the text into the textview. If you want it dynamically you should be able to use onTextChanged to fill in the data.
First of all check whether the control is coming to your setOnClickListener(). Put in a Log to find that out.
Next make sure that "add" is the button or item that u r using to initiate the copy process.
This statement of yours is correct.
myTextView.setText(myEditText.getText().toString());
Though you do not require the toString(). Doesnt really make a difference. I suggest you check that your textview and edittext is fine.
have you check the visibility of textview ?before clicking add button it is invisible rite?then u have to set the visibility on add button click.
From your code i understood that there is a button here too so try this should work:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.mybutton);
btn.setOnClickListener(btncall);
}
private OnClickListener btncall = new OnClickListener()
{
public void onClick(View v)
{
TextView mytextView = (TextView) findViewById(R.id.MytextView);
EditText myeditText = (EditText) findViewById(R.id.MyeditText);
mytextView.setText(myeditText.getText().toString());
}
};
}

How would i store a number/string into R.string.xx?

with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function:
if the user clicks confirm, his name will be set to R.string.nfoname
which will then be used in another layout through TextView x = setText(R.string.nfoname);
I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or intent.putextra()
Coming to ur source code i see this
public View x = findViewById(R.string.nfoname);
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Why this editText has to be final?
***x=(View) infoname.getText();***
You just use infoname.getText().toString() you will get the string value of the Edittext's current text.
Dude you can do stuff simply.
public View x = findViewById(R.string.nfoname);
This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.
x=(View) infoname.getText();
Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.
Also, as for this...
TextView x = setText(R.string.nfoname);
It would have to be...
TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));

Categories

Resources