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
{
    @Override
    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);
    }

    @Override
    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();
    }
}

dialog_create.xml

<?xml version="1.0" encoding = "utf-8"?>
<RelativeLayout
    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"
    android:background="@color/create_bg_1"
    tools:context = ".CreateDialogFragment"
    >

    <EditText
        android:id = "@+id/dialog_create_name"
        android:layout_width = "match_parent"
        android:layout_height = "@dimen/textFieldHight"
        android:text = "@string/create_name"
        android:background="@color/white"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        />


    <Button
        android:layout_width = "wrap_content"
        android:layout_height = "@dimen/textFieldHight"
        android:ems = "6"
        android:text = "@string/date"
        android:background="@color/white"
        android:id = "@+id/create_date_button"
        android:onClick = "pickDate"
        android:layout_below = "@id/dialog_create_name"
        />


</RelativeLayout>

MainActivity.java - Partially

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

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

        homeFragment = new HomeFragment();

        fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
        findViewById(R.id.bottomButton_home).setBackgroundResource(R.drawable.ic_home);
    }



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

        updateSelectedButton(3);
    }




    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.

John

Related posts

Recent Viewed