0

I've browsed similar questions and followed the suggestions there, but for the love of god, I can't get this to work, and it's driving me crazy. So here's the deal:

I have an editText, which needs to requestFocus at program startup, and pop the soft keyboard. If I put "android:windowSoftInputMode="stateVisible" in the Manifest, the keboard shows every time the activity starts. I only want it to show once with onCreate(), and when the user specifically clicks on the editText. My code for this is below:

EditText argument;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio_buttons);

    argument = (EditText) findViewById(R.id.editText_argument);
    InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(argument, InputMethodManager.SHOW_FORCED);

Q1) This code doesn't work. What am I doing wrong?

Q2) You see that I declared "EditText argument" outside of onCreate(), as I'd like to use this in the rest of the activity, not just within onCreate(). Is this good programming practice?

Q3) Then, when the user clicks done on the soft keyboard, I'd like this EditText to lose focus, i.e. the cursor should disappear. I understand that I need to have a dummy View to do this, but I still don't exactly understand how to switch focus to the dummy. How would I go about doing that?

Thanks so much in advance!

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
cartonn
  • 7,654
  • 3
  • 19
  • 19

1 Answers1

1

A1) You're missing a editText.requestFocus().

Refer: Soft Keyboard shows up on EditText focus ONLY once should help for dismissing soft keyboard.

A2) Yes, that's fine. Most of the UI elments should be declared at the class level scope and initialized in onCreate()

A3) A1's reference link should help you here.

Happy Coding!

EDIT:

onCreate():


EditText argument;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio_buttons);
    argument = (EditText) findViewById(R.id.editText_argument);

showKeyboard():


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

dismissKeyboard():


argument.requestFocus();
argument.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager keyboard = (InputMethodManager)                 getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(argument.getWindowToken(), 0);
    }
},200);
Community
  • 1
  • 1
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
  • I tried adding `editText.requestFocus()`, but it just doesn't work :( – cartonn Aug 04 '12 at 15:07
  • I get this error: "The method run() of type new Runnable(){} must override a superclass method". I ended up removing "@Override", and it works now. What was the override for? – cartonn Aug 04 '12 at 15:20
  • I'm really lost :( I removed the "@Override" and it worked. But are you saying that I shouldn't do that? – cartonn Aug 04 '12 at 15:23
  • my bad, you don't need to implement Runnable. Remove that. – Sagar Hatekar Aug 04 '12 at 16:55
  • Also, can you post what error do you get? The code snippets given to you have worked for others. So, you're definitely missing a step. – Sagar Hatekar Aug 04 '12 at 16:56
  • Are you sure your class extends an Activity? If not, you need to use the show and hide code snippets in your activity class. – Sagar Hatekar Aug 04 '12 at 16:58