0

Here all thing is working fine but i want to hide keyboard when click anywhere outside edittext...

Here is the source:

screen = this;
    sp = new ServiceProxy();
    login_account=(Button)findViewById(R.id.login_button); 
    sign_up=(Button)findViewById(R.id.button_login_sign_up_link);
    login_username=(EditText)findViewById(R.id.login_user_name);
    login_pass=(EditText)findViewById(R.id.login_user_password);


/*  //this is the thing i want. means i want to hide keyboard onclick outside  edittext   
    relative_layout = (RelativeLayout)findViewById(R.id.login_account);
    relative_layout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Hide Keyboard
            InputMethodManager imm = (InputMethodManager) UsernameverifyActivity.screen.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
             return true;
        }
    });*/

    login_account.setOnClickListener(new OnClickListener() {
        //private Integer id = 0;

            @Override
            public void onClick(View v) {


                String log_username = login_username.getText().toString();
                String log_password = login_pass.getText().toString(); 
                if(log_username.matches("")){
                    MessageDialog.showMessage("Alert!",
                            "Please enter Username", LoginAccountActivity.screen);
                }
                else if(log_password.matches("")){
                    MessageDialog.showMessage("Alert",
                            "Please enter Password", LoginAccountActivity.screen);
                }
                else{

                    try {

                        String username = log_username;
                        String password = log_password;

                        System.out.println("password is: " + password);
                        requestFor = "login";
                        additionalData = new JSONObject();
                        additionalData.put("username", username); 
                        additionalData.put("password", password);
                        additionalData.put("is_active", "Y");

                        } catch (JSONException e) { 
                        e.printStackTrace();
                    }
                    hideSoftKeyboard();
                    PostTask pt = new PostTask();
                    pt.execute(screen);
                    pdialog = new ProgressDialog(LoginAccountActivity.this);
                    pdialog.setCancelable(false);
                    pdialog.setMessage("Please Wait...");
                    pdialog.show();
                } 

        }

    private void hideSoftKeyboard() {
                 if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(login_username.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(login_pass.getWindowToken(), 1);
                    }
            }
    });

    sign_up.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) { 
                    Intent i=new Intent(getApplicationContext(),CreateAccountActivity.class);
                    startActivity(i);
                    overridePendingTransition(R.anim.animation,R.anim.animation2);
                    finish();
                }

    });
}



private class PostTask extends AsyncTask<Activity, String, String>
 {

    @Override
    protected String doInBackground(Activity... params) {
         if(requestFor.equals("login"))
            return (sp.login(params[0]));
            //return true;
         else
          System.out.println("Else Case" +LoginAccountActivity.screen.requestFor);
        return "";
    }


    protected void onPostExecute(String result) {
         if (requestFor.equals("login")) {
             if (result.equals("true")) {
                 if(pdialog != null)
                 {
                     pdialog.setCancelable(true);
                     pdialog.dismiss();
                 }
                    Intent i=new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(i);
                    startActivity(i);
                    overridePendingTransition(R.anim.animation,R.anim.animation2);
                    finish();

                } 
             else{
                 pdialog.setCancelable(true);
                 pdialog.dismiss();
                    MessageDialog.showMessage("Alert",
                            "Incorrect Username or Password", LoginAccountActivity.screen);

                }
            } 

    }
 }  

Update:

I have the following error:

Null Pointer Exception in LOG internalPolicy.phonewindowDecorView

At this line on Text_chat Activity:

InputMethodManager imm = (InputMethodManager) Text_chat.screen.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
fllo
  • 11,595
  • 5
  • 39
  • 95
  • i have tried this... http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1 http://stackoverflow.com/questions/7200281/programatically-hide-show-android-soft-keyboard?rq=1 –  Mar 24 '14 at 05:09
  • 1
    try [this](http://stackoverflow.com/questions/10550290/best-way-to-hide-keyboard-in-android) – user3355820 Mar 24 '14 at 05:20
  • 2
    try to hide keyboard inside onClick of your root layout. – Ketan Ahir Mar 24 '14 at 05:31

2 Answers2

1

clearFocus method with a "fake" container might be a solution.
You should create an empty view which is focusable like the following:

<LinearLayout
    android:id="@+id/container"
    ... >
    <LinearLayout
        android:layout_width="0dip" android:layout_height="0dip" 
        android:orientation="vertical"
        android:focusableInTouchMode="true" android:focusable="true" />
    <EditText
        ... >
    <EditText
        ... >
</LinearLayout>  

Then, put the following code in your onCreate method when you click outside in your layout:

// init your InputMethodManager outside onCreate method:
InputMethodManager mImm;

//...

// in onCreate:
mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);  
// on click event to hide the keyboard:
((LinearLayout) findViewById(R.id.container)).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(edittext1.hasFocus() || edittext2.hasFocus() || ...) 
            hideSoftKeyboard();
    }
});  

So hideSoftKeyboard method might be:

private void hideKeyboard() {
    mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    getActivity().getCurrentFocus().clearFocus();
}  

The clearFocus method when it's called, reset the focus on the first view which have focusable attributes set to true in your layout and hideSoftInputWindow with the second param set 0 force the keyboard to hide. So your EditText lose the focus and the SoftKeyboard disappears.
I found a similar trick here: How to hide soft keyboard on android after clicking outside EditText? (and another answer but I can't find it) and it works well!


Alternative: to reset the focus at the same time

private void hideKeyboard() {
    mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    // set the focus to the container:
    ((LinearLayout) findViewById(R.id.container)).requestFocus();
}

Let me know if this helps.

Community
  • 1
  • 1
fllo
  • 11,595
  • 5
  • 39
  • 95
0
try this one...    

put it below code in your activity in oncreate method and onpause, onresume.
if you want to hidden your softkeyboard try below lines in oncreate ,onpause,onresume in which are the activity you have to perform hidden keyboard.      
it will works..

this.getWindow().setSoftInputMode(
                        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

put .xml file in edittext properties like

android:focusableInTouchMode="true"

so call your hideSoftKeyboard() method in onCreate() in side..
like
    oncreate()
    {
    hideSoftKeyboard();
    } 

 thank you..
Sethu
  • 432
  • 2
  • 13