224

I have an EditText-Field and set an OnFocusChangeListener for it. When it has lost focus, a method is called, which checks the value of the EditText with one in the database. If the return-value of the method is true, a toast is shown and the focus should get back on the EditText again. The focus should always get back on the EditText and the keyboard should show, until the return-value of the method is false.

EDIT: I think, I haven't made my real problem perfectly clear yet: No other Item on the Screen should be able to edit, until the value of the EditText is edited to a value, which makes the method "checkLiganame(liganame)" return false. Only the EditText-Field should be editable.

here is my code (which doesn't work for me):

final EditText Liganame = (EditText) findViewById(R.id.liganame);

    Liganame.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {

                String liganame = Liganame.getText().toString();


                if (checkLiganame(liganame)) {
                    Toast toast = Toast.makeText(CreateTableActivity.this,
                            "Dieser Liganame ist bereits vergeben",
                            Toast.LENGTH_SHORT);
                    toast.show();
                    Liganame.requestFocus();
                }
            }

and the method:

public boolean checkLiganame(String liganame) {
    boolean found = false;

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getReadableDatabase();

    Cursor cursor = db.query("liga", new String[] { "liganame" },
            "liganame = '" + liganame + "'", null, null, null, null);
    Log.i("Liganame: ", String.valueOf(cursor));

    db.close();
    if (cursor != null) {
        found = true;
    }

    return found;
}

This code leads to the following result: After the EditText has lost focus, the focus jumps back to EditText, but I can't edit the text anymore.

EDIT2: Changed my code. Scenario:

I click on the first EditText and put a String in it, which is already in the database. The toast is showing. Now I can't edit my String anymore. I click "next" on the keyboard and the focus stays on the first EditText. I try to edit my String, but nothing happens. Instead my new String is showing in the second EditText. I click on the back-arrow of my device and reclick on the first and second EditText --> no keyboard is showing.

Here is my new Code:

public class CreateTableActivity extends Activity implements
    OnFocusChangeListener {

private EditText Liganame, Mannschaftsanzahl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_league);

    Liganame = (EditText) findViewById(R.id.liganame);
    Liganame.setOnFocusChangeListener(this);
    Mannschaftsanzahl = (EditText) findViewById(R.id.mannschaftsanzahl);
    Mannschaftsanzahl.setOnFocusChangeListener(this);

    final Button save_button = (Button) findViewById(R.id.create_tabelle_speichern_button);

    OnClickListener mCorkyListener = new OnClickListener() {
        public void onClick(View v) {
            ButtonClick();
        }
    };
    save_button.setOnClickListener(mCorkyListener);



}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    String liganame = Liganame.getText().toString();

    if (checkLiganame(liganame)) {
        if (Liganame.requestFocus()) {
            getWindow()
                    .setSoftInputMode(
                            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            Mannschaftsanzahl.clearFocus();
            Toast.makeText(CreateTableActivity.this,
                    "Dieser Liganame ist bereits vergeben",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
Andreas Schneider
  • 2,855
  • 3
  • 18
  • 18

18 Answers18

368

Just put this line on your onCreate()

editText.requestFocus();
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
ralphgabb
  • 10,298
  • 3
  • 47
  • 56
  • It also automatically activates the opening of the virtual keyboard. So you don't need to do this in code. – user2342558 Sep 11 '20 at 09:10
  • 15
    it is not opening keyboard on my case. – Raman Joshi Oct 19 '20 at 06:38
  • Just add this lines below above code=> InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT); – HandyPawan Apr 02 '21 at 18:20
181

Requesting focus isn't enough to show the keyboard.

To get focus and show the keyboard you would write something like this:

if(myEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

EDIT: Adding extra info to the answer after the checkLiganame method was added.

In the checkLiganame method you check if the cursor is null. The cursor will always return an object, so the check for null doesn't do anything. However the problem is in the line db.close();

When you close the database connection, the Cursor becomes invalid and most probably is nulled.

So close the database after you've fetched the value.

Instead of checking the cursor for null, you should check if the number of rows returned are more than 0: if(cursor.getCount() > 0) and then set your boolean to true if so.

EDIT2: So here's some code for how to make it work. EDIT3: Sorry wrong code I added... ;S

First off, you need to clear focus if another EditText gets focus. This can be done with myEditText.clearFocus(). Then in your onFocusChangeListener you shouldn't really care if first EditText has focus or not, so the onFocusChangeListener could look something like this:

public class MainActivity extends Activity implements OnFocusChangeListener {
    private EditText editText1, editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setOnFocusChangeListener(this);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText2.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String liganame = editText1.getText().toString();

        if(liganame.length() == 0) {
            if(editText1.requestFocus()) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                editText2.clearFocus();
                Toast.makeText(MainActivity.this, "Dieser Liganame ist bereits vergeben", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Replace the first check if(liganame.length() == 0) with your own check, then it should work. Take note, that all the EditText views should have set their onFocusChangeListener to the same listener like I've done in the example.

Darwind
  • 7,284
  • 3
  • 49
  • 48
  • Thank you so far. But this doesn't solve my whole problem. I edited the description of my problem. – Andreas Schneider Jan 14 '13 at 22:47
  • So what is checkLiganame doing exactly? Why Can't you just check if(liganame.isEmpty()) and if it is true, requestFocus again? – Darwind Jan 14 '13 at 22:55
  • 1
    Thank you :-) Now my method is working correctly :-) but my main problem with the EditText-Field isn't solved yet. I still can't edit it, after the method has found a row... – Andreas Schneider Jan 15 '13 at 10:29
  • So you added if(myEditText.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } Inside the onFocusChanged listener? – Darwind Jan 15 '13 at 11:25
  • Yes I did. Under Liganame.requestFocus();. I click "next" on the keyboard, to jump in the next EditText-Field. The toast is showing and the focus is on BOTH EditText-Fields. But I can only edit the second one. The first one, which should be the only one having focus in this case, is not editable anymore. – Andreas Schneider Jan 15 '13 at 12:00
  • I've tried your new code. But it doesn't work :-( I've put an edit in my original post. – Andreas Schneider Jan 15 '13 at 21:04
  • Did you ever manage to solve this? as I have the exact same problem with my project and posted my problem here :http://stackoverflow.com/questions/14788512/how-to-force-focus-to-edit-text – TimCS Feb 10 '13 at 15:58
  • Regarding the DB-related advice in this answer, [this blog post](http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking) shows why you're probably better off with a single global DB connection owned by your Application subclass. I make that connection and all accessors static for ease of use. You SHOULD close the cursor instance when you're done with it. – William T. Mallard Jul 06 '14 at 17:51
  • thanks for taking the effort. This is what makes SO awesome :) – Dheeraj Bhaskar Oct 16 '15 at 07:43
70

Darwind code didn't show the keyboard.

This works for me:

        _searchText.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);

in case the keyboard is not showing, try to force:

        imm.showSoftInput(_searchText, InputMethodManager.SHOW_FORCED);
Groco
  • 1,301
  • 15
  • 19
24

This works from me:

public void showKeyboard(final EditText ettext){
    ettext.requestFocus();
    ettext.postDelayed(new Runnable(){
            @Override public void run(){
                InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(ettext,0);
            }
        }
        ,200);
}

To hide:

private void hideSoftKeyboard(EditText ettext){
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(ettext.getWindowToken(), 0);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Guru raj
  • 806
  • 11
  • 10
19

This changes the focus of the EditText when the button is clicked:

public class MainActivity extends Activity {
    private EditText e1,e2;
    private Button b1,b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);
        e1.requestFocus();
        b1=(Button) findViewById(R.id.one);
        b2=(Button) findViewById(R.id.two);
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e1.requestFocus();

            }
        });
        b2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e2.requestFocus();
            }
        });
    }
}
Cactus
  • 27,075
  • 9
  • 69
  • 149
Simon Chius
  • 476
  • 5
  • 18
15

This is what worked for me, sets focus and shows keyboard also

EditText userNameText = (EditText) findViewById(R.id.textViewUserNameText);
userNameText.setFocusable(true);
userNameText.setFocusableInTouchMode(true);
userNameText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(userNameText, InputMethodManager.SHOW_IMPLICIT);
Berni Gf
  • 161
  • 1
  • 5
  • 1
    Thanks. `setFocusable` requires API 26. Also `setFocusableInTouchMode` was not needed in my case. – CoolMind Sep 18 '18 at 13:43
8

If we create an EditText dynamically then we have to set the requestFocus() as given below.

    EditText editText = new EditText(this);
    editText.setWidth(600);
    editText.requestFocus();

If already we declared the component in the xml view then we have to find it and we can the focus as given below.

EditText e1=(EditText) findViewById(R.id.editText1);
e1.requestFocus();

It sets only focus to the corresponding EditText component.

Sudhakar
  • 3,104
  • 2
  • 27
  • 36
6
    mEditText.setFocusableInTouchMode(true);
    mEditText.requestFocus();

    if(mEditText.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
Barakzai
  • 351
  • 1
  • 3
  • 10
5

If you try to call requestFocus() before layout is inflated, it will return false. This code runs after layout get inflated. You don't need 200 ms delay as mentioned above.

editText.post(Runnable {
   if(editText.requestFocus()) {
       val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
       imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
   }
})
4
    Button btnClear = (Button) findViewById(R.id.btnClear);

    EditText editText1=(EditText) findViewById(R.id.editText2);
    EditText editText2=(EditText) findViewById(R.id.editText3);

    btnClear.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            editText1.setText("");
            editText2.setText("");

            editText1.requestFocus();
        }
    });
Hari Prasad
  • 603
  • 8
  • 18
Prince Dholakiya
  • 3,255
  • 27
  • 43
4

My answer here

As I read on the official document, I think this is the best answer, just pass the View to parameter such as your EditText, but showSoftKeyboard seems like not working on landscape

private fun showSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
}

private fun closeSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}
Thành Thỏ
  • 173
  • 1
  • 8
3
 private void requestFocus(View view) {
        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

//Function Call
requestFocus(yourEditetxt);
2

You could do this with a single line:

yourEditText.RequestFocusFromTouch();
M B
  • 2,326
  • 24
  • 33
2

For Xamarin.Android I have created this extension.

public static class ViewExtensions
{
    public static void FocusEditText(this EditText editText, Activity activity)
    {
        if (editText.RequestFocus())
        {
            InputMethodManager imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
            imm.ShowSoftInput(editText, ShowFlags.Implicit);
        }
    }
}
c.lamont.dev
  • 693
  • 1
  • 6
  • 14
2

If using requestFocus() in onCreate() introduces the issue of keyboard not showing on tap, use BindingAdapter using a SingleLiveEvent and request the focus inside that.

Here's how to do it:

BindingAdapter

@BindingAdapter("requestFocus")
fun bindRequestFocus(editText: EditText, event: Event<Boolean>?) {
    event?.getContentIfNotHandled()?.let {
        if (it) editText.requestFocus()
    }
}
Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
1

I Dont know if you alredy found a solution, but for your editing problem after requesting focus again:

Have you tried to the call the method selectAll() or setSelection(0) (if is emtpy) on your edittext1?

Please let me know if this helps, so i will edit my answer to a complete solution.

Undisputed
  • 311
  • 1
  • 7
1
new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
} 
1

please try this code on manifest

<activity android:name=".EditTextActivity" android:windowSoftInputMode="stateAlwaysVisible">
</activity>