The Toolbar is not appearing

Asked

Viewed 67 times

1

I’m making an app for my tcc and I decided to put a side menu in the app, the menu works, but where the Toolbar appears only a black belt without icon and I put a burger menu icon.

I am using android studio 1.4, Toolbar has some problem in this version?

main java.

...

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

     
            ActionBar actionBar = getSupportActionBar();
                if (actionBar != null) {
                    actionBar.setDisplayHomeAsUpEnabled(true);
                    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
                }

        
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(menuSelection);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //Clique no ícone hamburger de menu, para a abertura do drawer
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private NavigationView.OnNavigationItemSelectedListener menuSelection = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            switch (menuItem.getItemId()){
                
            }
            mDrawerLayout.closeDrawers();
            return false;
        }
    };

activity_main.xml

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


<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TelaPrincipal">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="194dp"
                android:id="@+id/imageView"
                android:src="@drawable/logo_llga2"
                android:layout_marginLeft="0dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="BEM VINDO"
                android:id="@+id/tv_bem_vindo"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="40dp"
                android:textColor="#061af6" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="criar um novo relatório"
                android:id="@+id/button"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="40dp"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:onClick="entrar_clique" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ENVIAR RELATÓRIO"
                android:id="@+id/button5"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:onClick="enviar_clique" />

        </LinearLayout>


        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu_drawer"/>

</android.support.v4.widget.DrawerLayout>

xml style.

<!-- 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" />

I also added this to Androidmanifest.xml

android:theme="@style/Theme.AppCompat.Light.NoActionBar">
  • Are you sure the version is 1.4? Because this is an old version. The stable current version is 4.1

1 answer

0

There are several points to be corrected there:

If you are using android:theme="@style/Theme.AppCompat.Light.NoActionBar"> In Androidmanifest.xml, it doesn’t matter what you did in Styles.xml because the app will use the default theme. To use the theme that is declared in your Styles.xml, use android:theme="@style/AppTheme".

You don’t even need to edit anything in Styles.xml, unless you want to switch to a dark theme or something. The file that comes when you create the project is already a good basis.

You don’t need to create a Toolbar in your XML if you use the theme <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">, that, as the name says, brings an action bar for you. To use this action bar that comes, just call getSupportActionBar() in your charity.

The fact that everything went black might have something to do with the lines android:theme="@style/ThemeOverlay.AppCompat.ActionBar" and app:popupTheme="@style/ThemeOverlay.AppCompat.Light", but if you use the standard action bar, it will probably solve itself.

Browser other questions tagged

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