hiding keyboard issue - android

I am using the following code to hide the keyboard when a button is clicked:
private OnClickListener saveButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
final View activityRootView = findViewById(R.id.myProfileDetails);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
});
//other code that does something
}
}
The button also does some other things but none of it is related to the keyboard and on pressing the button, the activity does not change.
I also have two EditText fields in my activity. When I am using the application and i tap on either field, they gain focus and the keyboard appears. When I press the button, the keyboard disappears and the other code is executed exactly as it should be. In this instance, everything is perfect.
The problem arises when tap on either EditText field for the second time. Now, the EditText gains focus but the keyboard appears and disappears almost instantly without me doing anything. I am guessing my code makes the keyboard disappear permanently after the first time I click the button. Why is this happening and how can I correct this?

you are initializing a ClickListener in your OnClick. which will hide the keyboard as soon as heightDiff>100. Do not do this.
do like this
private OnClickListener saveButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
editText.getWindowToken(), 0);
//other code that does something
}
}

Related

close listener of searchview in layout is not working Android

I am building android Application in which i have embedded a searchview below toolbar. See Screenshot. But when i click on the search icon my keyboard appears but when i click close icon of searchview, keyboard does not disappear. Close listener is not working and action expand or collapse listener can not be used because it is not a menuitem. So how should i disappear the keyboard. Please guide.
simpleSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
hideKeyboard(getActivity());
return false;
}
});
You can do this by getting a reference to the [x] button, then setting an onClick listener on it. In the onClickListener, you could add logic to hide the keyboard.
The code below was obtained from this answer
// Catch event on [x] button inside search view
int searchCloseButtonId = searchView.getContext().getResources()
.getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) this.searchView.findViewById(searchCloseButtonId);
// Set on click listener
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Manage this event.
}
});
Inside onClick(View v) you can call a method to hide the keyboard like this
private void hideKeyboard(){
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Android show view and hide keyboard at same time. Weird behaviour

I have a custom view that could show a view in same space that should be soft keyboard native for android.
I need to having the keyboard opened, click in a button, hide the keyboard and shows other view in same place that keyboard be/was.
I have that implemented right now just with a hide keyboard and show custom view but has a weird behavior and min lag and overlapping.
Has someone implemented a similar stuff?
I have checked the Github project and found the bug and I have fixed that bug with the following code:
if (isRedPanelVisible()) {
showRedPanel(false);
showKeyboard(true, new KeyboardCallback() {
#Override
public void onKeyboardDone(boolean isVisible) {
}
});
}
if (KeyboardVisibilityEvent.isKeyboardVisible(TestActivity.this)) {
hideKeyboard(TestActivity.this);
new android.os.Handler().postDelayed(new Runnable() {
#Override
public void run() {
showRedPanel(true);
}
}, 100);
Note: You just have to put this in TestActivity.java under button's click event and Remove the previous code.
What I did
if your readPanel is visible then I called the showRedPanel to false and try to open the keyboard.
After that I have added a check for Keyboard's visibility event and if keyboard is visible I called hideKeyboard to make keyboard go away and call showReadPanel with true after a delay of 100 ms
Code: hideKeyboard
public void hideKeyboard(Activity activity) {
// Check if no view has focus:
try {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
}
}
So what happens in your code is that:
Tell system to close the keyboard -> Show red panel with a small delay -> Red panel is shown before keyboard closing -> Since keyboard mode is in adjustResize the red panel shown above keyboard -> Keyboard get closed -> Everything in place
Try to change windowSoftInputMode in manifest from adjustResize to adjustNothing.
Sadly keyboard in android doesn't work smoothly like in IOS, keyboard is handled by OS means you no control over it size, opening/closing animation and no callback! So the best way is to always show red panel and when needed Open keyboard on top of it.
se the following functions to show/hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}

How to dismiss android keyboard on dialog dismiss?

I have a dialog box open in my android app, and I have a button when clicked will dismiss the dialog box. The problem is there is also a textedit field, and if its focused and the keyboard is showing, then when I click the cancel button, then dialog goes away, but the keyboard is still showing.
I want to also dismiss the keyboard.
I was searching around, and for threads like this
Hide soft keyboard after dialog dismiss
But none of the solutions worked for me. By the way the edittext is a number input type, if that makes a difference somehow.
Does anyone know how to fix this?
Thanks
public void HandleTeamManagement() {
final Dialog teamDialog = new Dialog(this);
teamDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
teamDialog.setContentView(R.layout.dialog_team_management);
final EditText mergeNum = (EditText) teamDialog.findViewById(R.id.group);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mergeNum.getWindowToken(), 0);
// Setting Negative "NO" Button
Button cancelButton = (Button) teamDialog.findViewById(R.id.cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
teamDialog.dismiss();
}
});
// Showing Alert Dialog
teamDialog.show();
}
You can find a solution here:
http://www.workingfromhere.com/blog/2011/04/27/close-hide-the-android-soft-keyboard/
Close/hide the Android Soft Keyboard
Edited: adding code
Try this ..it worked for me
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if (editText!= null && getActivity() != null) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
editText.getWindowToken(), 0);
}
}
}, 1000);

Android: How to open keyboard for editing EditText when click button?

my case is: I have one EditText field that is with disabled focus.
Beside EditText field I has two buttons for Input methods. So I want when click first button: to open soft keybord and edit text in EditText field. I try many ways with:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
and doesn't work for me. Only way to open soft keyboard is:
toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
but there is no way to edit info from EditText field.
May you suggest me how to open keyboard and edit text for some EditText when click button.
Thanks a lot!
Edited:
So, EditText is not focusable be default. When I click Keyboard button - should be focusable, then show me soft keyboard to enter text and appear in EditText. Other method to insert is A-B-C button which not required keyboard. It will be something like Morse code input - touch and hold A-B-C button :) I'll try suggested example to implement in my case. Thank you guys :)
Thanks guys for your help :) I used all suggestions that you gave me, searched and tested a lot of other scripts and finally my code is working :)
Here is my final code:
InputEditText = (EditText) findViewById(R.id.InputText);
public void InputData() {
/* Keyboard Button Action */
KeyboardButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "On Keyboard Button click event!");
InputEditText.requestFocus();
InputEditText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
}
});
}
It may be useful for someone :)
Thank you!
The design you've described isn't recommended. You're violating the focusable attribute's purpose which is not to control whether the user can alter the text in a EditText component.
If you plan to provide an alternative input method because the use case seems to require this (e.g. you allow only a certain set of symbols in the editable text field) then you should probably disable text editing altogether for the time the user isn't allowed to change the value of the field.
Declare your editable field:
<EditText
android:id="#+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
notice that its focusable attribute is left with the default value. That's ok, we'll handle that later. Declare a button which will start editing process:
<Button
android:id="#+id/button_show_ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start editing" />
Now, in your Activity declare:
private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;
And in onCreate() do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ime_activity);
// Find out our editable field.
editText2 = (EditText)findViewById(R.id.edit_text_2);
// Save its key listener which makes it editable.
originalKeyListener = editText2.getKeyListener();
// Set it to null - this will make the field non-editable
editText2.setKeyListener(null);
// Find the button which will start editing process.
buttonShowIme = (Button)findViewById(R.id.button_show_ime);
// Attach an on-click listener.
buttonShowIme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Restore key listener - this will make the field editable again.
editText2.setKeyListener(originalKeyListener);
// Focus the field.
editText2.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
}
});
// We also want to disable editing when the user exits the field.
// This will make the button the only non-programmatic way of editing it.
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// If it loses focus...
if (!hasFocus) {
// Hide soft keyboard.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
// Make it non-editable again.
editText2.setKeyListener(null);
}
}
});
}
I hope the code with all the comments is self-explanatory. Tested on APIs 8 and 17.
Try this :
final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);
Button btsmall = (Button) findViewById(R.id.BtSmall);
btsmall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myedit2.requestFocus();
}
});
I Have Work this code for open a keybord when button click.
Like .
btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edt1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
}
});
its completed work.
LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
ll_about_me.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mEtEmpAboutYou.requestFocus();
mEtEmpAboutYou.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEtEmpAboutYou, InputMethodManager.SHOW_FORCED);
return true;
}
});
For those that uses fragments you can use InputMethodManager that way:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
Full Code:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
});
I hope this help
EditText txt_categorie = findViewById(R.id.txt_categorie);
txt_categorie.requestFocus();

Keyboard not shown when i click on edittextview in android?

When i click on the edittextview then some times keyboard shown or some times keyboard are not shown.
In android 2.1 it show the keyboard when i click on the edittextview
but when i start same application it on android 2.2 then it not show the keyboard.
Help me how to show that problem.
OK, This might be a late response, but it worked.
I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).
I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.
From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).
so call the function below on your EditText when it is brought to front:
mEditText.clearFocus();
or
parentViewThatContainsEditTextView.clearFocus();
I had a similar problem on Galaxy S3 (displaying EditText controls on a PopupWindow - the keyboard was never showing). This solved my issue:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
I didn't want to EditText lose a focus using editText.clearFocus(). Came up to this solution.
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
here's a possible solution:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
code is based on the next link:
http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
In my case it was in a PopupWindow and I simply needed to call popupWindow.setFocusable(true)
I had this same problem when displaying an EditText in a DialogFragment. Despite the EditText getting focus (i.e., when clicked, it showed the flashing caret), the keyboard did not display.
My solution was to add a dummy EditText to the uppermost view of my DialogFragment.
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
Possible scenarios:
1) On clicking the EditText, usually the keyboard comes up. But if you press the back key button in the emulator the keyboard (not the screen keyboard) dimisses.
2) In code you can disable the keyboard on clicking the EditText by setting a flag.
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
It works like a charm, In a case if you even want to hide on click of the edittextView.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
In your parent view check if there is android:descendantFocusability="blocksDescendants"
remove it.

Categories

Resources