2

My question is the same as Full screen background image in an activity. I did not use the background option but use the ImageView option, because I want to keep the aspect ratio (I do not mind some parts of the image getting cropped). I think the solution worked before, but somehow it started not working and showing a white status bar instead. enter image description here

I have tried other things in the question above, but I could not get the result I want (the background image below the status bar). What have I done wrong? Here are the entire source code that matters (I excluded the boilerplate.)

PS: I want to keep the status bar. What I want is like the screenshot of the linked question: full-screen image and a translucent status bar above the image. If I set the background image to the root layout, I get the result I want, except that the image aspect ratio is not kept. See the image below.

enter image description here

Manifest

<application
    android:theme="@style/AppTheme.NoActionBar">
    <activity
        android:name=".MainActivity">
    </activity>
</application>

Activity

class MainActivity : AppCompatActivity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

Layout

<android.support.constraint.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:adjustViewBounds="true"
    tools:context=".MainActivity">

    <ImageView
        android:adjustViewBounds="true"
        android:id="@+id/ivBackground"
        android:src="@drawable/doge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:fitsSystemWindows="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="0dp"/>
</android.support.constraint.ConstraintLayout>

Style v21

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>
</resources>

Style

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>

</resources>

[Added] I finally have got it working. But it was sort of weird. First, the root had to be CoordinatorLayout. ConstraintLayout did not work. Secondly, the must be a dummy view like below. If the dummy view was removed, the status bar became white. Whether android:statusBarColor or android:windowTranslucentStatus did not matter. The former made the status bar transparent, and the latter made it shady. See the screenshots, below. I think this may be a bug of the Support Library 27.1.0.

This layout worked.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <View
        android:fitsSystemWindows="true"
        android:layout_width="1dp"
        android:layout_height="1dp"/>

    <ImageView
        android:fitsSystemWindows="true"
        android:id="@+id/ivBackground"
        android:src="@drawable/doge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
</android.support.design.widget.CoordinatorLayout>

enter image description here enter image description here

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

0

I've had a similar issue, the solution is to make CoordinatorLayout the root layout, and then inside of it add your former layout root.

Also, removing this part in Style v21 might help too:

<item name="android:statusBarColor">@android:color/transparent</item>
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52