Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide the Android Soft Keyboard in dialog?

Tags:

android

In my application custom dialog is in BaseExpandableListAdapter class. In dialog I have two edit text. First is name and its mandatory. And second is address its optional. And two buttons OK and cancel. When Dialog shows I want to show keyboard with request focus for edit text name. After clicking of OK button Soft Keyboard should get hide.

like image 980
user2799407 Avatar asked Mar 12 '14 06:03

user2799407


People also ask

How do I make my keyboard invisible on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

How do I hide the soft keyboard on Android after clicking outside EditText?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


2 Answers

on click of ok button write the below code:-

  final Dialog dialog=new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    final EditText text = (EditText) dialog.findViewById(R.id.nameField);

   Button mOkBtn=(Button)dialog.findViewById(R.id.okBtn);


    // if button is clicked, close the custom dialog
   mOkBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            InputMethodManager imm = (InputMethodManager)getSystemService(context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
        }
    });

    dialog.show();

Define context as Context context=this.

like image 66
swati Avatar answered Sep 21 '22 17:09

swati


final Dialog dialog = new Dialog(_context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.setContentView(R.layout.prompts);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

final EditText name = (EditText) dialog.findViewById(R.id.name);
final EditText add = (EditText) dialog.findViewById(R.id.add);

Button btnok = (Button) dialog.findViewById(R.id.btn_ok);
Button btncancel = (Button) dialog.findViewById(R.id.btn_cancel);

 btnAddExpList.setOnClickListener(new OnClickListener() {
  @Override
   public void onClick(View v) {           dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
   }
 }
like image 41
user2799407 Avatar answered Sep 20 '22 17:09

user2799407