Text spanned over multiple edittext, Android - java

So, I want a user to enter his/her number plate of his/her car and I want it to be quite stylish by separating all the characters like this picture: enter image description here
Right now it consists of 6 EditTexts with TextWatchers that change the focus for each input. That part works fine but my problem is with the deletion och characters.
When a user want to edit one field, he/she can just click the view and delete eand replace. Although when the whole thing is wrong it is not possible to delete everything at once but you have to click every view and delete by hand.
So what I need is so when the user hit backspace on an empty view, it should change focus to the one before and delete that character and so on. So all the EditTexts will be connected and work as one. I've tried with KeyListeners that listens to backspace but that only works with hardware keyboards and not soft keyboards on the phone.
I'm also happy if someone can point me in the direction of another solution better than this one.
TextWatcher:
registrationPlateEditTexts is an List of all the EditText in order.
private TextWatcher regPlateTextWatcher = 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) {
for (int i=registrationPlateEditTexts.size()-1; i>=0; i--){
if (registrationPlateEditTexts.get(i).getText().length()==1 && i!=registrationPlateEditTexts.size()-1){
registrationPlateEditTexts.get(i+1).requestFocus();
return;
}
else if (registrationPlateEditTexts.get(i).getText().length()==1){
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
#Override public void afterTextChanged(Editable s) {}
};
else if (registrationPlateEditTexts.get(i).getText().length()==1){
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
#Override public void afterTextChanged(Editable s) {}
};`

I took 4 custom edit text with property of getting selected when you press back. Here are the listeners and the CustomEditText elements
mCodeFourEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mConfirmBtn.performClick();
return true;
}
return false;
}
});
mCodeTwoEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeTwoEt.getText().toString();
if (text.length() == 0) {
mCodeOneEt.requestFocus();
mCodeOneEt.selectAll();
return true;
}
}
return false;
}
});
mCodeThreeEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeThreeEt.getText().toString();
if (text.length() == 0) {
mCodeTwoEt.requestFocus();
mCodeTwoEt.selectAll();
return true;
}
}
return false;
}
});
mCodeFourEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeFourEt.getText().toString();
if (text.length() == 0) {
mCodeThreeEt.requestFocus();
mCodeThreeEt.selectAll();
return true;
}
}
return false;
}
});
mCodeOneEt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (mCodeOneEt.getText().toString().length() > 0) {
mCodeTwoEt.requestFocus();
}
}
});
mCodeTwoEt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (mCodeTwoEt.getText().toString().length() > 0) {
mCodeThreeEt.requestFocus();
}
}
});
mCodeThreeEt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (mCodeThreeEt.getText().toString().length() > 0) {
mCodeFourEt.requestFocus();
}
}
});
mCodeFourEt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Now here is the custom edittext class
public class CustomEditText extends android.support.v7.widget.AppCompatEditText {
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
true);
}
private class ZanyInputConnection extends InputConnectionWrapper {
public ZanyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
#Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// Un-comment if you wish to cancel the backspace:
// return false;
}
return super.sendKeyEvent(event);
}
#Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
Just use this custom class in your XML and see the example above which is for 4 edit text.

Related

set button active, when two editTexts are not empty

I want to set my button active, only when two EditTexts are not empty. I try to use TextWatcher for that, but it doesn't really work fine - button stays active, even if erased all text from price input. Here is my code:
final EditText titleEdit = findViewById(R.id.name);
final EditText priceEdit = findViewById(R.id.price);
final Button addButton = findViewById(R.id.add);
titleEdit.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 (!TextUtils.isEmpty(titleEdit.getText().toString().trim()) && !TextUtils.isEmpty(priceEdit.getText().toString().trim()))
addButton.setEnabled(true);
else addButton.setEnabled(false);
}
#Override
public void afterTextChanged(Editable s) {
}
});
I tried few solutions from Stackoverflow, but it's still the same. What should I change in my code?
Define the following variable as global variable
Boolean text1= false;
Boolean text2 = false;
Complete code
public class classname extends AppCompatActivity {
Boolean text1= false;
Boolean text2 = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Your code
titleEdit.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() > 0 ){
text1 = true;
if(text1 && text2){
addButton.setEnabled(true);
}
}else{
text1 = false;
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
priceEdit.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() >0 ){
text2 = true;
if(text1 && text2){
addButton.setEnabled(true);
}
}else{
text2 = false;
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
You should create Both EditText section TextWatcher.
Check if (s.length()>0))
Try with
titleEdit.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()>0))
{
addButton.setEnabled(true);
}
else
{
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
priceEdit.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()>0))
{
addButton.setEnabled(true);
}
else
{
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});

Android EditText - how to detect if something is written by SoftKey?

I want to detect if something put into EditText was put there using SoftKey.
I've overridden dispatchKeyEvent but it doesn't work ...
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.e("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
Find the solution
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
// like this you can detect
}
return super.onKeyDown(keyCode, event);
}
yourEditText.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.toString().isEmpty()){
//your text is not empty
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};

I am using one edittext which is blank already for that i want to know some listener

I have a blank/empty edittext i want to know the "backspace listener" or "count zero listener" for now am using addTextChangedListener() but none of the method is getting executed when am pressing backSpace key because that is already empty.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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() != 0)
Field2.setText("");
}
});
I think you use TextWatcher
mEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL){
//on backspace
}
return false
}
});

onTextChanged event in Android

My problem is little tangled, i have to EditText boxes. Now, when i typing something on first EditText then its shows on second EditText. And as vice versa while typing on second EditText it sets on first EditText. But it gives some error on setText of any one of EditText. Below is my code,
Boolean flag,flag2;
txt_editText_1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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() != 0)
{
flag=true;
flag2=false;
if(flag==true)
{
String result=txt_editText_1.getText().toString().trim();
txt_editText_2.setText(""+result);
}
}
}
});
txt_editText_2.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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() != 0)
{
flag=false;
flag2=true;
if(flag2==true)
{
String result=txt_editText_2.getText().toString().trim();
txt_editText_1.setText(""+result);
}
}
}
});
But its not working. So, can anyone give suggestion.
I found my answer. I have put touch event on EditText for maintain flag(Boolean) status.
Boolean flag,flag2;
txt_editText_1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
{
flag=true;
flag2=false;
return true;
}
}
});
txt_editText_2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
{
flag=false;
flag2=true;
return true;
}
}
});
txt_editText_1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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() != 0)
{
if(flag==true)
{
String result=txt_editText_1.getText().toString().trim();
txt_editText_2.setText(""+result);
}
}
}
});
txt_editText_2.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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() != 0)
{
if(flag2==true)
{
String result=txt_editText_2.getText().toString().trim();
txt_editText_1.setText(""+result);
}
}
}
});

How do listen EditText?

I have a EditText. I wamt tp do something, when the user presses the Enter key while changing EditText. How can I do that?
The most simplest method:
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
sample code for text watcher
your_edittext.addTextChangedListener(new InputValidator());
private class InputValidator implements TextWatcher {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
}
First, create an OnEditorActionListener (as a private instance variable, for example):
private TextView.OnEditorActionListener mEnterListener =
new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) {
/* If the action is a key-up event on the return key, do something */
}
return true;
});
Then, set the listener (i.e. in your onCreate method):
EditText mEditText = (EditText) findViewById(...);
mEditText.setOnEditorActionListener(mEnterListener);
Another alternative is:
your_edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
You need to use TextWatcher for this purpose. Here is an example on how to work with TextWatcher and Android textwatcher API.
your_edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});

Categories

Resources