Omer JR Omer JR - 28 days ago 11
Android Question

Adding icon in Android ToolBar

I have two activities


  • MainActivity

  • DetailActivity



My MainActivity.xml has AppBarLayout and Toolbar elements.
In Main Activity.java, I set the icon in toolbar using:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// setting icon
getSupportActionBar().setIcon(R.mipmap.ic_launcher);


This same line of code is not working for Detail activity.
Detail Activity has the toolbar by default. I can set the title in toolbar as well using:

getSupportActionBar().setTitle(String title);


But, I am unable to set Icon in Detail Activity using:

getSupportActionBar().setIcon(R.mipmap.ic_launcher);


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>




<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".apod_main"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".apod_detail"></activity>
</application>




Build Configs:

Android API 26

Build Tools Version 26.0.1

Answer Source

Try this:

getSupportActionBar().setDisplayShowTitleEnabled(true);  
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);