how to simply put an icon in actionBar?

2

I want to put an icon in my ActionBar I already looked for this information here in StackOverFlow, but the answers I found are very complicated (I'm a beginner) and so I wanted to know if it has a very simple form.     

asked by anonymous 07.12.2015 / 21:47

1 answer

2

You can use Toolbar for this.

Toolbar replaces Android's default% and that's much easier to work with.

First, you should set your app's theme to support ActionBar , saying that you want to change the% default% (be sure to change it on your Toolbar ):

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 ActionBar in your layout. Remember that it's an Android component, so it should be within your AndroidManifest.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 Toolbar , you can now manipulate it in your class. Inside it, you need to say your .xml you want to change the .xml default, for this you should use Activity method:

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

Done, now your ActionBar is already your setSupportActionBar() and you can customize it however you want. To change the default icon (or Toolbar ), you simply:

toolbar.setNavigationIcon(R.drawable.seu_icone);
    
08.12.2015 / 11:01