Hello,
I’m a beginner on android studio and kotlin and I have a problem. I made a simple application to start, it works in simulation on a Nexus 5X with android 10.0, but not on a PIXEL 6 with android 12.0 and I don’t understand why.
I only want to display a toast when I press a button
Here is my MainActivity code:
package com.example.helloword2
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
// entrer principale de l'app
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main )
val rollButton: Button = findViewById(R.id.roll_button )
rollButton.setOnClickListener { rollDice() }
}
private fun rollDice()
{
Toast.makeText(this, "Button clicked", Toast.LENGTH_SHORT).show()
}
}
Hnd my activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="Hello World!"/>
<Button
android:id="@+id/roll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/roll_label"/>
</LinearLayout>
Does anyone know why this doesn’t work?
Thank you.