Search code examples
androidandroid-edittextandroid-datepicker

Date Picker Dialog Not Working Changing In One Text Make Change To Another


I want to gat date in textview by using DatePicker Dialog. I have get Current date in the EditText As the Activity open. I have Implemented the DatePicker Dialog on the EditTexts but When I try to change the date by Clicking on Edit Text, It make change date to another textview in the form...

Basically I have 2 EditTexts and I implemented DatePicker Dialog On Both..

Here is my XML File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:orientation="vertical">


<TextView
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/txtclientname"
    />

<EditText
    android:layout_marginTop="80dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:id="@+id/currdate"
    android:textColor="@color/colorText"
    />

<EditText
    android:layout_marginTop="80dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/currdate1"
    android:layout_toRightOf="@+id/currdate"
    android:textColor="@color/colorText"
    android:inputType="date"
    />

Here is my Jave File

{

TextView EdtName;
EditText EditCurrDate,EdtCurrDate2;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view =  inflater.inflate(R.layout.invoice, container, false);

    String date_n = new SimpleDateFormat("MMM / dd / yyyy", Locale.getDefault()).format(new Date());

    EditCurrDate=view.findViewById(R.id.currdate);
    EdtCurrDate2=view.findViewById(R.id.currdate1);
    EdtName=view.findViewById(R.id.txtclientname);

    EditCurrDate.setText(date_n);

    EdtCurrDate2.setText(date_n);

    EditCurrDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year = mcurrentDate.get(Calendar.YEAR);
            int month = mcurrentDate.get(Calendar.MONTH);
            int day = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year, month, day);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });

    EdtCurrDate2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year1 = mcurrentDate.get(Calendar.YEAR);
            int month1 = mcurrentDate.get(Calendar.MONTH);
            int day1 = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year1, month1, day1);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });


    return view;
}

}


Solution

  • I just using the same edittext in the both click listeners

     {
    
    TextView EdtName;
    EditText EditCurrDate;
    EditText EdtCurrDate2;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(R.layout.invoice, container, false);
    
        String date_n = new SimpleDateFormat("MMM / dd / yyyy", Locale.getDefault()).format(new Date());
    
        EditCurrDate=view.findViewById(R.id.currdate);
        EdtCurrDate2=view.findViewById(R.id.currdate1);
        EdtName=view.findViewById(R.id.txtclientname);
    
        EditCurrDate.setText(date_n);
    
        EdtCurrDate2.setText(date_n);
    
        EditCurrDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar mcurrentDate=Calendar.getInstance();
                int year = mcurrentDate.get(Calendar.YEAR);
                int month = mcurrentDate.get(Calendar.MONTH);
                int day = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    
                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                        // TODO Auto-generated method stub
    
                        Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                        EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                    }
                },year, month, day);
                mDatePicker.setTitle("Select date");
                mDatePicker.show();
            }
        });
    
        EdtCurrDate2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar mcurrentDate=Calendar.getInstance();
                int year1 = mcurrentDate.get(Calendar.YEAR);
                int month1 = mcurrentDate.get(Calendar.MONTH);
                int day1 = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    
                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                        // TODO Auto-generated method stub
    
                        Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                        EdtCurrDate2.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                    }
                },year1, month1, day1);
                mDatePicker.setTitle("Select date");
                mDatePicker.show();
            }
        });
    
    
        return view;
    }
    

    }