androidkotlinandroid-activityviewstub

Making a ViewStub Overlay an Activity


I'm trying to create a Login ViewStub that will overlay the Welcome Activity when customers select the login button. However, I'm running into a problem. My stub will open when clicking login, but the buttons on my Welcome Activity won't dim. Also, do I need to make my background view of my stub wrap content so it can cover the whole screen? Or should I use a different technique from a stub to accomplish what I’m trying to do? Thank you!

Screenshots of what I'm trying to accomplish

enter image description here

enter image description here

activity_welcome.xml

<androidx.constraintlayout.widget.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:background="@drawable/gradient_background"
    tools:context=".Activities.Authentication.WelcomeActivity">

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="175dp"
        android:layout_height="75dp"
        android:layout_marginTop="60dp"
        android:contentDescription="@string/logo_image"
        android:scaleType="fitXY"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/skedaddle_services_" />

    <TextView
        android:id="@+id/findQualityServicesTextView"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:text="@string/finding_quality_service_providers_near_you_in_just_seconds"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logoImageView" />

    <TextView
        android:id="@+id/guestOrLogInTextView"
        android:layout_width="314dp"
        android:layout_height="38dp"
        android:layout_marginTop="8dp"
        android:text="@string/continue_as_a_guest_to_look_around_the_app_log_in_or_create_an_account_to_start_requesting_services"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/findQualityServicesTextView" />

    <ViewStub
        android:id="@+id/loginStub"
        android:layout_width="360dp"
        android:layout_height="620dp"
        android:layout="@layout/viewstub_login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="@drawable/first_login_rounded_button"
        android:text="@string/log_in"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/signUpButton"
        app:layout_constraintEnd_toEndOf="@+id/signUpButton"
        app:layout_constraintStart_toStartOf="@+id/signUpButton"
        app:layout_constraintTop_toBottomOf="@+id/guestOrLogInTextView"
        app:layout_constraintVertical_bias="0.13"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/signUpButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/first_signup_rounded_button"
        android:text="@string/sign_up"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/continueAsGuestButton"
        app:layout_constraintEnd_toEndOf="@+id/continueAsGuestButton"
        app:layout_constraintStart_toStartOf="@+id/continueAsGuestButton"
        app:layout_constraintTop_toBottomOf="@+id/loginButton" />

    <Button
        android:id="@+id/continueAsGuestButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:background="@android:color/transparent"
        android:text="@string/continue_as_guest"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signUpButton"
        tools:ignore="TextContrastCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

viewstub_login.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BF090909">

</androidx.constraintlayout.widget.ConstraintLayout>

WelcomeActivty.kt

val loginButton = findViewById<Button>(R.id.loginButton)
loginButton.setOnClickListener {
    val loginStub = findViewById<ViewStub>(R.id.loginStub)
    loginStub.inflate()

}

Solution

  • Got it, I had to set the alpha of each button.

    val loginButton = findViewById<Button>(R.id.loginButton)
    val signUpButton = findViewById<Button>(R.id.signUpButton)
    val guestButton = findViewById<Button>(R.id.continueAsGuestButton)
    
    loginButton.setOnClickListener {
        val loginStub = findViewById<ViewStub>(R.id.loginStub)
        loginStub.inflate()
    
        loginButton.alpha = 0.25f
        loginButton.isEnabled = false
        signUpButton.alpha = 0.25f
        signUpButton.isEnabled = false
        guestButton.alpha = 0.25f
        guestButton.isEnabled = false
    }