0

I'm trying to have a DatePicker dialog display when a user clicks on a button that says "Set your birthday", and what I need then is for the button to then display the user's selection back in the button. I have tried some of the solutions listed on this site with a TextView, but all to no avail. The DatePicker is displaying just fine, but I can't get the data to show up on the Button. I've tried these solutions: DatePicker not updating Textview in Android Display datepickers value in a textview Android: DatePicker and DatePicker Dialog How to transfer the formatted date string from my DatePickerFragment?

Here's my code:

First the DatePickerFragment:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

TheListener listener;

public interface TheListener {
    public void returnDate(String date);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(year, monthOfYear, dayOfMonth);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = sdf.format(c.getTime());

    if (listener != null) {
        listener.returnDate(formattedDate);
    }
}

}

Now, the Fragment that is calling it:

public class SignUpFragment extends Fragment implements MyBirthday.TheListener {

private Button btnBirthday;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_signup, container, false);

    return view;
}

@Override
public void onResume() {
    super.onResume();

    btnBirthday = (Button) getActivity().findViewById(R.id.signup_birthday_button);
    btnBirthday.setText("Set your birthday");
    btnBirthday.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment picker = new DatePickerFragment();
            picker.show(getFragmentManager(), "datePicker");
        }
    });
}

@Override
public void returnDate(String date) {
    btnBirthday.setText(date);
}

}

Any and all help will be greatly appreciated. If this is a duplicate question, I'm sorry for that, but I couldn't find a way to get my program to behave in the way I would like for it to. I couldn't find a solution on here.

Thanks in advance.

Community
  • 1
  • 1

1 Answers1

0

Try this. This will show you how to render the information in a textview, but you can transfer that to a button as well. You can find the full source code on http://www.ahotbrew.com/android-datepicker-example/

package com.ahotbrew.datepicker; 
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView; 
import java.util.Calendar;

public class MainActivity extends ActionBarActivity {

    public static TextView SelectedDateView;

    public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            SelectedDateView.setText("Selected Date: " + (month + 1) + "-" + day + "-" + year);
        }
    }

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

        SelectedDateView = (TextView) findViewById(R.id.selected_date);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }
}
Gurinder Singh
  • 867
  • 7
  • 9