Search code examples
androidimagesdkandroid-actionbar

Add an image to the default android app action bar


Actually I have this screen on the activity in my android app. Main activity screenshot

I wish to add an image to the left side of the text "Stone" on the top left corner which is contained in the default action bar of the app (which is the default theme).

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:gravity="center_horizontal"
        android:text="Stone Login"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/unameTxt"
        android:layout_width="358dp"
        android:layout_height="41dp"
        android:layout_marginTop="52dp"
        android:ems="10"
        android:hint="User id"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <EditText
        android:id="@+id/passwdTxt"
        android:layout_width="359dp"
        android:layout_height="42dp"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:hint="@string/prompt_password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/unameTxt" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:text="Accedi"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwdTxt" />

</android.support.constraint.ConstraintLayout>

On the top, there is the activity layout XML file.

This is my styles.xml file:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

I've tried many solutions but nothing has worked and I wish to change the actionbar for all the activities in the app.

Could you help me in doing this, please?

Kindly, Roberto


Solution

  • In order to display a custom drawable in your ActionBar you should add these lines:

    supportActionBar?.setDisplayShowHomeEnabled(true)
    supportActionBar?.setIcon(R.drawable.your_icon)
    

    Here's an article about ActionBar https://guides.codepath.com/android/Defining-The-ActionBar