Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button onClick attribute is none if activity written in Kotlin

Follow this tutorial: Android - Start Another Activity if I made MainActivity.java button OnClick attribute has the sendMessage() method.

But if I made MainActivity.kt button OnClick attribute has nothing to show, just a none.

Is this an Android Studio 3 bug or I missed something for Kotlin?

Java mainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /** Called when the user taps the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
    }

}

Kotlin mainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    /** Called when the user taps the Send button  */
    fun sendMessage(view: View) {
        // Do something in response to button
    }
}

OnClick attribute

XML layout (Java and Kotlin project are the same)

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="ir.bigbang.vahid.myapplication.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="81dp" />
</android.support.constraint.ConstraintLayout>
like image 913
Vahid Avatar asked Oct 27 '17 08:10

Vahid


People also ask

How do you write onClick function in Kotlin?

Inside the onClick function, we use the Kotlin when statement, which is equivalent to switch in other languages. For the onClick function to be triggered, you must register the setOnClickListener over the button with the interface using the context( this ).

How do I get button text on Kotlin?

To set Android Button text, we can assign android:text XML attribute for Button in layout file with the required Text value. To programmatically set or change Android Button text, we can pass specified string to the method Button.

What is required activity in Kotlin?

getActivity() (or activity in case of Kotlin) is the method to access the activity that created the current fragment. It can be null so you need to check for nullability inside your code. requireActivity() a method that returns the non-null activity instance to fragment or throws an exception.


4 Answers

It seems like the designer does not support Kotlin yet. Here are some solution:

XML (Not Recommended)

Add the following line to your Button tag. This is exactly what the designer will do.

android:onClick="sendMessage"

Old Fashion

No need to add anything.

val button = findViewById<Button>(R.id.Button)
button.setOnClickListener {

}

kotlin-android-extensions (Recommended)

Add apply plugin: "kotlin-android-extensions" to your build.gradle

// button is the Button id
button.setOnClickListener {

}
like image 136
Joshua Avatar answered Oct 18 '22 19:10

Joshua


Your code will like this:

button.setOnClickListener(){
            Toast.makeText(this@MainActivity, "Its toast!", Toast.LENGTH_SHORT).show();
        }

Here import will:

import kotlinx.android.synthetic.main. activity_main.*

Here "button" is the id of that Button in .xml file. Here the advantage is no need to create Button object in your java class.

like image 33
Md. Sajedul Karim Avatar answered Oct 18 '22 19:10

Md. Sajedul Karim


Once defined the sendMessage class as :

/** Called when the user taps the Send button  */
fun sendMessage(view: View) {
    setContentView(R.layout.activity_second)
    // Do something in response to button
}

And also defined a second activity as:

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }
}

I added the SendMessage to the OnClick function: enter image description here

And then it worked.

like image 4
Alex Avatar answered Oct 18 '22 20:10

Alex


You can easily define this inside the XML itself. But using the android:onClick attribute is still a little expensive.

Instead you could consider using the Kotlin Android Extensions and synthetic properties:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    button.setOnClickListener {
        // Do something in response to button
    }
}
like image 2
tynn Avatar answered Oct 18 '22 20:10

tynn