Keyboard remain opens when Toolbar back arrow is pressed - android

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

Related

How to hide soft keyboard without losing focus on searchview

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

How to close a SearchView programmatically on back pressed?

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

Hide Keyboard when select item from spinner in DialogFragment

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

android actionbar sherlock collospe and expand

I have the following code:
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
My issues is following:
I have an search icon in the ActionBar. When I tap on the search icon an edittext is opened and cancel button shown. The search is working properly. Now, when I click the cancel button I want to hide the edittext and cancel button. How can I achieve this?
I solve my problem... using
item.collapseActionView();
Thanks for reply and hope it this will help others..
Here is my code where i have used it.....
private EditText search;
private Button search_cancle;
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if(id==R.id.action_search){
View v = (View) item.getActionView();
search = ( EditText ) v.findViewById(R.id.txt_search);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
search_cancle=(Button)v.findViewById(R.id.search_cancel);
search_cancle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
item.collapseActionView(); // Here I have set it.....
search.setText("");
}
});
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}

navigation drawer hide keyboard when onDrawerOpened

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

Categories

Resources