2

This is my button in activity_main.xml

    <Button
        android:text="@string/fs"
        android:layout_width="154dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView" 
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp" 
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" 
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp" android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toTopOf="@+id/imageView" 
        app:layout_constraintHorizontal_bias="0.451"
        app:layout_constraintVertical_bias="0.069" android:id="@+id/button2"
        style="@style/Widget.AppCompat.Button" 
        android:background="@android:color/holo_green_dark"
        android:onClick="flowerpage"/>

This is the button being created I believe in the Mainactivity.kt

fun flowerpage(view: activity2) {
}

I am new to Kotlin, however, I used to HTML where you could connect two web pages together via HTML link, however, this doesn't seem to be as simple.

    button2.setOnClickListener {flower_button()}

this shows compiler error. am I missing an import..??

19
  • That function is not creating the Button. That's just the target function for your onClick attribute, though it currently has the wrong parameter type, and will crash if you try to use it. An Activity's Views are created from its layout in the setContentView() call. It's not really clear what your actual question is, however. Commented Dec 17, 2018 at 0:32
  • I understand that the command I am creating in mainactivity.kt is not the actual visual button, thanks tho.. Commented Dec 17, 2018 at 2:09
  • What is the actual problem, then? Commented Dec 17, 2018 at 2:11
  • So at this point in time I am taking the other answers advice and trying to create an onclicklistener button2.setOnClickListener {flowerpage()} however this causes a compiler error, Commented Dec 17, 2018 at 2:20
  • 1
    it worked thank you! I apologize for sounding so novice! Commented Dec 17, 2018 at 4:21

1 Answer 1

2

With kotlin you just need to give your button an id in the XML, then you can do this:

btn_id_you_gave.setOnClickListener {doSomething()}

private fun doSomething() {...}

You don't need to do the OnClick thing in the XML you want to take max advantage of Kotlin.

Sign up to request clarification or add additional context in comments.

3 Comments

So when I do that what would I put into {} to make the app go to activity2? So far I can see that I am issueing button2 (buttonid) to dosomething, which means the user will dosomething once the press the button, what's the syntax to move to activity2 once they issue the dosomething command. setcontentview(activity2)?
i get compiler error button2.setOnClickListener {flower_button()} private fun flower_button(){ val intent = activity_main(application, activity_activity2::class.java) startActivity(intent) }
To go to another activity you can use something like this: startActivity(Intent(this, Activity2::class.java)) The intent needs a context, so if you're doing the intent in one activity you can use "this", if it's in a fragment you have to use "getcontext".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.