user3599285
user3599285

Reputation: 91

Android action buttons in action bar not appearing?

I want to create an add button in the action bar but it doesn't seem to appear when i run thee code.

Here is my main_activity.java

public class MainActivity extends ActionBarActivity {

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




        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main_activity_actions, menu);
            return super.onCreateOptionsMenu(menu);
    }

    @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();
        if (id == R.id.action_add) {

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

This is my main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/action_add"
          android:icon="@drawable/ic_add"
          android:title="Add"
          android:showAsAction="always"
           />

</menu>

Please Help I cant figure out what's wrong !

Upvotes: 1

Views: 174

Answers (3)

Jayesh Khasatiya
Jayesh Khasatiya

Reputation: 2140

Replace your menu file with below menu file

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Theme.AppCompat.Light"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="yourpackagename.Mainactivity" >



<item android:id="@+id/action_add" 
  android:icon="@drawable/add_plush" app:showAsAction="always"></item>
</menu>

thats it...

Upvotes: 0

Namrata
Namrata

Reputation: 1684

Change your menu_xml to this :-

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_add"
        android:clickable="true"
        android:icon="@drawable/ic_add"
        android:showAsAction="always"
        android:title="action_location_found">
</menu>

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Since your Activity extends ActionBarActivity you would be using AppCompat from support library.

So change to

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_add"
      android:icon="@drawable/ic_add"
      android:title="Add"
      yourapp:showAsAction="always"
       />
    ...
</menu>

Quoting docs

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

Upvotes: 1

Related Questions