Yanshof
Yanshof

Reputation: 9926

how to show the SettingsActivity when pressing on 'Settings' button ?

I define a SettingsActivity on my app and i want to show this activity when pressing on the 3 dot on the right side of the screen.

enter image description here

How to do it ? I can't find any manual/demo that help me to do it.

Upvotes: 0

Views: 497

Answers (1)

Aryan Raj
Aryan Raj

Reputation: 134

First of all, create a menu resource file and add the following code.

<group android:checkableBehavior="single">
   <item 
        android:id="@+id/settings"
        android:title="Settings"/>
 </group>

then, in your MainActivity (Activity where you want to show the three dots in toolbar) create a method onCreateOptionsMenu and onOptionItemSelected,

 @Override
 public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater.inflate(R.id.menu_settings, menu);
    MenuItem menuItem = menu.findItem(R.id.settings);
    return true;
  }

@Override
public boolean onOptionsItemSelected(MenuItem item){
     int id = item.getId();
     if (id == R.id.settings){
          startActivity(new Intent(MainActivity.this, SettingsActivity.class));
     }
     return super.onOptionsItemSelected(item);
 }

Upvotes: 2

Related Questions