?a" /> ?a" /> ?a"/>

How to use toolbar/appBar with material full-screen dialogfragment in android?

34 Views Asked by At

I have such style for the dialog:

<style name="FullScreenDialogStyle" parent="Theme.MaterialComponents.Light.Dialog">
    <item name="colorPrimaryDark">?attr/colorPrimaryDark</item>
    <item name="colorAccent">?attr/colorAccent</item>
    <item name="colorPrimary">?attr/colorPrimary</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="actionMenuTextColor">?attr/colorAccent</item>
</style>

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

this is basic app theme:

<style name="Basic" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorControlNormal">#5c626d</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    <item name="rv">@style/ResumeEnterField</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>

and here I'm applying my dialog theme:

class ChangePassScr : DialogFragment(R.layout.change_pass_scr) {
    private var viewBinding: ChangePassScrBinding? = null

    override fun onStart() {
        super.onStart()
        dialog?.let {
            val size = ViewGroup.LayoutParams.MATCH_PARENT
            it.window?.setLayout(size, size)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.FullScreenDialogStyle)
    }
....
}

the problem is with the layout error:

Error inflating class com.google.android.material.appbar.AppBarLayout
                 Caused by: java.lang.reflect.InvocationTargetException

with my appBar:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:nestedScrollingEnabled="true"
        android:overScrollMode="never"
        android:scrollbars="none">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F1F1F1"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:paddingBottom="20dp">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/bar_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:navigationIcon="@drawable/ic_close"
                    app:subtitleTextAppearance="@style/Toolbar.SubtitleText"
                    app:titleTextAppearance="@style/Toolbar.TitleText"
                    app:titleTextColor="@color/white" />

            </com.google.android.material.appbar.AppBarLayout>

....

I tried to implement all material styled elements, but as I saw also app bar is available for the material elements.

UPDATE

The problem is with the style of the dialog, especially with this value:

<item name="colorAccent">?attr/colorAccent</item>

when I use style without it, all works, but how to implement changeable accent color in such case?

0

There are 0 best solutions below