Reputation: 13
I was working on a 2nd screen for main activity, the 2nd screen(screen_overlay.xml) was something like an pop up overlay screen that the user can close anytime when pressed the textView.
the problem that i encounter is, everytime I click the "close screen" text, the app always crashed. i've been looking at some references about this, but almost all of the solution end up with the app crashed.
most of the solution that i found was written in Java, so i tried to adapt it in Kotlin, i still not sure where do i miss something or there is something wrong in the kotlin code below.
below is the xml code,
screen_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.app.testApp.feature.screenView">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/screen_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView_close_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="CLOSE SCREEN"
android:clickable="true"
android:onClick="hideScreen"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
below is the kotlin code,
screenView.kt
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.app.testApp.R
import kotlinx.android.synthetic.main.screen_overlay.*
class TutorialDialog : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.screen_overlay)
}
fun hideScreen(v: View?){
val v:TextView = findViewById(R.id.textView_close_screen)
v.setOnClickListener{
screen_background.visibility = View.GONE
}
}
}
Anyone help to fix this issue will be appreciated, Thank you.
Upvotes: 1
Views: 412
Reputation: 2731
I assume you want to hide the view once it's clicked:
fun hideScreen(v: View) = v.visibility = View.GONE
Upvotes: 0
Reputation: 3268
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.app.testApp.R
import kotlinx.android.synthetic.main.screen_overlay.*
class TutorialDialog : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.screen_overlay)
textView_close_screen.setOnClickListener{
background.visibility = View.GONE
}
}
}
Upvotes: 1