Adding a button to the support ActionBar

28 views Asked by At

I currently have this setup for my application in which I have an action bar at the top of the screen with a "hamburger" icon in the top left that shows this navigation drawer. Navigation drawer in the application

I now want to add an icon in the top right of the action bar which will display a cart with a badge. Unfortunately, I don't seem to have found a viable solution, as most what I've seen answered for other questions seems outdated. In particular, what I've tried is adding another menu option that has "app:showAsAction" set to always and having it "setup" by overriding onCreateOptionsMenu(Menu menu) in my activity. The problem is that 1. The item is also displayed in the navigation drawer, which I do not want 2. This creates an option menu, which means I get the presence of these permanent three dots: Action bar with undesired option menu

I'd like to know about an alternative solution that lets me both have a navigation drawer with a regular "hamburger" icon in the action bar on the left and a cart button (button, not just an icon, I need to have behavior on click) in the top right. This is what my project looks like:

menu_entries.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/navMenu"
   tools:ignore="HardcodedText">
   <item
       android:id="@+id/navMenuCocktail"
       android:title="Cocktail" />
   <item
       android:id="@+id/navMenuFrullati"
       android:title="Frullati" />
   <item
       android:id="@+id/navMenuSuggested"
       android:title="Suggeriti" />
   <item
       android:id="@+id/navMenuLogout"
       android:title="Logout"/>
   <item
       android:id="@+id/navMenuActionCart"
       android:title="Cart"
       android:enabled="false"
       app:actionLayout="@layout/cart_item_layout"
       android:icon="@drawable/ic_action_cart"
       app:showAsAction="always"/>
</menu>

layout_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    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/drawerLayoutHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="HardcodedText">

    <ProgressBar
        android:id="@+id/progressBarHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="0.5"
        android:scaleY="0.5"
        android:indeterminateTint="@color/drink_orange"
       />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewDrink"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>



    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navViewMenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_entries"/>

</androidx.drawerlayout.widget.DrawerLayout>

ControllerHome.java (the activity displayed. I commented out a lot of irrelevant things such as networking and navigation drawer logic.)

//
public class ControllerHome extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener {

    private ArrayList<FeedItem> cartFeedItems = new ArrayList<>();

    private enum HomeState{
        HOME_COCKTAIL,
        HOME_FRULLATI,
        HOME_SUGGESTED,
        HOME_CART,
        HOME_LOGOUT;

        @Override
        public String toString(){
            if (this.equals(HOME_COCKTAIL)) return "Cocktail";
            else if (this.equals(HOME_FRULLATI)) return "Frullati";
            else if (this.equals(HOME_SUGGESTED)) return "Consigliati";
            else if (this.equals(HOME_LOGOUT)) return "Logout";
            else if (this.equals(HOME_CART)) return "Carrello";
            return "INVALID";
        }
    }
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private NavigationView navigationView;
    private HomeState currentHomeState = HomeState.HOME_COCKTAIL;
   //
    private RecyclerView recyclerViewDrink;
    private FeedItemAdapter feedItemAdapter;
    private ProgressBar progressBar;
    private TextView cartBadge;
    private int numberOfItemsInCart = 0;
    private final int MAX_NUMBER_OF_CART_ITEMS_DISPLAYED = 99;
    private FeedItemAdapter.ProductQuantityListener
            productQuantityListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_home);
        drawerLayout = findViewById(R.id.drawerLayoutHome);
        navigationView = findViewById(R.id.navViewMenu);
        recyclerViewDrink = findViewById(R.id.recyclerViewDrink);
        progressBar = findViewById(R.id.progressBarHome);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        navigationView.setNavigationItemSelectedListener(this);
        //
    }

    @Override
    public boolean onCreateOptionsMenu(@NonNull Menu menu){
        getMenuInflater().inflate(R.menu.menu_entries, menu);
        MenuItem menuItem = menu.findItem(R.id.navMenuActionCart);
        View actionView = menuItem.getActionView();
        cartBadge = actionView.findViewById(R.id.cart_badge);
        actionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNavigationItemSelected(menuItem);
            }
        });
        cartBadge = findViewById(R.id.cart_badge);
        cartBadge.setVisibility(View.GONE);

        productQuantityListener = //
                
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //
}
0

There are 0 answers