Search code examples
javaandroidandroid-actionbaraction-button

Android adding Action Buttons


Ok guys before i start i just want to tell you i have Android Studio 2.1 5 Preview. So the problem i have is with adding action buttons. And i followed this tutorial from the offical dev docs:

http://developer.android.com/intl/es/training/appbar/actions.html#add-actions

So after i went with the tutorial i arrived to this code:

MainActivity.java:

package com.example.amanuel.actionbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.ToolbarWidgetWrapper;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setLogoDescription("Logo");
        setSupportActionBar(toolbar);
    }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.action_settings:
        // User chose the "Settings" item, show the app settings UI...
        return true;

      case R.id.action_favorite:
        // User chose the "Favorite" action, mark the current item
        // as a favorite...
        return true;

      default:
        // If we got here, the user's action was not recognized.
        // Invoke the superclass to handle it.
        return super.onOptionsItemSelected(item);

    }
  }
}

res/menu/menu.xml:

<?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">
  <!-- "Mark Favorite", should appear as action button if possible -->
  <item
    android:id="@+id/action_favorite"
    android:icon="@mipmap/ic_favorite_black_48dp"
    android:title="@string/action_favorite"
    app:showAsAction="ifRoom"/>

  <!-- Settings, should always be in the overflow -->
  <item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    app:showAsAction="never"/>
</menu>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.amanuel.actionbar" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.example.amanuel.actionbar.MainActivity">

  <android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

So when i put all this and run it on genymotion i get this:

enter image description here

Well ok, i get my action bar but where in the world is my action buttons? Like it showed me on the dev docs:

enter image description here

Also i dont understand why the actionbar is in the middle.. Its normally suppose to be on the top, like a normal toolbar. This is just confusing me, please help. Thankyou!


Solution

  • You have nothing that references your menu.xml resource.

    Override onCreateOptionsMenu(), and use getMenuInflater().inflate(menu, R.menu.menu).