Search code examples
androidkotlinandroid-actionbarandroid-toolbarandroid-viewbinding

Unable to replace CustomToolbar with ActionBar in Kotlin


I Created a Custom Toolbar and placed it in activity, but I got this error :

java.lang.IllegalStateException: Activity com.example.citiesdistance.feature.main.MainActivity@3a04806 does not have an ActionBar set via setSupportActionBar()

Note :

When I set the default ActionBar to show The Custom Toolbar work correctly, but it is under the default ActionBar.

CustomToolbar Class :

class BaseToolbar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private lateinit var binding: ViewToolbarBinding

    init {
        if (attrs != null) {
            binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true)
            val attr = context.obtainStyledAttributes(attrs, R.styleable.BaseToolbar)
            val title = attr.getString(R.styleable.BaseToolbar_bs_title)
            if (!title.isNullOrEmpty())
                binding.toolbarTitleTv.text = title
            attr.recycle()
        }
    }
}

view_toolbar.xml (CustomToolbar Xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <ImageView
        android:id="@+id/backBtn"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:scaleType="centerInside"
        android:background="?selectableItemBackgroundBorderless"
        android:layout_gravity="center_vertical|start"
        android:layout_marginStart="16dp"
        app:srcCompat="@drawable/ic_back"/>
    <TextView
        android:id="@+id/toolbarTitleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:layout_marginStart="56dp"
        style="?textAppearanceHeadline6"
        tools:text="نام صفحه"/>
</FrameLayout>

activity_main.xml :

<?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"
    style="@style/WidthHeight.All.BothMatchParent"
    tools:context=".feature.main.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.example.view.BaseToolbar
            android:id="@+id/testToolBar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:bs_title="Page 1" />
    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

themes.xml :

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.CitiesDistance" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        .
        .
        .
    </style>
</resources>

Default ActionBar :

enter image description here

NoActionBar :

enter image description here


Solution

  • I think the only step you are missing is the step 5 from https://developer.android.com/develop/ui/views/components/appbar/setting-up

    In the activity's onCreate() method, call the activity's setSupportActionBar() method, and pass the activity's toolbar.