How to hide keypad in android after pressing home button - java

I am showing a list with messages and each message's row has a comment button.When i click on comment button opens a comment box with edit text and button for submitting the comment.When comment box appears on screen keypad also appears for entering text.If i pressed home button before entering text then application goes background but keypad remains on screen.This is the thing irritating me.For custom list i am using a custom adapter and code for comment box is written in that adapter.I tried using
inputmgr.hideSoftInputFromWindow(txtComments.getWindowToken(), 0);
but it is not working. So how i can hide this keypad programmatically.

Try using the code in https://stackoverflow.com/a/1109108/1904479. Hope you are not testing it in Android version 4.1.

please use this method to hide soft keyboard.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

KeyEvent.KEYCODE_HOME can NOT be intercepted. You can hide the keypad inputmgr.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); in onStop() method of your activity.
It does not require token from focused view editText.

Related

How to toggle between two keyboards (Google keyboard and Custom IME SoftKeyboard) by clicking on a button?

I have created a EditText,Custom IME SoftKeyboard and a button. I want that when my app opens and I click on EditText, it opens with my Custom IME Softkeyboard and when I click on the button, it switches to Default Keyboard. When I again click on the button, it switches back again to IME SoftKeyboard. So, Basically I am looking for a functionality of OnClick on a button that switches between Default Keyboard and Custom IME Softkeyboard.
I explored InputMethodManager but I only got the way to show the default keyboard.
mKeyboardSwitcher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
});
This is my code. Its just shows up the default keyboard.
I want the button to switch between Default keyboard and Custom Keyboard.

Make EditText not clickable, but still have focus

I have an EditText inside a CardView.
When the CardView is selected, I request focus for the EditText, see below:
cardProblem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//request focus
etProblem.requestFocus();
//display keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etProblem, InputMethodManager.SHOW_IMPLICIT);
}
});
The problem is that I don't want the EditText to be clickable (currently when I tap on the EditText it gets focus).
I understand that when I don't want it to be clickable, I can add:
android:clickable="false"
android:focusable="false"
But in this situation I can't set focusable to false because the EditText needs focus when I want to edit it.
Any ideas how I can resolve this?
I have resolve this, sort of...
Since I don't have time to waste, I went with the following:
I decided to set the visibility of EditText to invisible and when the CardView is selected I make the EditText visible then request focus.
Maybe it helps you:
create a fake EditText as a first child in your CardView and set the width and height to zero or set visibility to gone, so he will get the focus by default and the foucus in your main EditText will be removed and when you need to set focus to it just request the focus :
etProblem.requestFocus();

checking and unchecking a RadioButton upon clicking

I was having some problem when trying to check and uncheck radio button upon click.
What I am trying to do is, when the activity on load, the radio button is set to checked by default. When user pressed on the checked radio button, I set the radio button to uncheck and empty the textview. Then, when the user pressed on the radio button again, I set it to checked and set some text in textview. Here is my code:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if(radioButtonEmail.isChecked() == true){
radioButtonEmail.setChecked(false);
editTextEmail.setText("");
}else {
radioButtonEmail.setChecked(true);
editTextEmail.setText("TEST");
}
}
However, this only worked for the first time when I tried to uncheck the radio button. After I set it to false, when I try to check it again, it does not work. Any ideas? Thanks!
I don't have an exact explanation for the behavior you are seeing, but your logic looks off to me. I think you intend to do this:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if (radioButtonEmail.isChecked()) {
editTextEmail.setText("");
}
else {
editTextEmail.setText("TEST");
}
}
In other words, you don't manage whether the radio button is clicked from your event handler. Rather, you check for that state inside the handler, and then respond appropriately.

Soft KeyBoard does not show up in Landscape orientation

In my app, I have a dialog with EditText. I have coded it in such a way that as soon as the dialog shows up,the soft keyboard shows up, the edit text will get focus , all the existing text in the editText is preselected. And user can start typing. This all works fine when I am holding my phone in portrait orientation. But the same thing does not work when I have phone in landscape orientation. Part of my code in my fragment is pasted below.
final EditText inputDeviceId = new EditText(getActivity());
inputDeviceId.setInputType(InputType.TYPE_CLASS_TEXT);
String savedDevId = getDeviceID();
inputDeviceId.setText(savedDevId);
inputDeviceId.setSelectAllOnFocus(true);
// some more code ... to create dialog , ok cancel buttons then,
final AlertDialog alertDialog = alertDialogBuilder.create();
inputDeviceId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if(hasFocus){
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
alertDialog.show();
Now this all works in portrait orientation and does not work in landscape.
In landscape mode the dialog with edittext shows up , it has focus and existing text is preselected but it does not show the keyboard. User has to click in the editText for keyboard to come up. Why? I am using nexus-5 to test.

Can't get focus on editText when children are added in layout

i am adding a spinner, an image and an editText in a linearLayout everytime i click a button.
Now whenever i add this layout , editText shows blinking, means it has focus but the keyboard won't show up. Even i click on it, the keyboard won't show up. What most i can do is to click somewhere else and then back at the editText to make it show keyboard and proper focus.
I am using following code, how can i fix this bug.
viewHolder.title = (EditText) view.findViewById(R.id.AddNewDetail);
view.setTag(viewHolder);
layout.addView(view);
i think you should force the softkeyboard show.
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(editText,
InputMethodManager.SHOW_FORCED);
and close it
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),
0);
and focus on edittext
editText.requestFocus();
May you have added the below line in for your activity in manifeast file
android:windowSoftInputMode="stateHidden"
try removing it or you can manaually use the requestfocus method to get focus. try the below method.
edittext.requestFocus();

Categories

Resources