Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast.show() not showing the message in Emulator

This seems to work on Android 6 Marshmallow device Xiaomi Redmi 3S. Seems like an emulator problem, but it isn't really misbehaving in any other sense.

I am a beginner in Android development and don't know much. I have already confirmed that notifications for my app is ON.

Specs: API Level : Android 4.4 (KitKat)

Android Studio version: 3.6.4

Emulator: Pixel XL API 28

All I attempted to do was a toast message would pop up telling how many times the button has been clicked.

Here's the MainActivity.kt:

class MainActivity : AppCompatActivity() {
    private var greetingCount = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnToastMessage.setOnClickListener {
            val suffix = when (++greetingCount) {
                1 -> "st"
                2 -> "nd"
                3 -> "rd"
                else -> "th"
            }
            val greetingMsg = "Welcome for the $greetingCount$suffix time(s)!"
            Toast.makeText(this@MainActivity, greetingMsg, Toast.LENGTH_LONG).show()
            Log.i(".MainActivity", "Clicked Button")
        }
    }
}

And here's the activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity"
    android:padding="30dp">
    <TextView
        android:id="@+id/txtHelloWorld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.314" />

    <Button
        android:id="@+id/btnToastMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtHelloWorld"
        app:layout_constraintVertical_bias="0.49" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

like image 724
zombiesauce Avatar asked Apr 17 '20 17:04

zombiesauce


2 Answers

Didn't know this could be done, but wiping data and cold-booting the device fixed it.

like image 126
zombiesauce Avatar answered Oct 16 '22 03:10

zombiesauce


Following are the steps you can follow (because I too got stuck):

  • Try restarting and cleaning your emulator device.
  • OR
  • You can run the app on your physical (android) device by setting the USB Debugging option to True.
like image 32
Muhammad Saad Avatar answered Oct 16 '22 05:10

Muhammad Saad