Mehul
Mehul

Reputation: 29

Add options in an option menu of main toolbar in Android studio

I am new at android and am trying to create a customized toolbar. I was wondering if there is any way to add options to the settings menu(3 dots) when it is clicked.

enter image description here

enter image description here

Upvotes: 0

Views: 7365

Answers (2)

Akash
Akash

Reputation: 71

First you need to add an item in menu_main.xml(res>menu) file like.

<menu 
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"
tools:context="in.amzbizsol.vts.MainActivity">
<item
    android:id="@+id/action_changepassword"
    android:orderInCategory="1"
    android:title="Change Password"
    app:showAsAction="never" />
<item
    android:id="@+id/action_logout"
    android:orderInCategory="2"
    android:title="Logout"
    app:showAsAction="never" />

then in your MainActivity create something like

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_changepassword) {
        Intent intent=new  Intent(getApplicationContext(),ChangePassword.class);
        startActivity(intent);
        return true;
    }else if(id==R.id.action_logout){   
                finish();
            }
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Upvotes: 6

Onkar Nene
Onkar Nene

Reputation: 1379

Please Check this documentation - https://developer.android.com/guide/topics/ui/menus.html

Upvotes: -1

Related Questions