Android: Enter key move to next EditText - android

I have an anctivity with 6 EditText... the activity is working good, but I need to click on each EditText to fill it...
How can I make enter key change the focus to another EditText?
What I've already tried
1. I already setted OnKeyListener on every EditText.
switch (v.getId()) {
case R.id.editText1:
activity.findViewById(R.id.editText2).requestFocus();
return true;
case R.id.editText2:
activity.findViewById(R.id.editText3).requestFocus();
return true;
case R.id.editText3:
activity.findViewById(R.id.editText4).requestFocus();
return true;
.
.
.
}
But, when I click on enter key, before the focus change to another EditText, a new line is create on previous EditText.
2. I've add android:singleLine="true" to every EditText. It isn't work on EditText with inputType="number"

You can add imeOptions attribute for EditText's in your xml:
android:imeOptions="actionNext"
But not forget to specify inputType:
For example, android:inputType="text"
For more details check this question - Move to another EditText when Soft Keyboard Next is clicked on Android

Try this code :
it working fine in otp screen i am using four edit text in this activity follow these step
i am using TextWatcher class like this
/*Textwatcher class*/
public class GenericTextWatcher implements TextWatcher {
private View view;
public GenericTextWatcher(View view) {
this.view = view;
}
#Override
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
String text = editable.toString();
switch(view.getId()) {
case R.id.edit_otp1:
if(text.length()==1)
edit_otp2.requestFocus();
break;
case R.id.edit_otp2:
if(text.length()==1)
edit_otp3.requestFocus();
break;
case R.id.edit_otp3:
if(text.length()==1)
edit_otp4.requestFocus();
break;
case R.id.edit_otp4:
break;
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
Use textwatcher in edittext otp activity like this
/*call textwatcher class*/
edit_otp1.addTextChangedListener(new GenericTextWatcher(edit_otp1));
edit_otp2.addTextChangedListener(new GenericTextWatcher(edit_otp2));
edit_otp3.addTextChangedListener(new GenericTextWatcher(edit_otp3));
edit_otp4.addTextChangedListener(new GenericTextWatcher(edit_otp4));
it works for me please try.

Related

How to check whether given text is valid or not using TextWatcher?

I want to implement the MPIN functionality in my mobile banking application..I have six edittext using text-watcher I'm doing this function.
Once i completed the 6 fields how to check whether that pin is correct or not automatically and how to show alert.
If Pin is correct how to move next screen automatically.
I stored the pin in sharedpreference using this value how to check condition.Where I have to check this condition. Please help me.
This is my code:
public class AboutUsActivity extends AppCompatActivity {
EditText t1,t2,t3,t4,t5,t6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
t1=(EditText) findViewById(R.id.first);
t2=(EditText) findViewById(R.id.second);
t3=(EditText) findViewById(R.id.third);
t4=(EditText) findViewById(R.id.fourth);
t5=(EditText) findViewById(R.id.fifth);
t6=(EditText) findViewById(R.id.sixth);
t1.addTextChangedListener(new GenericTextWatcher(t1));
t2.addTextChangedListener(new GenericTextWatcher(t2));
t3.addTextChangedListener(new GenericTextWatcher(t3));
t4.addTextChangedListener(new GenericTextWatcher(t4));
t5.addTextChangedListener(new GenericTextWatcher(t5));
t6.addTextChangedListener(new GenericTextWatcher(t6));
}
public class GenericTextWatcher implements TextWatcher
{
private View view;
private GenericTextWatcher(View view)
{
this.view = view;
}
#Override
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
Log.i("AA","aftertext--");
String text = editable.toString();
switch(view.getId())
{
case R.id.first:
if(text.length()==1)
t2.requestFocus();
if(text.length()==0)
t1.requestFocus();
break;
case R.id.second:
if(text.length()==1)
t3.requestFocus();
if(text.length()==0)
t1.requestFocus();
break;
case R.id.third:
if(text.length()==1)
t4.requestFocus();
if(text.length()==0)
t2.requestFocus();
break;
case R.id.fourth:
if(text.length()==1)
t5.requestFocus();
if(text.length()==0)
t3.requestFocus();
break;
case R.id.fifth:
if(text.length()==1)
t6.requestFocus();
if(text.length()==0)
t4.requestFocus();
break;
case R.id.sixth:
if(text.length()==0)
t5.requestFocus();
break;
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Log.i("AA","beforetext--");
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
Log.i("AA","textchanged--");
}
}
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(arg0.length() == 6)
{
// method to check if code is correct or not
}
}
or easy way to do this, use library:
https://github.com/mukeshsolanki/android-otpview-pinview
this will give you callback when user write 6 digit
private OtpView otpView;
otpView = findViewById(R.id.otp_view);
otpView.setListener(new OnOtpCompletionListener() {
#Override public void onOtpCompleted(String otp) {
// do Stuff
Log.d("onOtpCompleted=>", otp);
}
});

Can you help me with a listener?

I have a function of showDiaglog.
I just want to, when user tap checkbox the diaglog shown, and the spinner also, but I got a problem with listener. Everytime I open settings, the popup show up twice.
First because of listener of spinner then second because one of my setting checked.
I save every changes in that setting.
Can you help me?
selectLanguage
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
saveSettingan(Key_Select, position);
if (position == 0) {
setDefaultLocal();
} else {
setLocal("in");
}
showDialog();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
and this one
case R.id.checkAcak:
saveSettingan(Key_Acak, isChecked);
showDialog();
break;
You could store a boolean which remembers the visibility of the dialog. Your show dialog should include this:
if (isDialogVisible)
return;
isDialogVisible = true;
Then you set a dismiss listener to set isDialogVisible to false.

Hiding character in edittext

I have the following code
public class Answer extends Activity implements KeyListener{
EditText ed1;
Button b;
LinearLayout l;
int flag=0;
int f=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
b=(Button)findViewById(R.id.ans);
l=(LinearLayout)findViewById(R.id.An);
ed1.setKeyListener(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag==1)
l.setBackgroundColor(Color.BLACK);
}
});
}
public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
// TODO Auto-generated method stub
switch(arg2){
case KeyEvent.KEYCODE_A:
if(f==0){
ed1.setText("");
f++;
flag=1;
}
}
return false;
}
The problem with this code is that on pressing 'a' it is setting color but after that it doesn't show anything in the edittext - ie it is setting color but after that it is not showing anything in edittext.If we press 'a' followed by 'n' then it will hide both 'a' and 'n' but i want to hide 'a' only
If you are looking to remove all of the a's from your EditText every time that you type 'a', then ed1.setText(""); is the culprit (it's clearing your EditText value every time).
Try this edit in your switch case:
String currText = ed1.getText().toString();
ed1.setText(currText.replace("a", ""));

how to update textview after text changed in edittext field

I have this application where field 1 is edittext and field 2 is textfield.
I want to add date into textview depending on the data entered in edittext field. That is after entering the data in edittext field and when i move to next edittext field the textview of 1st edittext field must be updated.
I know about add textchangedlistener but if it is the solution how am i supposed to implement it.
EDIT : I tried setonfocuschanged but it din work
k1_e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if((v_k1>=5.63)&&(v_k1<=11.25)){
k1_m.setText("mm");
}else if((v_k1>=30)&&(v_k1<=60)){
k1_m.setText("D");
}
}
});
k2_e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if((v_k2>=5.63)&&(v_k2<=11.25)){
k2_m.setText("mm");
}else if((v_k2>=30)&&(v_k1<=60)){
k2_m.setText("D");
}
}
});
edit 2 :
and details here
dr=Float.parseFloat(dr_e.getText().toString());
k1=Float.parseFloat(k1_e.getText().toString());
k2=Float.parseFloat(k2_e.getText().toString());
al=Float.parseFloat(al_e.getText().toString());
al_const=Float.parseFloat(alconst_e.getText().toString());
v_k1=Float.valueOf(k1);
v_k2=Float.valueOf(k2);
EDIT 3 :
I get data into edittext fields using
if(dr_e.getText().toString().length()==0|k1_e.getText().toString().length()==0|k2_e.getText().toString().length()==0|al_e.getText().toString().length()==0|alconst_e.getText().toString().length()==0){
flag=1;
}else{
dr=Float.parseFloat(dr_e.getText().toString());
al=Float.parseFloat(al_e.getText().toString());
al_const=Float.parseFloat(alconst_e.getText().toString());
}
Try this
you can use onFocusChangeListener on youredittext
k1_e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View arg0, boolean arg1) {
String data=k1_e.getText().toString().trim();
if(data.length!=0)
{
k1=Float.parseFloat(data);
v_k1=Float.valueOf(k1);
if((v_k2>=5.63)&&(v_k2<=11.25)){
k2_m.setText("mm");
}else if((v_k2>=30)&&(v_k1<=60)){
k2_m.setText("D");
}
}
}
});

Android check spaces in EditText

I have a problem regarding Edit Text in Android.
I have a field named Username :
I want that whenever someone writes a username with a space e.g "Gaurav Arora". Then it should raise a toast or error on the login button press.
I did in this way - I simply stopped the effect of space bar with the help of a text watcher as-
public static TextWatcher getNameTextWatcher(final TextView agrTextView) {
TextWatcher mTextWatcherName = new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void afterTextChanged(Editable arg0) {
String result = agrTextView.getText().toString().replaceAll(" ", "");
if (!agrTextView.getText().toString().equals(result)) {
agrTextView.setText(result);
}
}
}
But this has not solved my purpose. I just want to implement whenever the person uses a username with space it should accept the space but on the press of login button it should raise a toast
Thanks
Just check if your edit text contains blank space or not.
Use the following code in the onClick() event of the Button.
if (agrTextView.getText().toString().contains(" ")) {
agrTextView.setError("No Spaces Allowed");
Toast.makeText(MyActivity.this, "No Spaces Allowed", 5000).show();
}
Try this..
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String result = agrTextView.getText().toString();
if(result.contains ("\\s"))
Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
}
});
Simply do this:
TextView.getText().toString().contains(" ");
String question = txtNote.getText().toString();
if(question.trim().length() > 0){
Log.i("LOG", "Has value");
}else{
Log.i("LOG", "don't has value");
}
use trim() method to remove all spaces and then check for empty
if ( !edtText.getText().toString().trim().isEmpty()) {
//the EditText is not Empty
}
Also you can block entering space bar in username, or can give toast or snack on entering space in username .
Can refer this link might be helpful for you Block entering space in edittext.
Let me know if any concern.

Categories

Resources