ajit
ajit

Reputation: 27

Edit text focus issue

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

Upvotes: 0

Views: 311

Answers (3)

Ganesh Pokale
Ganesh Pokale

Reputation: 1594

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

Upvotes: 1

PLe
PLe

Reputation: 137

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);
}

Upvotes: 0

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

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

Upvotes: 1

Related Questions