Showing posts with label menu. Show all posts

This is a useful tool, especial if you have a complex UI. the ability to go back to your previously opened Activity.

1. first you need to import the menuItem class on your opened Second Activity.



import android.view.MenuItem;

2. next add the following code under

public class SecondActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        //add back button        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3. now paste the follow code.

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

    if(id == android.R.id.home){
          this.finish();
    }

    return super.onOptionsItemSelected(item);
}


Its that simple, i hope this article was helpful.










How to add a Back button on Activity

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











































How To create Menu Bar with clickable item

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