1

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'

Community
  • 1
  • 1
A_Matar
  • 2,210
  • 3
  • 31
  • 53
  • 3
    Just saying: it is much MUCH easier to extend `AppCompat` and put your own `ListView` in the layout using `setContentView` than trying to mess with delegates and callbacks. Also, `ListView` is not officially deprecated, but if you're starting a new project you really should use the new modern `RecyclerView` – Budius Jul 30 '15 at 12:07

1 Answers1

0

One approach you could use is to use a FragmentActivity as your base activity then have it load in a ListFragment. In the list fragment you can simply add this line in the onCreate method:

getActivity().getActionBar().setIcon(R.drawable.ic_main);

However Budius is right you should use a RecyclerView, its a new android class which simplifies the old viewholder pattern. Android List and Card Guide , RecyclerView Android Doc