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
}
}
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>
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 ).
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.
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.
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 {
}
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.
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:
And then it worked.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With