EditText requestfocus until valid entry

android edittext example
android edittext material design
different types of edittext in android
edittext android:style
android edittext inputtype
edittext input to textview output in android studio
android edittext hint
android edittext box style

I am using the following code. Here what I need is , the edittext1 should be focused until user makes valid entry and it can pass on next edittext2 request focus. Where I have mistake here.

 public void afterTextChanged(Editable s) {
            if(editext1.getText().toString().length()==2)
            {
                if(Integer.parseInt(editext1.getText().toString()) >= 51)
                {
                    Toast.makeText(getApplicationContext(),"Error: It shouldn't be above 50",Toast.LENGTH_SHORT).show();
                    editext1.setError("Please Check");
                    editext1.requestFocus();
                }
            }
            else
            return;
            editext2.requestFocus();
     }

Here edittext1 allowed value 0 to 50. If user enter above 50 it should retain there. If they enter between 0 and 50 then it should be requestfocus(); on next edittext1. But this above code jumped to the next editext2 even entered above 51

Notice below lines:

else
return; //already returned before editext2.requestFocus();comment or delete this line.
editext2.requestFocus();

Edited:

            if(editext1.getText().toString().length()==2)
            {
                if(Integer.parseInt(editext1.getText().toString()) >= 51)
                {
                    Toast.makeText(getApplicationContext(),"Error: It shouldn't be above 50",Toast.LENGTH_SHORT).show();
                    editext1.setError("Please Check");
                    editext1.requestFocus();
                }
                else{
                    editext2.requestFocus();
                }
            }

EditText requestfocus until valid entry, Notice below lines: else return; //already returned before editext2.requestFocus();​comment or delete this line. editext2.requestFocus();. Edited: I have an EditText and a Button.On click of the button i want to open the EditText keyboard and at the same time request focus on the EditText, So that the user directly start typing in the keyboard and text appears in the EditText.

There is an issue when you call requestFocus(). It should be called as

 public void afterTextChanged(Editable s) {
    if (editext1.getText().toString().length() == 2) {
        if (Integer.parseInt(editext1.getText().toString()) <= 50) {
            editext2.requestFocus();
            return;
        } else {
            Toast.makeText(getApplicationContext(), "Error: It shouldn't be above 50", Toast.LENGTH_SHORT).show();
            editext1.setError("Please Check");
            editext1.requestFocus();
        }
    }
}

How to make EditText not focused when creating Activity, How do you make EditText not focus when creating activity? Add Requestfocus on EditText programmatically android Juned Mughal December 15, 2015 March 29, 2016 Android Examples Tutorials Change editText box selection on button click handling using Requestfocus function through programming file.

You need to handle inner if too. so return from inner if the condition is true

public void afterTextChanged(Editable s) {
            if(editext1.getText().toString().length()==2)
            {
                if(Integer.parseInt(editext1.getText().toString()) >= 51)
                {
                    Toast.makeText(getApplicationContext(),"Error: It shouldn't be above 50",Toast.LENGTH_SHORT).show();
                    editext1.setError("Please Check");
                    editext1.requestFocus();
                    return; //Add this line only
                }
            }
            else
            return;
            editext2.requestFocus();
     }

How can I detect focused EditText in android?, How do I change the focus to next text in Android? I have a problem: My Layout is simple only an editText and a listView (both of them is seperated). Not like other cases in other thread, my editText location is above of listView. And now i have

Android Disable EditText from Auto Focus (Keyboard) on Activity , When you define an edit text widget, you must specify the R.styleable.​TextView_inputType attribute. For example, for plain text input set  Show soft keyboard automatically when EditText receives focus Posted by David Chandler on May 2, 2012 I’ve been unpleasantly surprised at how difficult it is to find info on showing or hiding the Android soft keyboard, so I’m putting a few code snippets out here for posterity.

Set next EditText focused and editable on KEY_DOWN, Choosing the input type configures the keyboard type that is shown, acceptable characters, and appearance of the edit text. For example, if you want to accept a  Application developer can add automatic selection Requestfocus by implementing <requestFocus /> tag inside layout xml file editText which user wants to selected automatically on activity start time. Code for MainActivity.java file.

EditText, Android EditText is simply like a text field, text area or a text input field in online EditText widget that automatically formats input number valid decimal number I tried developing before in Android but I did not like the experience in using it (​for example, a child EditText would request focus and open the soft keyboard to  I read around about the questions on how to set an object to be focused however I cannot seem to find either an answer to what I am trying to do. Using the On Focus Listener I have done the follow

Comments
  • Thank You. So much... As I need.
  • No Still now it skip into next ( edittext2) even entered above 51
  • When I enter a single digit it skipped into next.
  • Thank You @Syed Sumair . Now it works.. I missed this way.
  • There was an issue in my logic for length condition, I've again edited my answer hope it works for your case