Home > other >  My button is not working, how can i make it work on android studio?
My button is not working, how can i make it work on android studio?

Time:11-12

I made a button on my android studio project and I'm pretty sure the code is just fine. the problem is that the button does not respond - I click it, but it doesn't give me the wanted result. If you could help, please do. thank you so much for your time!

this is the code:

XML file code:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/Bdateofbirth"
android:text="Date of birth"
android:gravity="center"
android:layout_gravity="center"/>

onClick Code:

    @Override
public void onClick(View view) {
    if (view == Bdateofbirth) {
        dialogDOB();
    }

}

dialogDOB:

private void dialogDOB(){

    Calendar calendar = Calendar.getInstance();
    int d = calendar.get(Calendar.DAY_OF_MONTH);
    int m = calendar.get(Calendar.MONTH);
    int y = calendar.get(Calendar.YEAR);
    DatePickerDialog datePickerDialog = new DatePickerDialog(signup.this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            i1=i1 1;
            String date = i   "/"   i1   "/"   i2;
            Toast.makeText(signup.this, date, Toast.LENGTH_SHORT).show();
            Bdateofbirth.setText(date);
        }
    }, d, m, y);
    datePickerDialog.show();
}

THANK YOU

CodePudding user response:

In your Activity's XML file, add the attribute android:onclick="onClick" for your button to connect it to your onClick method. You don't need the @override.

  • Related