I have an app that dynamically adds EditText boxes in one method, and I want to use a button to clear those dynamic EditText boxes.
When they were created, I gave them integer IDs and I'm able to access the EditText variable in the activity. So how do I call that dynamic EditText box by its ID number and clear the text?
Why don't you just save a reference (or list of references) as Activity field? Populate the list and then onClick loop and clear. Doing it by ID is less efficient since you would have to perform a lookup
((EditText) findViewById(id)).setText("");
Reset edittext box on click the button
can.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
log.setText("");
pswd.setText("");
}
});
Related
I'm dynamically creating Edittext on a button click and saving this edittext to an ArrayList of type Edittext and then getting values of all edittext in an ArrayList of type String. While creating new Edittext Iam also creating a BUTTON with cross image which delete that edittext when user click on it. and then Iam passing these edittexts to Options of CheckBox question which is also Iam creating dynamically.
Issue is when I create multiple edittext and randomly delete someone and click ok arraylist remove the last index of it .Iam also setting ids to Edittext but couldnt able to understand how to get the Id of the same Edittext whose value I want to delete and then remove him from ArrayList too.
Here I'am creating the Edittext and adding them in "moreOptList" with there ids.
EditText checkBoxMoreEdt = new EditText(this);
checkBoxMoreEdt.setId(i);
moreOptList.add(i, checkBoxMoreEdt);
Here Iam deleting the view on Button clickListener,but instead of deleting the the exact value from arrayList also its only delete the view and the last index of array from the list.For example if I have enter 4 options
a
b
c
d
and then deleted the option 2 which is b and click ok,it will show me
a
b
c
what its doing is removing the last save value from arrayList now How should I get the Id of exact edittext I want to delete and delete it from arrayList also.
final Button button = new Button(this);
button.setId(b);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chkbParentLayout.removeView(ll);
moreOptList.remove(i);
}
});
Most probably the problem is in ArrayList. When you remove element by index all elements are shifted, hence indexes do not match your views:
list.add(0, 0) //[0]
list.add(1, 1) //[0, 1]
list.remove(0) //[1]
list.remove(1) //throws IndexOutOfBoundsException
One options is to replace ArrayList with HashMap:
map = HashMap<Integer, View>()
map.put(i, view)
...
group.remove(map.remove(i))
Another useful thing is to use setTag/getTag methods of View, you can store your indexes there, so you might not need to have external indexes storage:
view.setTag(index)
index = view.getTag()
As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:
Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?
(Both buttons is in Main Activity)
First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)
//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.
//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
{
View v = (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
{
EditText editText = (EditText)layout.getChildAt(i);//thats it.
Log.w("Edit Text "+i+" Value" , editText.getText());
}
}
Now I want to get all user entered data from those 5 edittext on click
of GET DATA button. Is this possible?
Answer: Yes, this is possible.
How?
Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.
I am making a grading app, basically I have an edit field (number field) that you can enter your grades (in numbers) in, or multiple fields for each grade? But lets say I want to calculate the average grade , and if my edit fields are not in an array I would have to go through each value of the field by id and that is going to make the code too long. So I want an array that takes values from each edit field, so if I have 2 edit fields and I type 4 in the first and 6 in the second my array would be {4,6}. I am a beginner in android development but I have solid Java experience.
There's only so much you can do here since each EditText is its own object with its own value field. So to some extent, you're going to have to reference each EditText.
What you can do at least is track each EditText in a List as a member variable. that way you're only having to grab a reference to each edittext once in your activity. Then, when you need to reference the collection for averaging or whatever else, you can just iterate over the list calling .getText(). If you need to reference a specific edittext, either store a separate reference in another member variable, or look it up in the list by id or by a tag you set (see here for more info on tags).
Maybe init each editText in a HashMap with a list of numeric values and use the editText reference to add and get values to the list of numeric values like this:
final Map<EditText, List<Integer>> editTexts = HashMap<EditText, List<Integer>>();
If you init an EditText you could do something like this:
EditText editText = (EditText) findViewById(R.id.editText);
editTexts.put(editText , new ArrayList<>());
editText.setOnClickListener(new OnClickListener(){
public void onClick(View v){
editTexts.get((EditText) v).add(SOME_VALUE);
}
);
I havn't tested it but this could be a valid strategy.
I am using Edit Text watcher, to do some calculations.like below,
And every Edit Text group (A,B,C) is working fine independently and relatively. But relatively worked only if i come from top to bottom. I want to refresh (getText and setText) on every Edit Text onTextChange() method invoked. i can do this using custom method .
But can i do this in one go , like all edit text values of a particular XML get refresh, so each editText onTextChange will be called in TOP-DOWN approach, Because every descendent group is dependent in some context to previous group .
This is doable, you just must have an algorithm. But yes, it is possible, and not too challenging...
As a hint, EditText.onTextChanged() methods inside of the each listener will need access its other 5 EditText siblings/neighbors. (A-C rows and 1-2 columns, respectively).
All 6 onTextChangedListener implementations for the 6 Edit Texts may need to be unique in how they handle textChanged events. You will need to pass in the objects in which they are responsible for changing in their constructors.
ie
EditText et1 = (EditText)findViewById(R.id.editText1);
EditText et2 = (EditText)findViewById(R.id.editText2);
et1.setOnTextChangedListener(new TextChangedListener(et2){
//global var
EditText nextEditText;
public TextChangedListener(EditText editText){
super();
nextEditText = editText;
}
public void onTextChanged(){
this.alert();
}
public void alert(){
//modify data here and update self if necessary.
//then alert the next boxes on text changed method.
nextEditText = editText.getListener().alert();
}
});
I have to create a group of EditText s that each can contains 1 character of a pin code with auto flow between like in the following image -
when user tapping the the keyboard, the application moves to the next EditText with out any Intervention of the user.
How can it be done?
It's called PinView, here is a link for PinView.
They have made custom keyboard too. Including back button, and verification code, make changes as per your requirement. It's very good and simple tutorial to achieve this important functionality.
Make an array of edit text:
private EditText pinInput[] = new EditText[4];
Then add each EditText in array:
pinInput[0] = (EditText) findViewById(R.id.first);
...
Then implement the functionality by using onTextChanged and onFucusChanged listeners