Show soft keyboard for EditText when Fragment starts

At times When app screen starts, we may need to show Soft keyboard Popup for the EditText, which having focus. So user don’t need to touch on the EditText to show keyboard.

show soft keyboard

To show keyboard for EditText view when Activity starts. We can just implement in XML design file in EditText tag by using <requestFocus />. see below

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="15">
<requestFocus/>
</EditText>

But the above XML code not works when using in Fragment, to show keyboard when Fragment screen starts.

Solution to show soft keyboard

We need to handle in code to force show keyboard. Following code works fine..

/**
* TODO showInputMethod
*/
public void showInputMethod() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}

Call this method in Fragment OnCreateView(…). Now It will show the softkeyboard.

And You can hide the Softkeyboard, when user touched on any other place than the EditText in Activity screen, using following code (place this code in Activity)

  /* (non-Javadoc)
* @see android.app.Activity#dispatchTouchEvent(android.view.MotionEvent)
*/
  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
      View v = getCurrentFocus();
      boolean ret = super.dispatchTouchEvent(event);
      if (this instanceof SkylineMessagesActivityAdv) {
          return false;
      }
      if (v instanceof EditText) {
          View w = getCurrentFocus();
          int scrCoords[] = new int[2];
          w.getLocationOnScreen(scrCoords);
          float x = event.getRawX() + w.getLeft() - scrCoords[0];
          float y = event.getRawY() + w.getTop() - scrCoords[1];
          if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
              InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
          }
      }
      return ret;
  }

Hope it helps somebody..

Cheers 🙂

You may be also interested in

 

Display Menu items in ActionBar when using Fragment

Display Menu items in Fragment ActionBar

When Implementing Fragment screen, If it require to display Menu Items only specific to that Fragment screen.

If you just write the following code…

       
        @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		menu.clear();
		inflater.inflate(R.menu.done_actions_menu, menu);
	}

	/**
	 * On selecting action bar icons
	 * */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Take appropriate action for each action item click
		switch (item.getItemId()) {
		case R.id.action_done:
			Log.d(TAG, "Done button clicked...");
                        //action code
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

It won’t display Menu items in ActionBar.

To display Menu Items you should also override OnCreate method of Fragment class as shown below

     
       @Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

	}

Hope it helps somebody…

cheers  🙂