How to add "+91" and remove "+91" in edit text in android? - android

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) {
}
};

Related

How to limit specific character in edit text?

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

Press zero any number of times in edit text, but it should be visible to user in only 1 digit like Calculator

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");
}
}
}
});

Disable button if edit text field is empty. Can't get it to working

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

Automatically passing focus betwen EditText

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

EditText - Cursor coming to start for every letter when clear text

I set TextWatcher to EditText like below. But when I try to clear text, cursor is coming to start after clearing every letter.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
}
}
Do it like this (UPDATED):
class MyInputWatcher implements 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 temp = s.toString().replaceAll("[^a-zA-Z]", "");
if (s.toString().length() != temp.length()) {
et.setText(temp);
et.setSelection(temp.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
Please try like this
editText.setSelection(editText.getText().toString().length());
Subhash Kumar, you can use method:
et.setSelection(position)
for displaying cursor in need position
Set position to your cursor on afterTextChanged() method like this.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
et.setSelection(et.getText().toString().length())
}
}
Every time you clear a character it calls onTextChanged() method, as your implementation it get the edittext text and back set to it, so the cursor comes to the starting of the text. Clear et.setText(s.toString().replaceAll("[^[:alpha:]]", "")); and it will be fixed. Or use this et.setSelection(et.getText().toString().length+1);

Categories

Resources