zoe vas zoe vas - 2 months ago 40
Android Question

Show dialogfragment inside a fragment

I want to display a dialogfragment which implements the datepickerdialog inside a fragment!
I have a button whose listener displays the datepickerdialog.
The button is defined in fragment layout as:

<Button
android:id="@+id/buttonSelectDate"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:onClick="showDatePickerDialog" />


The code of the fragment class is the following(my problem is in showDatePickerDialog function):

public class SchedulerChooserFragment extends Fragment {

private int year;
private int month;
private int day;

TextView dateText;
View rootView;

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

LayoutInflater lf = getActivity().getLayoutInflater();
rootView = lf.inflate(R.layout.fragment_scheduler_chooser, container,false);

return rootView;
}



// display current date
public void showCurrentDateOnView() {

dateText = (TextView) rootView.findViewById (R.id.textDate);

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

System.out.println("year" + year);
// set current date into textview
dateText.setText(month + 1 +"-"+day+"-"+year);

// set current date into datepicker


}

public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment(dateText);
newFragment.show(getSupportFragmentManager(), "datePicker");
}

}


However, the getSupportFragmentManager does not exist inside a fragment.
I have tried getActivity().getSupportFragmentManager but the app crashes.
Also, I have tried getChildFragmentManager, and the app crashes too.

The datePickerFragment class is the folllowing:

import java.util.Calendar;

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

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

TextView txtDate;


public DatePickerFragment(TextView txtDate) {
super();
this.txtDate = txtDate;
}

@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) {
/*txtDate.setText(year+" " +month +" "+ date);*/
}



}


I am new to android! Sorry, if my issue is obvious.

Thanks

Answer

I found the error. The Log output threw a java.lang.IllegalStateException: about not finding the showDatePickerDialog which was defined as android:onClick="showTimePickerDialog" for a button.

But, it is not possible to define onclick methods like this inside fragments. So i changed the SchedulerChooserFragment to :

public  class SchedulerChooserFragment extends Fragment   implements OnClickListener{

        View rootView;
        TextView textDisplayDate;
        private int year;
        private int month;
        private int day;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.fragment_scheduler_chooser, container,false);
            showCurrentDateOnView();
            Button b = (Button) rootView.findViewById(R.id.buttonSelectDate);
            b.setOnClickListener(this);
            return rootView;
        }

     // display current date
            public void showCurrentDateOnView() {

                textDisplayDate = (TextView) rootView.findViewById(R.id.textDate);


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

                // set current date into textview
                textDisplayDate.setText(year+" "+month+" "+day);


            }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.buttonSelectDate:

                     DialogFragment newFragment = new DatePickerFragment(textDisplayDate);
                    newFragment.show(getChildFragmentManager(), "datePicker");

                    break;
                }

            }


    }
Comments