-3

I'm trying to create an android application. One part is to make sure that toast message shows. It works when I'm running the app on the android studio emulator, but it doesn't show when I'm running on actual Samsung phone

It's about a thousand line code, so I don't think it's helpful to copy all of it. So, I'd like to know if there is any common issues that can make toast messages to show in emulator but not in actual phone.

-- EDIT --

This is one of the parts where I want to toast message

private fun addListenerOnImageButtonHelper(intent: Intent, sensorId: Int, sensorType: Int, sensorTypeToString: String) {
    findViewById<ImageButton>(sensorId).setOnClickListener {
        if (sensorManager.getDefaultSensor(sensorType) != null) {
            intent.putExtra("sensor", sensorType)
            startActivity(intent)
        } else {
            Toast.makeText(this,
                "$sensorTypeToString sensor is not available on this device",
                Toast.LENGTH_LONG).show()
        }
    }
}

-- EDIT --

Phone's setting to show notification was turned off. Problem Solved... Thanks

Mr.B
  • 29
  • 5

1 Answers1

0

You need add ActivityName.this when you create toast

Because you want to show toast from ImageButton click

private fun addListenerOnImageButtonHelper(intent: Intent, sensorId: Int, sensorType: Int, sensorTypeToString: String) {
    findViewById<ImageButton>(sensorId).setOnClickListener {
        if (sensorManager.getDefaultSensor(sensorType) != null) {
            intent.putExtra("sensor", sensorType)
            startActivity(intent)
        } else {
            Toast.makeText(ActivityName.this,
                "$sensorTypeToString sensor is not available on this device",
                Toast.LENGTH_LONG).show()
        }
    }
}

Else you need to pass Context as parameter in your function

Like

private fun addListenerOnImageButtonHelper(context: Context,intent: Intent, sensorId: Int, sensorType: Int, sensorTypeToString: String) {
    findViewById<ImageButton>(sensorId).setOnClickListener {
        if (sensorManager.getDefaultSensor(sensorType) != null) {
            intent.putExtra("sensor", sensorType)
            startActivity(intent)
        } else {
            Toast.makeText(context,
                "$sensorTypeToString sensor is not available on this device",
                Toast.LENGTH_LONG).show()
        }
    }
}