Skip to content
Advertisement

Setting date to a shared prefernece in android studio

Very new to android studio and this is the first app I’ve built so all help would be appreciated. I have a calendar set up and im trying to save the selected date into a shared preference. My problem is that the methods I’ve created are not showing up as onclick options in my xml file.

Here is the code I have so far:

JavaScript

}

Advertisement

Answer

Unless you are using view databinding, the (old) way of setting an android:onClick="goSaveDate" on Views in XML layouts is deprecated.

Deprecated: View actually traverses the Context hierarchy looking for the relevant method, which is fragile and restricts bytecode optimizers such as R8. Instead, use View.setOnClickListener.

You can set an OnClickListener from inside your activity class, for example inside the onCreate method.

JavaScript

Replace R.id.button_save_date and R.id.button_show_date with the ids of your views, on which you would like to set the click listeners on. If those views (buttons, components in XML) still do not have an android:id="@+id/..." attribute set in the XML layout, then add that first.

The syntax :: (this::goSaveDate) is a method reference.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement