Disable actionbar Up button - android

I am trying to disable the Up button in the Support Actionbar on certain user actions. According to http://developer.android.com/reference/android/app/ActionBar.html#setHomeButtonEnabled(boolean), setHomeButtonEnabled should do what I want. However, the button remains clickable. (I can disable the other icons in the actionbar without problem).
I am trying to disable it from a fragment.
My code to disable is:
#Override
public void onPrepareOptionsMenu (Menu menu) {
if (isUpdating) {
getBaseActivity().getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_backgrey));
getBaseActivity().getSupportActionBar().setHomeButtonEnabled(false);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setEnabled(false);
menu.getItem(i).getIcon().setAlpha(64);
}
} else {
getBaseActivity().getSupportActionBar().setHomeButtonEnabled(true);
getBaseActivity().getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_back));
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setEnabled(true);
menu.getItem(i).getIcon().setAlpha(255);
}
}
}
and my code in activity onCreate is:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_back));
and my Fragment onCreate contains
setHasOptionsMenu(true)

In your fragment add
getBaseActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
and in your Activity add
getSupportActionBar().setDisplayHomeAsUpEnabled(false);

In oncreate() setDisplayHomeAsUpEnabled should be set to false as well.
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled(boolean)

Related

How to disable BottomNavigationView click and Touch?

i want to implement behavior on certain condition the bottom view is unable to click, i want to make if bottom view item being click it does not navigate to that item but still stay at the current item
You can disable menu items if you want to disable bottom navigation view
private void enableBottomBar(boolean enable){
for (int i = 0; i < mBottomMenu.getMenu().size(); i++) {
mBottomMenu.getMenu().getItem(i).setEnabled(enable);
}
}
Kotlin style one-liner:
bottom_navigation.menu.forEach { it.isEnabled = false }
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:contextClickable="false"/>
Try this code.it Disables the click.
Dynamicaly using Java pr Kotlin you can disable click.
bottomView.setEnabled(false);
bottomView.setFocusable(false);
bottomView.setFocusableInTouchMode(false);
bottomView.setClickable(false);
bottomView.setContextClickable(false);
bottomView.setOnClickListener(null);
setting onClick Listener to Null helps to Disable click events
bottomView.menu.forEach { it.isEnabled = false }
You can set the touch listeners of its subviews. Example using android-ktx:
bottomNav.children.forEach {
(it as? ViewGroup)?.children?.forEach {
it.setOnTouchListener { _, _ -> true } // or null to enable touch again
}
}
public class CustomBottomNavigationView extends BottomNavigationView {
...
#Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
ViewGroup menuView = (ViewGroup) getChildAt(0);
if (menuView != null) {
for (int i = 0; i < menuView.getChildCount(); i++) {
menuView.getChildAt(i).setEnabled(enabled);
}
}
}
}
You can do something like
bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }

How to detect menu item overflow?

Is it possible to programmatically detect when a menu is overflowed?
My intention is to have a menu item always be visible (SHOW_AS_ACTION_ALWAYS), except for in the case where it would cause other items to overflow, in which case, don't show the menu item at all. That is:
if (overflowed) actionBarMenu.removeItem(id);
You are not saying where this menu is appearing, so I'll just give an example of what you can do with a Toolbar. What you need to do is to get the reference to the ActionMenuView from the Toolbar and then call isOverflowMenuShowing on it, something like this:
private boolean isOverflowShowing(Toolbar toolbar) {
if(toolbar == null) {
return false;
}
for(int i = 0; i < toolbarView.getChildCount(); i++) {
View v = toolbarView.getChildAt(i);
if(v instanceof ActionMenuView) {
return ((ActionMenuView)v).isOverflowMenuShowing();
}
}
return false;
}
This is crude and dirty - and I haven't tested it - but it should get you started.

Android Studio GridLayout toggle all button

I'm using a Gridlayout with the MyView.java class. I got the code from GridLayout.
I implemented another button under the GridLayout. I want to toggle all (GrigLayout-)Buttons with this button. Does anyone have an Idea how I could do this?
Thank you!!!
You have to revise through all child views, and check if they are toggle buttons. Here is an example, when you want to toggle, call toggleButtons(grid);
private void toggleButtons(ViewGroup v) {
View a;
for(int i = 0; i < v.getChildCount(); i++)
{
a = v.getChildAt(i);
if(a instanceof ViewGroup) {
toggleButtons((ViewGroup) a);
}
else if(a instanceof Button){
// Toggle here
}
}
}

Android Support Library 21 toolbar losing theme

My question is in continuation to this question Toolbar NavigationIcon loose theme. This is a known bug in support library 21.
In my case i have two activities with two different theme toolbars. When I navigate back from Activity2 to Activity1, Activity1 toolbar buttons (back and overflow) take Activity2's theme.
I am trying to do a workaround for this problem and this what I have tried so far.
onResume() of my Activity1
#Override
protected void onResume() {
super.onResume();
supportInvalidateOptionsMenu();
if(mToolbar != null) {
// set navigation icon
mToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
onPrepareOptionsMenu of Activity1
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// set overflow icon
for(int i = 0; i < mToolbar.getChildCount(); i++) {
if(mToolbar.getChildAt(i) instanceof ActionMenuView) {
ActionMenuView v = (ActionMenuView)mToolbar.getChildAt(i);
for(int j = 0; j < v.getChildCount(); j++) {
if(v.getChildAt(j) instanceof TintImageView) {
TintImageView v1 = (TintImageView)v.getChildAt(j);
v1.setImageResource(R.drawable.abc_ic_menu_moreoverflow_mtrl_alpha);
}
}
}
}
return super.onPrepareOptionsMenu(menu);
}
with this workaround my back button is working fine but overflow icon comes in Activity2's theme untill I press it once. Once pressed it changes its theme. Also I am using Searchview in Activity1 and when its activated then only overflow icon appears.
Let me know if more information is required.

How can I ellipsize MenuItem?

I need to ellipsize the end of each menu item. How can I target the textView in menuItem to setEllipsize
SubMenu sm = menuItem.getSubMenu();
for (int i = 0; i < sm.size(); i++) {
MenuItem mi = sm.getItem(i);
}
If defining the MenuItem from XML, you can use android:actionLayout to change the layout of the MenuItem. So you'll have:
<item android:id="#+id/button_id"
android:icon="#drawable/icon"
android:showAsAction="always"
android:actionLayout="#layout/action_button"
android:title="#string/text"/>
Then in layout/action_button, you could have:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
...
whatever other styles you need to set
...
/>
Keep in mind that if you do this, you may need to trigger onOptionsItemSelected yourself (if you're using ActionBarSherlock, for example). To accomplish that, do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate your menu
...
// Get the MenuItem of interest
final MenuItem item = menu.findItem(R.id.button_id);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
onOptionsItemSelected(item);
}
});
}
Please read the following link, it contains valuable information about when to ellipsis MenuItem titles:
When to use ellipsis after menu items
Unfortunately, I did not found an option to ellpsize MenuItem titles just like TextView method setEllipsize(), but if it is really something you must to do, you could solve it easily:
SubMenu sm = menuItem.getSubMenu();
String title = "";
for (int i = 0; i < sm.size(); i++) {
MenuItem mi = sm.getItem(i);
title = mi.getTitle().toString();
if (title.length() > 10) {
String truncated = title.subSequence(0, title.length() - 3).toString().concat("...");
mi.setTitle(truncated);
}
}

Categories

Resources