title: How to put logout in action bar menu
body: <p>I used to use a logout button to logout when pressed on an activity but now I am willing to put it at action bar menu. I don't know how to do it.</p>
  
  <p>What I want to do: A user if presses logout on the right action bar menu, he should log out of the app.</p>
  
  <p>This is the code that I always use to have logout on button press (consider that activity page has only logout button):    </p>
  
  <pre><code>public class Activity_Main extends Activity {
  
  private Button btnLogout;
  private SQLiteHandler db;
  private SessionManager session;
  
      @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
       btnLogout = (Button) findViewById(R.id.btnLogout);
  
      // SqLite database handler
      db = new SQLiteHandler(getApplicationContext());
  
      // session manager
      session = new SessionManager(getApplicationContext());
  
      if (!session.isLoggedIn()) {
          logoutUser();
      }
  
      // Logout button click event
      btnLogout.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              logoutUser();
          }
      });
  }
  
  /**
   * Logging out the user. Will set isLoggedIn flag to false in shared
   * preferences Clears the user data from sqlite users table
   * */
  private void logoutUser() {
      session.setLogin(false);
  
      db.deleteUsers();
  
      // Launching the login activity
      Intent intent = new Intent(Activity_Main.this, Activity_Login.class);
      startActivity(intent);
      finish();
  }
  </code></pre>
  
  <p>Currently the action bar menu has two item more apps and rate apps which redirect to google play..I want to replace more app with logout action.</p>
  
  <p>This is what I want to modify:</p>
  
  <pre><code>@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // toggle nav drawer on selecting action bar app icon/title
      if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
      }
      // Handle action bar actions click
      switch (item.getItemId()) {
      case R.id.rate_app:
          try {
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
          } catch (android.content.ActivityNotFoundException anfe) {
              startActivity(new Intent(Intent.ACTION_VIEW,
                      Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
          }
          return true;
      case R.id.more_app:
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.more_apps))));
          return true;
      default:
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>This is the menu.xml</p>
  
  <p>
  </p>
  
  <pre><code>&lt;item
      android:id="@+id/ic_menu"
      android:icon="@drawable/ic_ab_overflow_compat"
      android:showAsAction="always"
      android:title=""&gt;
      &lt;menu&gt;
  
          &lt;!-- Rate App --&gt;
          &lt;item
              android:id="@+id/rate_app"
              android:showAsAction="ifRoom|withText"
              android:title="@string/rate"/&gt;
  
          &lt;!-- More App --&gt;
          &lt;item
              android:id="@+id/more_app"
              android:showAsAction="ifRoom|withText"
              android:title="@string/more"/&gt;
      &lt;/menu&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p></p>