how to simply place an icon in actionBar?

Asked

Viewed 1,518 times

2

I want to put an icon on my Actionbar already searched for this information here on Stackoverflow, but the answers I found are very complicated (I’m beginner) and so I wanted to know if it has a very simple form.

  • 3

    Please show an example of the answer you found and what you found complicated in it so we can judge if you can simplify them.

1 answer

2


You can use the Toolbar for that reason.

To Toolbar replace the ActionBar Android standard and with it is much easier to work.

First, you must configure your application theme to support Toolbar, saying that you want to change the ActionBar standard (don’t forget to change in your AndroidManifest.xml):

Styles.xml:

<style name="Seu.Tema" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/suaPrimaryColor</item>
    <item name="colorPrimaryDark">@color/suaPrimaryDarkColor</item>
</style>

Androidmanifest.xml

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Seu.Tema">
        ...
</application>

Now, you need to put the Toolbar in its layout. Remember that it is any component of Android, so it should be inside your .xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

</LinearLayout>

Once in the .xml, now you get to manipulate her in her class. Inside her, you need to tell her Activity that you want to change the ActionBar standard, for this you must use the method setSupportActionBar():

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

All right, now you Toolbar is already your ActionBar and you can customize it as you like. To change the default icon (or the Navigation Icon), you simply:

toolbar.setNavigationIcon(R.drawable.seu_icone);
  • thank you very much. was of great help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.