How can i hide android keyboard on navigation toggle click - android

I want to hide the keyboard on my android application when I click on the navigation button. I have searched for a solution but none worked for me.
NavigationDrawerActivity
public class NavigationDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_doctor) {
} else if (id == R.id.nav_about_us) {
}else if (id == R.id.nav_feedback) {
}
else if (id == R.id.nav_feedback) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
My application looks like this enter image description here

try this:
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
#Override
public void onDrawerStateChanged(int newState) {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);

Method 1 (if the input method has been shown in the window, the hidden, conversely display) InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
Method 2 (the view to accept the view of soft keyboard input, SHOW_FORCED said forced display)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);//Forced to hide the keyboard To obtain input method open state
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();

problem is occur you are using edit text when app run keyboard automatically open , to stop it you can add to your AndroidManifest.xml: Activity's name where edit text is used like you use edittext in mainActivity then you write this :- android:windowSoftInputMode="stateHidden" for other activity you can call it like above. and also add hide keyboard code after press backbutton your code is :
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Related

default back button in navigation drawer doesn't work

I have created an app which uses one activity (Navigation Drawer) and a fragment. But I'm unable to use toolbar back button to navigate back from the fragment to the main activity. Hardware back button works perfectly. I know that I need to override onOptionsItemSelected, catch android.R.id.home, check if there is something in the back stack and then pop it. After changing the fragment, "burger" button changes to "back arrow", but when I click on it the overridden method "onOptionsItemSelected" is never called, rather on click of the back button NavigationDrawer menu opens.
NOTE: I have referred many answers in the StackOverflow including THIS which is the same as that of my problem but that did not work for me. I have been working on this for a week any help is greatly appreciated. Please do not down vote. 1
Here is my code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void init() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
Log.d("Backstack Count", String.valueOf(getSupportFragmentManager().getBackStackEntryCount()));
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
showUpButton(false);
} else {
showUpButton(true);
}
}
});
}
private void showUpButton(boolean show) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disabled first, then enable - VERY VERY IMPORTANT.
if (show) {
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
} else {
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
invalidateOptionsMenu();
}
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// onBackPressed();
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
return true;
}
// return false;
// return super.onOptionsItemSelected(item);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
displayImportFrag();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void displayImportFrag() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Log.d("called", "new frag");
ImportFragment importFragment = new ImportFragment();
ft.add(R.id.fragment_frame, importFragment).addToBackStack(null).commit();
}
}
ImportFragment.java
public class ImportFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.import_fragment, container, false);
return rootView;
}
}

How to add image to popup window from URL in navigation drawer Activity

I have popup window implemented in my main navigation drawer activity. What i want is to get an image from URL, set that image in imageView in
activity_pop_up.xml, and show popup window in my main activity
Here is my MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// HERE IS POPUP WINDOW
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_pop_up, null);
pw = new PopupWindow(layout, 600, 800, true);
findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0);
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentMainPage pocetnaFragment = new FragmentMainPage();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.relativeLayoutZaFragment, pocetnaFragment, "1");
fragmentTransaction.commit();
getSupportActionBar().setCustomView(view,params);
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
return;
}
else if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Pritisnite još jednom za izlaz",
Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.podesavanja)
{
return true;
}
if (id == R.id.galerija)
{
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.pocetna)
{
FragmentMainPage fragmentMainPage = new FragmentMainPage() ;
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
, fragmentMainPage, fragmentMainPage.getTag()).addToBackStack(null).commit();
TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
Title.setText("Auto magazin");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_pop_up.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
<ImageView
android:id="#+id/baner3img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:srcCompat="#drawable/autojedan" />
</RelativeLayout>
#Nikola you can create a class which extends Dialog with a custom layout that is your activity_pop_up having imageView. You can set the requestWindowFeature(Window.FEATURE_NO_TITLE) that will make it look like a pop up. For image downloading you can use Picasso, Universal Image Loader or Glide and display it in your UI.

Navigation item's title disappeared when clicked

I have created drawer with navigation view. I have Navigation item's on which I am calling other activities.
The issue is when I click on navigation item, the other activity launches,and if I come back to main activity and open a drawer the clicked navigation item's title is disappeared only I can see the icon of the item.
code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.menu_icon);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer , toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer .addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_list) {
startActivity(new Intent(MainActivity.this, LaunchVenueServiceActivity.class));
// Handle the camera action
}
else if (id == R.id.nav_dashboard) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
else if (id == R.id.nav_config)
{
startActivity(new Intent(MainActivity.this,LaunchYourServiceStep2.class));
}
else if (id == R.id.nav_chat) {
} else if (id == R.id.nav_notes) {
startActivity(new Intent(MainActivity.this,NotesActivity.class));
} else if (id == R.id.nav_user_guide) {
}
else if(id == R.id.nav_log_out)
{
SharedPreferences.Editor editor = getSharedPreferences("username",MODE_PRIVATE).edit();
editor.remove("UserUsername");
editor.commit();
Intent intent1 = new Intent(MainActivity.this,LoginActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(intent1);
}
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
}
What can be the reason for this?? Can anyone help please? Thank you..
Got the solution.. Just tried to change the color of navigation item's text and it worked. Don't know why and how..
navigationView.setItemTextColor(ColorStateList.valueOf(Color.BLACK));
That may appear if you changed style of your layout in manifest. Check if you have defined all items from default style such as color especially.

Toolbar hamburger on click listener?

I'm implementing persistent search view like google play in my android application.here is my code.
public class NavigationDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private EditText searchView;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;
private TextView appName;
private boolean isNavigationDrawerOpened;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout)findViewById(R.id.mainLayout);
coordinatorLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(isNavigationDrawerOpened){
hideSearchAndEnableAppName();
}
return false;
}
});
searchView = (EditText) findViewById(R.id.searchView);
appName = (TextView) findViewById(R.id.appName);
searchView.setVisibility(View.GONE);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),"asdfasdfasdf",Toast.LENGTH_LONG).show();
return false;
}
});
drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "asdfasfasdf", Toast.LENGTH_SHORT).show();
}
});
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(NavigationDrawerActivity.this,"yayyy mani got it",Toast.LENGTH_LONG).show();
}
});
appName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isNavigationDrawerOpened) {
searchView.setVisibility(View.VISIBLE);
searchView.requestFocus();
appName.setVisibility(View.GONE);
isNavigationDrawerOpened = true;
animateDrawerIndicator(true);
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void hideSearchAndEnableAppName() {
animateDrawerIndicator(false);
isNavigationDrawerOpened = false;
searchView.setVisibility(View.GONE);
appName.setVisibility(View.VISIBLE);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if(isNavigationDrawerOpened){
hideSearchAndEnableAppName();
}else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
drawer.openDrawer(GravityCompat.START);
Toast.makeText(getApplicationContext(),"asdfasdf",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void animateDrawerIndicator(boolean shouldAnimate) {
ValueAnimator anim;
if(shouldAnimate) {
anim = ValueAnimator.ofFloat(0, 1);
} else {
anim = ValueAnimator.ofFloat(1, 0);
}
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
// You should get the drawer layout and
// toggler from your fragment to make the animation
toggle
.onDrawerSlide(drawer,
slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}
}
Here everything is working fine, but when user has clicked on toolbar edittext(as per my code) i'm changing the hamburger icon to back button. Now if user clicks on back button, it should animate back to hamburger and edittext should be hidden(as per my requirement)
But, for that to happen i need to handle hamburger click. I have tried onClickListners on toggle,actionbartoggle etc.But in vain please help.
When both toolbar and drawer were present.
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
This is I used in my app when Change the hamburger icon with other
when set icon than on click of that is not working because
we need to use:
mDrawerToggle.setDrawerIndicatorEnabled(false);
But when only toolbar(No Navigation Drawer):
toolbar = (Toolbar) findViewById(R.id.toolbar);
toggle = new ActionBarDrawerToggle(
this, null, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
Then change the hamburger icon to any other as:
toolbar.setNavigationIcon(R.drawable.ic_dehaze_black_24dp);
Directly calling an ActionBarDrawerToggle's onDrawerSlide() method to animate the toggle is messy. A better option is to set a DrawerArrowDrawable as the toggle's Up indicator, and animate that ourselves instead, enabling and disabling the toggle's drawer indicator as necessary.
First, we declare a DrawerArrowDrawable field in the Activity.
private DrawerArrowDrawable searchToggle;
We then initialize it in the onCreate() method, set it as the Up indicator, and define the navigation OnClickListener.
searchToggle = new DrawerArrowDrawable(this);
toggle.setHomeAsUpIndicator(searchToggle);
toggle.setToolbarNavigationClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideSearchAndEnableAppName();
}
}
);
In the animateDrawerIndicator() method, we first disable the drawer indicator if we're showing the search EditText. This causes the toggle to revert to the Up indicator, which we'll then animate as necessary, switching back to the drawer indicator once the hiding animation is finished.
public void animateDrawerIndicator(final boolean shouldAnimate) {
ValueAnimator anim;
if (shouldAnimate) {
anim = ValueAnimator.ofFloat(0, 1);
// Show the Up indicator instead
// of the drawer indicator
toggle.setDrawerIndicatorEnabled(false);
}
else {
anim = ValueAnimator.ofFloat(1, 0);
}
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
// Animate our Drawable instead of the drawer's
searchToggle.setProgress(slideOffset);
// If we're hiding and have finished,
// re-enable the drawer indicator
if(!shouldAnimate && slideOffset == 0) {
toggle.setDrawerIndicatorEnabled(true);
}
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}

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