I have the following situation: I have a Dialog. Within this dialog, which is a Fragment, I have a Button. This Button calls a DatePickDialog (DatePickFragment). If I set the date, the method

onDateSet(DatePicker view, int year, int month, int day)

is called, if I am correct. (If not, please tell me!). Now I want to change the Text of the Button to the Date. I already have a method that transforms the date to a String. But I cannot figure out how I get from the onDateSet method to the Button in the xml file... I cannot use findViewById. I thought I could use the view (DatePicker) but I do not know how...

The Java File for the DatePicker is in a seperate package (tools).It would be perfect to reuse this method if it is needed somewhere else. But that is not so important to me at the moment.

Some more code: DatePickerFragment.java

package com.domain.app.tools;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;

import java.util.Calendar;

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
    public Dialog onCreateDialog(Bundle savedInstanceState)
        final Calendar myCalendar = Calendar.getInstance();
        int year = myCalendar.get(Calendar.YEAR);
        int month = myCalendar.get(Calendar.MONTH);
        int day = myCalendar.get(Calendar.DAY_OF_MONTH);

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

    public void onDateSet(DatePicker view, int year, int month, int day)
        //Here I am stuck
        dateToString(year, month, day);

    public String dateToString(int year, int month, int day)
        StringBuffer sb = new StringBuffer();
        sb.append(day + "." + month + "." + year);

        return sb.toString();


<?xml version="1.0" encoding = "utf-8"?>
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:id = "@+id/dialog_create"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".CreateDialogFragment"

        android:id = "@+id/dialog_create_name"
        android:layout_width = "match_parent"
        android:layout_height = "@dimen/textFieldHight"
        android:text = "@string/create_name"

        android:layout_width = "wrap_content"
        android:layout_height = "@dimen/textFieldHight"
        android:ems = "6"
        android:text = "@string/date"
        android:id = "@+id/create_date_button"
        android:onClick = "pickDate"
        android:layout_below = "@id/dialog_create_name"


MainActivity.java - Partially

public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener
    CreateDialogFragment createDialogFragment;

    protected void onCreate(Bundle savedInstanceState)

        homeFragment = new HomeFragment();

        fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();

    public void openCreate(View view)
        createDialogFragment = new CreateDialogFragment();
        createDialogFragment.show(fragmentManager, "TEST");


    public void pickDate(View v)
        DialogFragment datePicker = new DatePickerFragment();
        datePicker.show(getFragmentManager(), String.format(getResources().getString(R.string.pickDate)));

        ((EditText) v).setText();//Here I am stuck

I appreciate any help.


Related posts

Recent Viewed