I have two activities
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
Try this:
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
try this if you want to add back arrow in your actionbar
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With