Since my question asking about separating characters that are typed in an EditText into groups or blocks didn't receive much attention, I've come here with another question on an alternative way to accomplish what I need.
So, my idea is to have four EditText that have 4 char length. Now what I want is to change the focus as the user type, from an EdutText to another when the limit is reached.
Example: Let's say we have EditText A, B, C, D; the first to gain focus is EditText A, then when the chars length at A reach 4, the focus passes to EditText B and so on and stops At D.
I'm trying something like the code bellow, but the focus is not requested when the length reaches 4 chars.
inputFieldA.requestFocus();
inputFieldA.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldB.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldC.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldD.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
So I'm here asking if there is a way to do that.
Write your conditions in afterTextChanged method and make changes as follows :
inputFieldA.requestFocus();
inputFieldA.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
if(inputFieldA.getText().toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
});
inputFieldB.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
if(inputFieldB.getText().toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
});
inputFieldC.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
if(inputFieldC.getText().toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
});
inputFieldD.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
if(inputFieldD.getText().toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
});
if(inputFieldA.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
replace this lines in onTextChange() methode.
instead of adding validation on the basis of CharSeq length add validation on the basis of edittext text like this:
replace
if(s.toString().trim().length() == 4)
with
if (currEdittext.getText().toString().length() >= 4)
Note: update your condition as well.
currEdittext will be either inputFieldA, inputFieldB, inputFieldC depending on your requirement
Related
My question is that How to limit specific character in edit text?
for example I want to limit # character using max. 3 times in edit text.
How can I do this? Any advice or code sample please.
thanks.
use this
yourEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count>3){
yourEditText.setEnabled(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try this
int currHash=0;
edtText.addTextChangedListener(new TextWatcher() {
CharSequence previous;
public void afterTextChanged(Editable source) {
if(source.toString().contains("#") && currHash=2){
source.clear();
source.append(previous.toString());
}
else
if(source.toString().contains("#"))
currHash++;
}
public void beforeTextChanged(CharSequence source, int start, int count, int after) {
previous = source;
}
public void onTextChanged(CharSequence source, int start, int before, int count) {}
});
I found a solution like this.
etDesc.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
hastagCount = s.toString().length() - s.toString().replace("#", "").length();
if(hastagCount<=3){
previousDesc = s.toString();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
hastagCount = s.toString().length() - s.toString().replace("#", "").length();
if(hastagCount>3){
s.clear();
s.append(previousDesc);
int pos = etDesc.getText().length();
etDesc.setSelection(pos);
etDesc.setFocusable(true);
}
}
});
I have an edit text to fill amount. So, I added text watcher, but I don't want user to "0000000..." if user will continuously press "0000..." instead it should show only one time '0' like Calculator
How can I do that?
private void setAmount() {
etAmount.addTextChangedListener(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) {
if (s.length() == 1) {
if (s.toString().equals("0")) {
etAmount.setText("0");
}
}
//Not working
}
#Override
public void afterTextChanged(Editable s) {}
});
}
I think, my logic is also wrong, because when I enter single input and check s.length, and after et.setText("0"), it will go to infinite loop.
Try this:
etAmount.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
String text = s.toString();
if (text.length() == 2 && text.charAt(0) == '0') {
etAmount.setText(text.substring(1));
etAmount.setSelection(1);
}
}
});
You can do it like this:
etAmount.addTextChangedListener(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) {}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 2) {
if (s.toString().startsWith("00")) {
etAmount.setText("0");
}
}
}
});
Currently I am learning to code and came across one problem. I need my upload button to be disabled untill Edit Text field is somekind of symbol than default hint
Full code
private TextWatcher EmptyEdit = 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) {
if (s.toString().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
Add your code in afterTextChanged() instead of onTextChanged().
private TextWatcher EmptyEdit = 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.toString().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
};
Do your code inside afterTextChanged().
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
mUpload.setEnabled(!TextUtils.isEmpty(s.toString().trim()))
}
// don't forget to set listner
editText.addTextChangedListener(EmptyEdit);
yourEditText.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
//Disable your button in here
}
});
I multiplied the values in two edittext et_weight and et_rate and got result in the other edittext.Now i want to divide et_freight and et_weight to get result in et_rate when i change value in et_freight.Can anyone suggest a method
et_weight.addTextChangedListener(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) {
if (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et_rate.addTextChangedListener(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) {
if (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et_freight.addTextChangedListener(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) {
if (!et_freight.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_rate.setText(String.valueOf(Integer.valueOf(et_freight.getText().toString()) / Integer.valueOf(et_weight.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
You could do this by checking if the EditText is focused before changing it's value. For example the first condition:
if (et_weight.isFocused() && !et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
Also, to simplify your code you can create some helper methods like these:
private void updateDivide(){
int weight = Integer.valueOf(et_weight.getText().toString());
int freight = Integer.valueOf(et_freight.getText().toString());
et_rate.setText(String.valueOf(freight / weight));
}
private void updateMultiply(){
int weight = Integer.valueOf(et_weight.getText().toString());
int rate = Integer.valueOf(et_rate.getText().toString());
et_freight.setText(String.valueOf(weight * rate));
}
private boolean isSet(EditText editText){
return !editText.getText().toString().matches("");
}
And then use it in your code like this:
et_weight.addTextChangedListener(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){
if (et_weight.isFocused() && isSet(et_weight) && isSet(et_rate)) {
updateMultiply();
}
}
#Override
public void afterTextChanged (Editable s){
}
});
et_rate.addTextChangedListener(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){
if (et_rate.isFocused() && isSet(et_rate) && isSet(et_weight)) {
updateMultiply();
}
}
#Override
public void afterTextChanged (Editable s){
}
});
et_freight.addTextChangedListener(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){
if (et_freight.isFocused() && isSet(et_weight) && isSet(et_freight)) {
updateDivide();
}
}
#Override
public void afterTextChanged (Editable s){
}
});
I have an activity in which I have mobile edit text what I want when user enter mobile number I want to add "+91" before number and when user delete full number I want to delete "+91" also .How can I do that
TextWatcher m_MobileWatcher = 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().contains("+91")) {
m_InputMobie.setText("+91" + s.toString());
Selection.setSelection(m_InputMobie.getText(), m_InputMobie.getText().length());
}
}
};
Try below code this is working fine, I have checked.
TextWatcher m_MobileWatcher = 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) {
String text_value = m_InputMobie.getText().toString().trim();
if(text_value.equalsIgnoreCase("+91"))
{
m_InputMobie.setText("");
}else
{
if(!text_value.startsWith("+91") && text_value.length()>0) {
m_InputMobie.setText("+91" + s.toString());
Selection.setSelection(m_InputMobie.getText(), m_InputMobie.getText().length());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
};