0

I am implementing a fragment that have where I have an edittext and I want to popup a date picker when I click on it I used the following link

Datepicker: How to popup datepicker when click on edittext

But change the following: I have added: private static final String TAG = "AppoinFragment"; and String myFormat = "MM/dd/yy"; SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

And replace nextField.requestFocus(); with fullname.requestFocus(); When running my application and clicking on the edittext nothing happened. Here is the code I used:

AppointFragment.java

package com.example.appointapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

public class AppointFragment extends Fragment{

    private static final String TAG = "AppoinFragment";
    EditText fullname;

    private Appointment mappoitnemnt;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mappoitnemnt= new Appointment();


}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_appoint, parent, false);



    fullname = (EditText)v.findViewById(R.id.datea_text);

    fullname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                DialogFragment datePickerFragment = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d(TAG, "onDateSet");
                        Calendar c = Calendar.getInstance();
                        c.set(year, month, day);

                        String myFormat = "MM/dd/yy"; 
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                        fullname.setText(sdf.format(c.getTime()));

                        fullname.requestFocus(); 
                    }
                };
                datePickerFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
            }
        }
    });


    return v;
}
}

DataPickerFragment.java

package com.example.appointapp;

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;

public  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);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {

    }
}

Please help me to solve the problem

Community
  • 1
  • 1
  • Is there anything else on the Fragment? It's possible that the EditText already has focus as such clicking on it won't trigger the listener. Try swapping it for an OnClickListener instead – Jahnold Dec 30 '15 at 21:30
  • yes i have added android:focusable="false" beaccue someone wrote tht we should added it. when i removed it it works.Thank you so much for the help – java progammera Dec 30 '15 at 21:36

0 Answers0