Erito
Erito

Reputation: 325

The Toolbar doesn't appear

I'm trying to create an application but I can't find why my toolbar doesn't appear.

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }
}

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:actionbarcompat_mse="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item android:id="@+id/sort_settings"
          android:icon="@drawable/sort_icon"
          android:title="Sort"
          actionbarcompat_mse:showAsAction="ifRoom"/>

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        actionbarcompat_mse:showAsAction="never" />
</menu>

my_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_alignParentTop="true"
    app:popupTheme="@style/AppTheme.PopupOverlay">


</androidx.appcompat.widget.Toolbar>

And here is my layout for activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/my_toolbar"/>

</RelativeLayout>

I've checked out for multiple solutions, but none managed to solve the problem. Thanks for your time!

Upvotes: 0

Views: 67

Answers (1)

Vibhanshu Sharma
Vibhanshu Sharma

Reputation: 259

In toolbar xml change

android:layout_height="wrap_content"

this to

android:layout_height="?attr/actionBarSize"

This will make your toolbar height as default size of action bar.

Upvotes: 2

Related Questions