Monday, January 28, 2019

This will be one of the features you will use the most, so its best you get the handle of it now.

create a file under res/menu/app_menu_bar.xml and paste the code below.



<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    >
    <item        android:id="@+id/harzard_menu"        android:title="Harzard"        android:icon="@mipmap/harzard_sign"        app:showAsAction="ifRoom"    />

    <item        android:id="@+id/indicator_left"        android:title="Indicator Left"        android:icon="@mipmap/indicator_left_on"        app:showAsAction="ifRoom"        />

    <item        android:id="@+id/indicator_right"        android:title="Indicator right"        android:icon="@mipmap/indicator_left_on"        app:showAsAction="never"        />

</menu>


looking at the above code
ifRoom: menu icon will only if the is room on the menu bar.

Never: menu item will only show if you click on the ... vertical icon.

@mipmap/: This is where the compiler will look for the item's icon. res/mipmap

now open you MainActivity.java and paste the following code under the MainActivity class.


@Overridepublic boolean onCreateOptionsMenu(Menu menu){
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.app_bar_menu, menu);

    return true;
}

@Overridepublic boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();

    if(id == R.id.harzard_menu){
        //your code here
    }
    else if(id == R.id.indicator_left){
        //your code here
    }
    else{
        //your code here
    }

    return false;

}



We have two functions on the above code, first we override the onCreateOptionsMenu 
function and inflate our app_bar_menu.xml layout.

New we override the onOptionsItemSelected function to help us identify which menu item has
been clicked. 

READ >> How to make a clickable items to a new Activity











































Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Mdunge Apps - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -