Itsuki
Itsuki

Reputation: 3

Android studio Kotlin multiple buttons to open multiple activity

I have 5 buttons. I got one to work but i can't make the other 4 work. Here is my code for the first button:

//MainActivity.kt
fun next(view: View){
        val intent = Intent(this, SecondActivity::class.java)
        startActivity(intent)
    }

Then i just put a onclick on my first button.

//fragment_dashboard.xml
android:onClick="next"

Im expecting the other 4 buttons to open a specific activity like button1 will open the activity "Chapter I". I've been trying to use multiple intents but it doesn't work as well. Any suggestions on how i can make onClick listeners for the other buttons?

Here is the fragment_dashboard.xml where i've put the 5 buttons:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="fragments.DashboardFragment">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="85dp"
        android:layout_marginEnd="149dp"
        android:text="Chapter I"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="next" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="146dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="146dp"
        android:text="Chapter II"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="143dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="144dp"
        android:text="Chapter III"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="143dp"
        android:layout_marginTop="53dp"
        android:layout_marginEnd="144dp"
        android:text="Chapter IV"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="146dp"
        android:layout_marginTop="47dp"
        android:layout_marginEnd="146dp"
        android:layout_marginBottom="208dp"
        android:text="Chapter V"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4" />


</androidx.constraintlayout.widget.ConstraintLayout>

!UPDATE! I tried another method to set onclick for my buttons. It doesn't show any error but everytime I run my app it keeps crashing the moment i run it.

val button = findViewById<Button>(R.id.button)
        button.setOnClickListener{
            val intent = Intent(this, ChapterIAct::class.java)
            startActivity(intent)
        }
        val button2 = findViewById<Button>(R.id.button2)
        button2.setOnClickListener {
            val intent1 = Intent(this, ChapterIIAct::class.java)
            startActivity(intent1)
        }

        val button3 = findViewById<Button>(R.id.button3)
        button3.setOnClickListener {
            val intent2 = Intent(this, ChapterIIIAct::class.java)
            startActivity(intent2)
        }
val button4 = findViewById<Button>(R.id.button4)
        button4.setOnClickListener{
            val intent3 = Intent(this, ChapterIVAct::class.java)
            startActivity(intent3)
        }

        val button5 = findViewById<Button>(R.id.button5)
        button5.setOnClickListener{
            val intent4 = Intent(this, ChapterVAct::class.java)
            startActivity(intent4)
        }

Button onClicks

!UPDATE! I put my onclick inside the onCreateView inside the fragment.

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val button = view.findViewById<Button>(R.id.button)
        button.setOnClickListener{
            val intent = Intent(this, ChapterIAct::class.java)
            startActivity(intent)
        }
        return view
    }

but the Intent has an error of "None of the following functions can be called with the arguments supplied. (Context!, Class<*>!) defined in android.content.Intent (String!, Uri!) defined in android.content.Intent"

Intent error

Upvotes: 0

Views: 1023

Answers (1)

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2377

I just noticed that these buttons all inside the fragment. You are suppose to do like this.

Inside the onCreateView function

    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_blank, container, false)
    val button = view.findViewById<Button>(R.id.button) //add view
    button.setOnClickListener{
        val intent = Intent(context, ChapterIAct::class.java)
        startActivity(intent)
    }
    //all here same like at top
    return view

Upvotes: 0

Related Questions