How to return the date value from DatePickerDialog in Android?
android, datepicker
Solution
- You could create a public method in your DatePickerFragment to return the string.
- You could have a static variable in your MainActivity that this class writes to.
- You could use SharedPreferences to store the string.
I would go with the first option, it's the simplest if your application is basic.
There are multiple ways of going about this as you can see, so it's important to look at how the user interacts with your app and when that date is needed. The public return method is nice if you hold a reference to the Fragment in MainActivity and you don't need the data ASAP. The static variable is nice if you need the string changed as soon as the user chooses a date. The last method is wasteful and is least recommended, but there is no static "magic" being done.
Problem
I am new to android . I ve created a Date picker in android using following guide .http://developer.android.com/guide/topics/ui/controls/pickers.html ``` public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { StringBuilder sb = new StringBuilder(); public static String date; @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) { // Do something with the date chosen by the user sb.append(year); sb.append('-'); sb.append(month+1); sb.append('-'); sb.append(day); date = sb.toString(); System.out.println("The date is "+date); } ``` I need to return this date value (date = sb.toString()) to my MainActivity . Since the onDateSet method is void what should I do ? Additional Information - DatePickerDialog Triggers at the MainActivity class ,But not with single button click . There are several processes happens in side a single button , Date picker will triggers only when certain condition is met . I do not want to display the date value either . Just want it returned for further processing. Appreciate any kind of guidance Changed onDataset method and justshow() ``` public void onDateSet(DatePicker view, int year, int month, int day) { ``` // Do something with the date chosen by the user ``` sb.append(year); sb.append('-'); sb.append(month+1); sb.append('-'); sb.append(day); date = sb.toString(); MainActivity.newdate=sb.toString(); System.out.println("The date is "+MainActivity.newdate); ``` } public void justShow(){ ``` System.out.println("The date is "+MainActivity.newdate); ``` } This is the relevant Part From Main(After making changes suggested in first reply ) ``` DateToken mydate=new DateToken(); String test=dayvals.get(0); DialogFragment df=new DatePickerFragment(); if(test.equalsIgnoreCase("day")){ df.show(getSupportFragmentManager(), "DatePik"); } System.out.println("Date is on Main"+newdate); DatePickerFragment dpf=new DatePickerFragment(); dpf.justShow(); ``` newdate is the static String , but still outputs null. In both MainActivity and justShow methods . But in onDataSet method date outputs correctly