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:

public class calendar extends AppCompatActivity {


CalendarView calendarView;
SharedPreferences booking_date;
String myDate, txt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar);

    calendarView = findViewById(R.id.id_calendarView);
    

    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {

             myDate = dayOfMonth + " " + (month + 1) + " " + year; // note the months start at 0

            //redDateText.setText(myDate);

        }

    });
}

public void goSaveDate(View view){

    booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);

   
    txt = myDate;

    SharedPreferences.Editor editor = booking_date.edit();
    editor.putString("key_date", txt);
    editor.commit();

    Toast.makeText(getBaseContext(), "Date Saved!!", Toast.LENGTH_SHORT).show();


}

public void goShowDate(View view){
    booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);

    String name = booking_date.getString("key_date", null);
    TextView textView = findViewById(R.id.textView5);
    textView.setText("Your Next booking is on: " +name);

}


    @Override
    public void finish () {
        super.finish();
        overridePendingTransition(R.transition.slide_in_left, R.transition.slide_out_right);
    }

}

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.

// each click on R.id.button_save_date will call the goSaveDate method
findViewById(R.id.button_save_date).setOnClickListener(this::goSaveDate);

findViewById(R.id.button_show_date).setOnClickListener(this::goShowDate);

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