skip to Main Content

I am working on a project in which I am trying to make the screen black, so I am using View attribute here but the button is still visible(check screenshot). I was expecting the button will not be visible. I will handle the View attribute from the Kotlin file. Please help to fix this.

xml screenshot

<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"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/log_in_button"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:backgroundTint="#F5931D"
        android:text="login"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/loading_animation"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3

Answers


  1. It is likely that your View has lower elevation than the Button. You can try to set the elevation of the view to be higher than the elevation of the button, however, it would likely be better if you manage the visibility of the button and loading animation view, than their elevation.

    In your layout preview, you can manage the visibility by adding tools:vibility = "gone" to your button. This will hide the button in your preview, but not in the actual view at runtime. To achieve the same at runtime, you have to manage the visibility property view on the button and the loading view yourself.

    You can manage the visibility of the button and the loading animation based on your loading state.

    Somewhere in your code you will have loading logic and a view state.
    Let’s assume there is a boolean flag in your state that informs your view (activity/fragment) whether you are currently loading.

    Whenever you click on login (or start a login), you can set the flag to true. Whenever your login operation finishes you can set the flag to false.

    In your activity/fragment you can observe the flag and change the visibility of your views accordingly.

      loginButtonView.isVisible = !loading
      loadingAnimationView.isVisible = loading
    

    The following is an option how it can be done, but there are many options, so consider this more as pseudo code. Ideally, you will have a ViewModel that manages the state of the view. In the ViewModel you can have a state object that is exposed to the view and consumed by it.
    In my example, I will show it with Flow, but you can utilise different methods.

    class MyViewModel : ViewMode() {
    
     data class State(val loading: Boolean = false)
    
     private val _state = MutableStateFlow(State())
     val state: Flow<State> = _state
    
     fun dispatchLogin() {
       viewModelScope.launch() {
         _state.value = _state.value.copy(loading = true)
         // do some work
         _state.value = _state.value.copy(loading = false)
       }
     }
    

    In your activity you can subscribe to your state flow and your button to dispatch the action.

    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
    
      viewModel.state
        .map { it.loading }
        .onEach { updateViewState(it) }
        .launchIn(lifecycleScope)
    
      loginButtonView.setOnClickListener { viewModel.dispatchLogin() }
    
    
    }
    
    // ...
    
    fun updateViewState(loading: Boolean) {
      loginButtonView.isVisible = !loading
      loadingAnimationView.isVisible = loading
    }
    
    
      
    
    Login or Signup to reply.
  2. It’s elevation issue, you have 2 options:

    • Completely hide your button by changing visibility in your code.
    • Set the elevation of loading_animation to a value higher than the default button elevation. Check this link from material design for component’s default elevation

    A button has a resting elevation = 2dp, so set your loading_animation‘s elevation higher than that and it will cover everything

    android:elevation="4dp"
    
    Login or Signup to reply.
  3. It is due to elevation issue.Below are two possible solutions

    1. you can change the Button with Textview and consider the Textview as button itself while coding.

          <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"
           tools:context=".MainActivity">
      
       <androidx.appcompat.widget.AppCompatTextView
           android:id="@+id/log_in_button"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginTop="30dp"
               android:layout_marginStart="30dp"
               android:layout_marginEnd="30dp"
               android:background="#F5931D"
               android:text="login"
               android:textAllCaps="false"
               android:textColor="@color/white"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toTopOf="parent" />
          <View
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/black"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              />
      
       </androidx.constraintlayout.widget.ConstraintLayout>
      
    2. You can handle the visibility of Button by setting its value gone or visible

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search