Search code examples
javaandroidxmlsurfaceviewcustom-view

SurfaceView as backGround


I want to use my surfaceView as the background of my activity but there is my problem:

if I set setZOrderOnTop(true); it will cover the entire screen on top of everything and nothing is visible on it and if I set it to false surfaceView will be invisible.

here is my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".views.activities.IntroActivity">

<!--this is my custom surface view-->

    <ir.example.app.views.custom.noise3
        android:id="@+id/intro_noise"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.view.ViewPager
        android:id="@+id/introPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/nextIntro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/next_round_btn_shape"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/lastIntro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/back_round_btn_shape"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

EDIT: This is my viewPager fragment's 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">

<com.hosseinkud.toolbox.utiles.CTV
    android:id="@+id/introTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="88dp"
    android:layout_marginEnd="8dp"
    android:gravity="center"
    android:text="some nice text"
    android:textColor="@color/white"
    android:textSize="@dimen/textVeryLarge"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/introPic"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:src="@drawable/tvtest"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/introTitle"
    app:layout_constraintVertical_bias="0.0" />

and this is screenshot when I set setZOrderOnTop(true);: enter image description here


Solution

  • At the end, I found the answer myself and you should handle it like following by adding setZorderOnTop(false); to your activity and below code to your appTheme :

    <style name="Theme.Transparent" parent="AppTheme">
                    <item name="android:windowIsTranslucent">true</item>
                    <item name="android:windowBackground">@android:color/transparent</item>
                    <item name="android:windowContentOverlay">@null</item>
                    <item name="android:windowIsFloating">false</item>
                    <item name="android:backgroundDimEnabled">false</item>
    </style>
    

    But I suggest that you simply use textureView instead of surfaceView it prevents tons of issues you might have and you don't even need to use a transparent background too.