I am trying to add action icons to my toolbar but in vain. I have followed all the suggestion from this thread.
Maybe the problem is that my Activity doesn't extend ActionBarActivity
directly because it already extends ListActivity
as follows:
public class ShopsCatalogueActivity extends ListActivity {
private Toolbar toolbar; // Declaring the Toolbar Object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//using delegates because this activity already extends ListActivity and we want to add toolbar
AppCompatCallback callback = new AppCompatCallback() {
@Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
@Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
//get the layout
delegate.setContentView(R.layout.activity_shops_catalogue);
//adding toolbar following material design approach
toolbar= (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
delegate.setSupportActionBar(toolbar);
...
}
here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyOwnAppNameSpace="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
MyOwnAppNameSpace:showAsAction="never" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:orderInCategory="200"
android:title="Search"
MyOwnAppNameSpace:showAsAction="always"></item>
<item
android:id="@+id/action_user"
android:icon="@drawable/ic_user"
android:orderInCategory="300"
android:title="User"
MyOwnAppNameSpace:showAsAction="ifRoom"></item>
</menu>
In the Activity:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shops_catalogue_toolbar_menu, menu);
return true;
}
In build.gradle I am using:
compile 'com.android.support:appcompat-v7:22.2.0'