Home > front end >  No response from a click event in Android Studio
No response from a click event in Android Studio

Time:11-22

I am building an app in Android Studio, which has a Main activity and a Settings activity. I am using the starter code from the PunchThrough BLE tutorial. To go straight to the GitHub repo, click here.

Expectation: In the Settings activity, I want the text in the LinearLayout to be clickable, and I want an action to be performed upon being clicked. In this case, I am trying to get a Toast message to pop up which describes which item was clicked.

Reality: When I land on the Settings activity page and try to click on the items in the LinearLayout (whether it's a Button view or a TextView in the XML), I don't get any response from the click. When I make the items Button views, I can see that the button is being clicked, but nothing happens. When I make the items TextViews, I can't see anything happening at all.

I would like to know if there is something preventing my Toast messages from being launched on the click event.

In case it is helpful, here is the relevant code from my Main activity.

class MainActivity : AppCompatActivity(R.layout.activity_main) {

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        settings_button.setOnClickListener {
            val intent = Intent(this, Settings::class.java)
            startActivity(intent)
        }
   }

Here is the code for my Settings activity.

class Settings : AppCompatActivity(R.layout.settings) {

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

        weather_data_textview.setOnClickListener {
            Toast.makeText(this, "Shows weather data", Toast.LENGTH_SHORT).show()
        }

        remove_device_textview.setOnClickListener {
            Toast.makeText(this, "Shows remove device dialogue", Toast.LENGTH_SHORT).show()
        }

        pool_maintenance_tips_textview.setOnClickListener {
            Toast.makeText(this, "Shows pool maintenance tips", Toast.LENGTH_SHORT).show()
        }

    }

}

And finally, here is the Settings activity XML.

<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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >

        <TextView
            android:id="@ id/weather_data_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:clickable="true"
            android:focusable="true"
            android:minHeight="48dp"
            android:text="@string/weather_data_label"
            android:textSize="30sp"
            />

        <TextView
            android:id="@ id/remove_device_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:clickable="true"
            android:focusable="true"
            android:minHeight="48dp"
            android:text="@string/remove_device_label"
            android:textSize="30sp"
            />

        <TextView
            android:id="@ id/pool_maintenance_tips_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:clickable="true"
            android:focusable="true"
            android:minHeight="48dp"
            android:text="@string/pool_maintenance_tips_label"
            android:textSize="30sp"
            />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Disclaimer: I would include a list of sources I've already looked at for this problem, but I have probably turned every single link purple on Google in my pursuit of an answer.

CodePudding user response:

You overrode the wrong overload of onCreate(). You used the one with PersistableBundle, which according to the documentation is only used when persistableMode is set to persistAcrossReboots.

As such, your onCreate() function is not getting called at all.

Change your override to the onCreate() with only one argument:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    //...
  • Related