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