Issue
I have an Activity with 3 buttons and I want each one of them to lead to others activities. I code with KOTLIN !
XML code
<Button
android:id="@+id/btn_1"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_marginTop="32dp"
android:background="@drawable/custom_buttons"
android:text="Characters"
android:textColor="@color/white"
android:textStyle="normal"
app:flow_verticalAlign="top"
app:layout_constraintBottom_toBottomOf="@+id/imageView4"
app:layout_constraintEnd_toEndOf="@+id/imageView4"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="0.31" />
<Button
android:id="@+id/btn_2"
android:layout_width="150dp"
android:layout_height="48dp"
android:background="@drawable/custom_buttons"
android:text="Matchs Up"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView4"
app:flow_verticalAlign="center"/>
<Button
android:id="@+id/btn_3"
android:layout_width="150dp"
android:layout_height="48dp"
android:background="@drawable/custom_buttons"
android:text="glossary"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/imageView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
MainActivity.KT
val button: Button = findViewById<Button>(R.id.btn_1)
val button: Button = findViewById<Button>(R.id.btn_2)
val button: Button = findViewById<Button>(R.id.btn_3)
button.setOnClickListener{
val intent = Intent(this, Page1::class.java)
startActivity(intent)
}
button.setOnClickListener{
val intent = Intent(this, Page2::class.java)
startActivity(intent)
}
button.setOnClickListener{
val intent = Intent(this, Page3::class.java)
startActivity(intent)
}
I have a red error on "val button : Button" -> Conflicting declarations: val button: Button, val button: Button.
What do I do wrong and is there a better way to do this please ?
Solution
You cannot have 2 or more variables with the same name in the same scope.
val button
means: "Create a NEW (immutable) variable called button". To solve the problem, assign a unique name to each variable. For example:
val button1: Button = findViewById<Button>(R.id.btn_1)
val button2: Button = findViewById<Button>(R.id.btn_2)
val button3: Button = findViewById<Button>(R.id.btn_3)
button1.setOnClickListener{
val intent = Intent(this, Page1::class.java)
startActivity(intent)
}
button2.setOnClickListener{
val intent = Intent(this, Page2::class.java)
startActivity(intent)
}
button3.setOnClickListener{
val intent = Intent(this, Page3::class.java)
startActivity(intent)
}
I assume that you are new to Kotlin. It's highly recommended to check out the official Kotlin docs and read the basic syntax or just Variables in Kotlin.
Answered By - YektaDev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.