I want to create EditTextFields dynamically on depending the condition. Condition is that if I start typing on first EditTextField it will create one more EditTextField in the bottom and will create the third EditTextField when i start typing on second one. Similarly i want to delete the bottom text if there is no text in the upper EditTextField. Thanks.
Use a parent view, like a ScrollView that you know you can add a flexible about of content to. Then use a TextWatcher a/k/a a text change listener. You could then create a new text view which you would add to the ScrollView if text was typed into the EditText field.
For neatness I'd probably create a custom TextView class that housed this text change listener and replication check. Here's example of how you could add a TextView
//instance variable
private LinearLayout containerLayout;
private newTextViewCreated = false;
//initialize your conatinerLayout before you use it
//and create your first edit text field
public void onCreate(Bundle savedInstaceState){
containerLayout = (LinearLayout)findViewById(R.id.conatinerLinearLayout);
createEditText();
}
private void createEditText(){
EditText editText = new editText(this);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count > 0 && !newTextViewCreated){
createEditText();
newTextViewCreated = true;
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
//TODO Auto-generated method stub
}
});
containerLayout.addView(editText);
}
I didn't test this out, I'm writing it now but here's what I'm thinking. Read the description of how a TextWatcher works so you understand the inner methods. You're going to have to play with the conditionals but what you're doing is listening for a change in the number of characters entered and then making a recursive call to create an additional view when chars are added to each text view. I use a boolean flag to show when a view has been created so we don't add one each time the char is changed. I moved outside the createEditText method based on your comment. If you made your own EditText class you could just add a method that would set/get the status of whether this TextView had spanwed another. To remove you would just add a delete condition that would remove the view from the linear layout.
User TextWatcher
Implement your Activity with TextWatcher and override method
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
Show / Hide them in your layout if you know the total amount of editText fields needed or add them programatically like so:
EditText myET = new EditText(MyActivity.this);
myET.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
LayoutContentView.addView(myET);
Then check:
if (myET.getText().toString().trim().equals(""))
{
//Don't Show
}else{
//SHOW
}
SO question could help:https://stackoverflow.com/a/6792359/350421
EditText toAdd = new EditText(this);
list.add(toAdd);
Related
My app has seven editTexts and one button below them. I want the button to change its background when the user filled all editText with numeric values (int or double, except zero).
I think I should use OnTouchChangeListener but I am not sure how to do it, any advice?
Add addTextChangedListener to your EditText.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Add your logic here
}
#Override
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(textWatcher);
// Remember to remove when whatever actions required have completed.
editText.removeTextChangedListener(textWatcher);
Assuming all your EditText views require the same logic, create a single TextWatcher object and bind the listener to each as above. You can also implement this in your class:
class MyClass implements TextWatcher
And add the override methods into the class and write the code in the onTextChanged() method.
Put a line inside your
android:digits="123456789"
And in java file check for the length of edittext.if the length of all editext is greater than 1 then change the color of the button.
If worked ...check my answer
I have an activity in which I get from the user credit card's serial number.
It contains four editTexts - each one for 4 digits.
I've used an array for the editTexts -
EditText[] editSerial = new EditText[4];
and I've restricted the input's length in each editText with
android:maxLength="4"
I want the focus to move to the next editText once the user have entered 4 digits in the current one.
I've seen this answer - How to automatically move to the next edit text in android:
et1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count)
{
// TODO Auto-generated method stub
if(et1.getText().toString().length()==size) //size as per your requirement
{
et2.requestFocus();
}
}
Is there any BETTER solution than repeat this code 3 times?
Kind of. You need a TextWatcher on each one but you can extract it as a proper class so that you can pass in parameters indicating the View to focus on next.
Then it'll be like
et1.addTextChangedListener(new FocusSwitchingTextWatcher(et2));
et2.addTextChangedListener(new FocusSwitchingTextWatcher(et3));
et3.addTextChangedListener(new FocusSwitchingTextWatcher(et4));
The class:
private static class FocusSwitchingTextWatcher implements TextWatcher {
private final View nextViewToFocus;
TextWatcher(View nextViewToFocus) {
this.nextViewToFocus = nextViewToFocus;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length > size) {
nextViewToFocus.requestFocus();
}
}
... // the other textwatcher methods
}
I have this application in which I create multiple EditTexts dynamically. The amount depends on user input. I am trying to allow the focus to change to the next edittext after two characters have been entered. I have this so far:
for(EditText editText : editTextList ) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().length() == 2) {
//code to change focus to next edit text goes here
}
}
});
}
The first problem is that this could potentially create many instances of TextWatcher if the user enters a large number (average in this context would probably be 100-600 EditText fields).
The second problem is how would I go about changing focus in the afterTextChanged method because I would want to change the focus to the next EditText away from the one that is currently being represented inside the loop.
Should I not be concerned about the potential performance issues of multiple TextWatcher objects? Should I scrap this whole implementation and focus elsewhere? Any suggestions will be greatly appreciated.
As you may have a lot of EditTexts at runtime i would suggest you to use maxlength attribute of EditText in this way you will restrict user to enter only 2 characters.
like this
EditText editText = new EditText(this);
int maxLength = 2;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
Now add next button in your keyboard like this
yourEditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
It is the default behavior of next button to change focus to the immediate next view which takes input being in the same parent layout.
Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
I want to create demo app that calculate prize of content in listview.
User can set quantity in Edittext, from that quantity i want auto sum of all imtems.
Currently i have use setOnFocusChangeListener on Edittext.. but i am failed to get tatal.
Please see image...
Any suggestion ?
Thanks..
Instead of setOnFocusChangeListener use for each EditText
addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
totalTextView.setText(getSumOfAll());
}
});
}
On the EditText, you can add a TextWatcher with addTextChangedListener(...).
This TextWatcher will change the underlining data and call onDataChanged() on the adapter.
Use ListViews getChildCount() and getChildAt(i) to get your EditTexts then getText() and
summarize them.
int count = listviewview.getChildCount();
int sum=0;
for (int i=0; i<count; i++) {
EditText t = (EditText)((YourLayout)view.getChildAt(i)).getChildAt(1); //1 will get your EditText from the Layout of the List Item because you have 3 children there.
sum+=Integer.parseInt(t.getText());
}
return sum*yourprice;
hope it helps.
Since your rows ideally are fixed (or not) either or it does not matter. All you need to do is iterate through each item in your ListView's Adapter and then grab the text from each of them and then extract your data. Then keep track of it, and handle it however you wish to do it.
You can set TextWatcher on each textview, and on text changed method, get string from each edit text, parse value to integer, and add to sum.