I am trying to hide keyboard after selecting an item from Spinner but the code is not working and nothing happens. But in other side the same code works in normal fragment.
Here is the method to hide keyboard:
public static void hideKeypad(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
You need to specify the rootView of the Fragment because in your method it's see the getCurrentFocus() == null, So it will never go to the rest of the code.
This is the right code:
public static void hideKeypad(Activity activity, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Make a class variable View and equal it with the rootView of the Fragment in the onCreateView() and use this method anywhere in this Fragment.
This is working for me in a fragment
public void removePhoneKeypad() {
if(getActivity().getCurrentFocus()!=null &&getActivity().getCurrentFocus().getWindowToken() != null) {
System.out.println("getCurrentFocus() in frag");
InputMethodManager inputManager = (InputMethodManager) rootView
.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = getActivity().getCurrentFocus().getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Related
Expand searchview when fragment is oppened , request focus on searchview but hide soft keyboard .
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.barcode,menu);
MenuItem scan = menu.findItem(R.id.scanbarcode);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem scan = menu.findItem(R.id.scanbarcode);
mBarcode = (android.support.v7.widget.SearchView) scan.getActionView();
mBarcode.setQueryHint(getString(R.string.scanb));
scan.expandActionView();
mBarcode.setOnQueryTextListener(this);
}
write this in your activity in manifest file
android:windowSoftInputMode="stateHidden|adjustResize"
and in your fragment rite this
searchTditText.requestFocus();
Call this method to hide soft keyboard.
public static void hideSoftKeyboard(Context context, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Check this code:
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
I have the same question which I found here which I will re-iterate because the solution is not 100% exactly what I need:
I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expected. Clicking the "X" in the SearchView box closes the SearchView as expected. However, when the SearchView is activated and I press the "back" button, my app is exited. This is the correct behavior, but what I am trying to do now is to capture back button press and just have it close the SearchView (not my app) when the SearchView is visible. Is there a way to invoke the SearchView OnCloseListener() programmatically on a back button press? For example, something like this:
Here is the solution:
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
The problem is the solution requires the user to press back not once, but twice: once* to exit the keyboard and **once to close the SearchView.
How can I close the keyboard AND the SearchView at the same time by pressing back once?
edit:
Somone had a similar problem with EditText and the solution was to subclass the EditText view.
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
searchView.onActionViewCollapsed()
} else {
super.onBackPressed();
}
}
below method is Clear your text Only
searchView.setIconified(true);
below This methods is close your search view
searchView.onActionViewCollapsed()
try
#Override
public void onBackPressed() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
Add a method to close the keyboard within onBackPressed()
here is the code to hide keyboard.
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
}
Based on #Archana answer, the onBackPressed() should be like :
#Override
public void onBackPressed() {
hideSoftKeyboard();
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
private void hideSoftKeyboard(){
View view = activity.getCurrentFocus ();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
}
}
or you can also override the onKeyDown()
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
hideSoftKeyboard ();
if (! searchView.isIconified ()) {
searchView.setIconified (true);
}
return true;
}
return super.onKeyDown (keyCode, event);
}
private void hideSoftKeyboard() {
View view = activity.getCurrentFocus ();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
}
}
An interesting answer!
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
public class MainActivity extends AppCompatActivity {
MenuItem menuItemSearch;
#Override
protected void onResume() {
if(menuItemSearch!=null)
MenuItemCompat.collapseActionView(menuItemSearch); // while activity begins, searchView is closed if remained open.
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menuItemSearch = menu.findItem(R.id.menuSearch);
}
}
I did it like this in the activity where the SearchView is located
#Override
public void onBackPressed() {
super.onBackPressed();
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
}
Instead of hide soft keyboard manually, try clearFocus() method
mSearchView.setIconified(true);
mSearchView.clearFocus();
I am following below code:-
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish();
overridePendingTransition(R.transition.right_in, R.transition.right_out);
}
return super.onOptionsItemSelected(item);
}
In this when my keyboard is open and I press my toolbar back arrrow ,the keyboard remain open and activity got finish. I have tried forcefully hiding keyboard in on pause() by callling below method but doesn't look good while transition :-
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Try to put in your toolbar back button this code:
//Hide keyboard when button was clicked.
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish();
overridePendingTransition(R.transition.right_in, R.transition.right_out);
}
//Hide keyboard when button was clicked.
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
return super.onOptionsItemSelected(item);
}
Why are you getting android home button with onOptionsItemSelected() ?
Just do like above :
toolbar.setNavigationOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
main = new Intent(SettingActivity.this, MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
finish();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
startActivity(main);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
Hope it will help you, Darkball60
I am using a search field in the actionbar by reffering it to an xml in the layout folder called search_layout.xml.In the 2.3.3 version of android, there is an issue like the search icon is not shown in the keypad for the first time after I enter the text. Instead I have to click to the "enter" key, then on the textfield and then only the "search" icon appears. This does not happen in higher versions like 4.0.4 and 4.4.2 of android.
Here is the search_layout.xml:
And this xml is reffererd to in res>>menu>>menu.xml
<item
android:id="#+id/action_notification"
android:actionLayout="#layout/search_layout"
android:icon="#drawable/ic_searchicon"
android:orderInCategory="0"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
This is the code snippet I am using for this in my activity called Activity_HomeScreen class.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem itemListMap,itemRefresh;
editsearch = (EditText) menu.findItem(R.id.action_notification).getActionView();
editsearch.addTextChangedListener(textWatcher);
editsearch.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH)
{
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
}
return false;
}
});
MenuItem menuSearch = menu.findItem(R.id.action_notification);
menuSearch.setOnActionExpandListener(new OnActionExpandListener()
{
// Menu Action Collapse
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
return true;
}
// Menu Action Expand
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Solved this issue by doing like this:
public boolean onMenuItemActionExpand(MenuItem item)
{
editsearch.clearFocus();
editsearch.post(new Runnable() {
#Override
public void run() {
editsearch.requestFocus();
final InputMethodManager imm = (InputMethodManager) Activity_Cat_Products.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editsearch, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}
I have a fragment with edittext in it.
when i click the edittext, the keyboard show up.
the problem is when i open the drawer, the drawer does not hide the keyboard.
the keyboard is still showing even i switch to another fragment.
How can i hide the keyboard when i open the drawer.
i try to put
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
and
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
both of it do not hide the keyboard.
Use this line of code before opening/closing the slide drawer:
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
Try this...
#Override
protected void onCreate(Bundle savedInstanceState) {
......
//Initialize
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
DrawerListerner:
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
Set drawer listener is already deprecated you can detect navigation drawer state change using mDrawerLayout.addDrawerListener() and close keyboard onDrawerStateChange
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
//Called when a drawer's position changes.
}
#Override
public void onDrawerOpened(View drawerView) {
//Called when a drawer has settled in a completely open state.
//The drawer is interactive at this point.
// If you have 2 drawers (left and right) you can distinguish
// them by using id of the drawerView. int id = drawerView.getId();
// id will be your layout's id: for example R.id.left_drawer
}
#Override
public void onDrawerClosed(View drawerView) {
// Called when a drawer has settled in a completely closed state.
}
#Override
public void onDrawerStateChanged(int newState) {
// Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
I create my own Helper class to show or hide keyboard.
Here it is...
public static class Helper {
public static void showKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}
}
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && activity.getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
Call Helper.showKeyboard(this) to show the keyboard.
Call Helper.hideKeyboard(this) to hide the keyboard.
this refers to Activity.
This is the simple best solution i've come up so far.All you gotta do is to create a InputMethodManager object. Add a listener to the DrawerLayout object and finally
add the line of code in the following methods inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0); and your good to go.
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(#NonNull View view, float v) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
#Override
public void onDrawerOpened(#NonNull View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
#Override
public void onDrawerClosed(#NonNull View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
#Override
public void onDrawerStateChanged(int i) {
}
});
The problem is that getWindowToken() must be called from the View that is currently "holding" the keyboard. It is very annoying, I agree with you, but that is how it works.
For example let's say EditText mEditText is the object currently in focus receiving the keyboard keystrokes. So then your code would be:
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
If You Used the toggle with drawer then add onDrawerStateChanged manually in your onCreate method
DrawerLayout drawer = findViewById(R.id.drawer_layout);
//ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, appToolBar, R.string.Open_Drawer, R.string.Close_Drawer);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, appToolBar, R.string.app_name, R.string.app_name) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
/* InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
*/ }
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
/* InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
*/ }
#Override
public void onDrawerStateChanged(int newState) {
// Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();