0

I have this fragment:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/main_app_bg_color"
    android:orientation="vertical">

    <ImageView
        android:background="@drawable/custom_header_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        // more views here

     </LinearLayout 

</LinearLayout>

custom_header_bg is a transparent PNG containing the 2 blue layers. You can see the title bar's color is black, and I want it to be transparent, so it shows dark blue instead of black.

I tried this:

   override fun onCreate(savedInstanceState: Bundle?) {
        (activity as AppCompatActivity?)!!.supportRequestWindowFeature(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR_OVERLAY)
        super.onCreate(savedInstanceState)
}

An exception occured:

android.util.AndroidRuntimeException: Window feature must be requested before adding content

which pointed at onCreate(). How to fix this?

anta40
  • 6,511
  • 7
  • 46
  • 73

2 Answers2

0

You can add this line to your themes.xml to change the color as you choose;

        <item name="colorPrimaryVariant">@color/darkBlue</item>

But this will set the status bar color to dark blue throughout the app. Maybe the link below will be more helpful. Transparent status bar.

:)

Roland
  • 91
  • 4
0

You are referring to the status bar as the title bar. You can use the following to change the color of the status bar. Put these lines in your theme file according to the min API level.

Requires Api >= 21:

<item name="android:statusBarColor">@color/statusBarColor</item> 

For lower Apis:

<item name="colorPrimaryDark">@color/yourColor</item>
  • Doesn't work on my case. On the other side, ` val w = requireActivity().window w.setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS )` worked fine. There's a downside, though: all the items on my bottom navigation view become unclickable :/ – anta40 May 27 '22 at 06:56