How to show icon images in Android Action Bar?

Asked

Viewed 101 times

1

I created a simple application, it is something very trivial. What is happening is that I am trying to create a menu with icons on Action Bar, but what is actually happening is that my application only shows a simple menu without showing the icons. I’ve researched several places, tried to track several videos on YOUTUBE. When I realized that the same way I was being taught on the Internet and in the official Android documentation was the same way I was doing on my project I imagined it was a bug problem on my virtual device, that is, I thought the problem was in my Android emulator from my local computer, so I decided to enable the developer mode of my smartphones, and running generated the same problem. So I conclude that the problem was code even, the problem is in my project, now just I just try to identify where the problem is and fix.

This image below is the Preview of my development IDE, that’s how I expected my application to behave.

inserir a descrição da imagem aqui

When I run my application it behaves totally differently. See and follow in the image below.

inserir a descrição da imagem aqui

As my project is small I will put the code of my entire project here, and I would like you to help me identify where this possible problem is and please help me to fix it.

This is my Activity.

inserir a descrição da imagem aqui

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toolbar;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

This is my main XML file.

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?android:attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@android:style/ThemeOverlay.Material.ActionBar"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is my menu.

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/action_download"
        android:icon="@drawable/ic_file_download_black_24dp"
        android:title="Download"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_open"
        android:icon="@drawable/ic_folder_black_24dp"
        android:title="Abrir"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_add"
        android:icon="@drawable/ic_add_circle_black_24dp"
        android:title="Adicionar"
        app:showAsAction="always" />
</menu>

This is the main configuration that makes everything work properly, at least it was to work properly.

inserir a descrição da imagem aqui

<resources>

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

</resources>

1 answer

1


Before it was like this;

inserir a descrição da imagem aqui

And I changed it and it stayed that way;

inserir a descrição da imagem aqui

I took questions with some of my fellow developers, they informed me that when we use the IDE’s graphics editor it is recommended to do an analysis of the code that the IDE is composing for you, that is exactly what went wrong. The graphics editor created for me some lines of code that Frameworks usually uses, I simply replace the tag app for android, which is a native Android tag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.