Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 952
omer Avatar asked Dec 19 '22 04:12

omer


2 Answers

Try this:

getSupportActionBar().setDisplayShowTitleEnabled(true);  
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
like image 99
Murli Prajapati Avatar answered Jan 12 '23 08:01

Murli Prajapati


try this if you want to add back arrow in your actionbar

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher)
like image 36
AskNilesh Avatar answered Jan 12 '23 08:01

AskNilesh