0

I have a activity , in a activity one edit text having some hint text.

when i start activity by default focus gone on edit text and hint invisible.

please tell me how to make hint visible

Thank you

ajit
  • 27
  • 7
  • 2
    Possible duplicate of [Stop EditText from gaining focus at Activity startup](http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup) – Iulian Popescu Oct 19 '16 at 08:15

3 Answers3

1

Add in the page an empty view:

<LinearLayout
  android:focusable="true" 
  android:focusableInTouchMode="true"
  android:layout_width = "0dp"
  android:layout_height = "0dp"/>

This view will gain the focus and prevent EditText from getting it

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
1

Add this line in onCreate before setContentView()

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

also if you have requestFocus in xml part then remove it

Ganesh Pokale
  • 1,538
  • 1
  • 14
  • 28
0

After setContentView():

KeyListener keyListener = new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
setInput(YOUR_EDIT_TEXT, keyListener, imm);

setInput method:

private void setInput(EditText _editText, KeyListener keyListener, InputMethodManager imm){
_editText.setKeyListener(keyListener);
if (_editText.isFocusable()) {
    _editText.setFocusable(true);
    _editText.setFocusableInTouchMode(true);
    _editText.requestFocus();
}

_editText.setSelection(_editText.getText().length());
imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_editText, InputMethodManager.SHOW_IMPLICIT);
}
PLe
  • 137
  • 4