Home > other >  Having a problem creating transparentToolbar
Having a problem creating transparentToolbar

Time:03-25


import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager

class zz {


    private fun transparentToolbar() {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
        }
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        }
        if (Build.VERSION.SDK_INT >= 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
            getWindow().setStatusBarColor(Color.TRANSPARENT)
        }
    }

    private fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
        val win = activity.window
        val winParams = win.attributes
        if (on) {
            winParams.flags = winParams.flags or bits
        } else {
            winParams.flags = winParams.flags and bits.inv()
        }
        win.attributes = winParams
    }
}

this are the errors i got Type mismatch: inferred type is zz but Activity was expected Unresolved reference: getWindow line 15 and 21,(this) line 18 and 22 (getwindow) enter image description here

CodePudding user response:

in your first method this refers to current object receiver, which in your case is the zz class, getWindow() reference is not resolved because there's no such method available in the receiver zz class, but in Activity class implementation it has the method so you will need an Activity instance to be able to call getWindow()

in your second method it expect to receive Activity as the argument so the type mismatch because zz doesn't inherit from or is the expected Activity class

CodePudding user response:

Rather than using the default toolbar create a new toolbar using the widget:

Firstly remove the Toolbar from your app style

From

<style name="Theme.<AppName>" parent="Theme.AppCompat.DayNight.ActionBar">

To

<style name="Theme.<AppName>" parent="Theme.AppCompat.DayNight.NoActionBar">

Add a custom layout style to your theme.xml

<style name="CustomActionBar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="android:background">#00000000</item>
</style>

Then add a toolbar widget to your activity

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:theme="@style/CustomActionBar"/>

This should do it!

  • Related