I need to hide the hamburger icon
This is my toolbar
I need to hide the default hamburger icon of navigation bar and load it from another button click.The navigation bar need to appear on the attachment icon click in my toobar and need to disappear when i click outside(anywhere).Can this be done ?
if you are using ActionBarDrawerToggle then you can add a line:
toggle.setDrawerIndicatorEnabled(false);
and opening and closing drawer you can write in your click event:
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
You can hide the hamburger icon by doing this:
toolbar.setNavigationIcon(null); // to hide Navigation icon
toolbar.setDisplayHomeAsUpEnabled(false); // to hide back button
If you have added the attachment icon manually (As an imageView inside a Toolbar) :
boolean isDrawerOpen = false;
imageViewAttachment..setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
}
});
Or, if you've added as a Menu item :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.attachment:
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
return true;
}
return super.onOptionsItemSelected(item);
}
Related
hi i have an activity and I display 2 frigments I want to automatically close the toolbar when the back button is clicked
How can I do this? Should the code be written in an activity or in a fragment?
Do it in an activity:
boolean toolbarIsOpened = false;
#Override
public void onBackPressed() {
if (toolbarIsOpened){
//closeToolbar
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
}else{
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
}
}
Another way to hide:
getSupportActionBar().hide();
Press "back arrow" programmatically:
onBackPressed();
You can use performclick method like below snippets
img_back.performClick();
I have a menu upside my page and it has an action item. like this:
action item
I set app:showAsAction="always" for it.
I have invisible view (in UI) in my page too.
I want to if I click on it, my invisible view appears. so I set a boolean parameter for that.
My boolean set true but my object doesn't appear at all. How can I fix it?
here is my code:
public static boolean myBoolean=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this if doesn't check after change parameter
if(myBoolean)
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_setting: {
myBoolean=true;
return true;
}}}
What I am getting from this is you want to show a view based on the click of some menu item in action bar right?. if this is true then do like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_setting:
if(myBoolean){
findViewById(R.id.checkBox).setVisibility(View.GONE);
myBoolean = false;
}else{
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
myBoolean = true;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can't decide the visibility earlier in your code since click may happen sometime after onCreate() only.
I am trying to display a popup edit text when "action_guess" is clicked. i am not sure on how to go about this. here is my code:
public MyMenuItemClickListener() {
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_guess:
Toast.makeText(mContext, "guess code", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
If you want an EditText in a Dialog, you can follow this example.
https://www.mkyong.com/android/android-prompt-user-input-dialog-example/
If you want it appear in the ActionBar, you can overlay the EditText inside the ActionBar layout and show/hide with setVisibility().
I'm using the ActionBar extending from the AppCompatActivity. How can I check, if the dropdown menu of the ActionBar is opened at the moment.
I've tried it in this method. But it does not fire if I open the drop down menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
Log.i("ActionBar", "ActionBar dropdown is open at this moment");
switch (item.getItemId()) {
case R.id.preferences:
intent.setClass(StartupActivity.this, PreferencesActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.info:
intent.setClass(StartupActivity.this, InformationActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.contact:
intent.setClass(StartupActivity.this, ContactActivity.class);
startActivityForResult(intent, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It only fires, if I click on an item of the drop down menu. But I want to check, if the user clicks on the three-dot menu.
Try the following method:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
// menu is open
return super.onMenuOpened(featureId, menu);
}
I've tried this method, it works:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// menu open
return super.onPrepareOptionsMenu(menu);
}
Even when initializing the menu, and not only when you click, but better than nothing.
I found a workaround to actually enable the ActionBar home button on the nested PreferenceScreen... however it doesn't call OnOptionsItemSelected in my PreferenceActivity. Anyone know a way to actually use the home button on a nested PreferenceScreen?
Modification of post 35 here:
http://code.google.com/p/android/issues/detail?id=4611
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
{
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getActionBar().setHomeButtonEnabled(true);
return false;
}
I had this problem recently and this is how I solved it. Firstly to access the PreferenceScreen I use the exact same method you mentioned above.
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
initializeActionBar((PreferenceScreen) preference);
}
return false;
}
From here I looked into what a PreferenceScreen is, and I was saddened to find out it is just wrapper of a Dialog. Moving forward, I then set the actionbar display options and attempt find the home button area. This unfortunately wasn't too easy to get, but with the help of the hierarchy viewer I managed to gain access by finding the home icon and then its parent views. Once we have access to the containing LinearLayout, we can attach an onClickListener where we dismiss the PreferenceScreen's dialog, which calls PreferenceScreen's onDismissListener and returns us to the previous screen.
/** Sets up the action bar for an {#link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}