-1

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

}

tomerpacific
  • 4,704
  • 13
  • 34
  • 52

2 Answers2

0

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

}

0

You are basically setting text to the same edit text EdtCurrDate in your first and second dialog listeners.

so you have to change the second one to EdtCurrDate2

Note : i suggest using date patterns in order to retrieve date strings from a date object

Example :

public class DateUtils {

//Date patterns examples 
public static String UI_DATE_PATTERN = "EEE, dd MMMM yyyy, HH:mm";
public static String SIMPLE_DATE_PATTERN = "dd/MM/yyyy"; // <-- Your desired date pattern

//converts date to a string
public static String toString(Date date, String pattern) {
    try {
        return new SimpleDateFormat(pattern, Locale.getDefault()).format(date);
    } catch (Exception e) {
        return e.getMessage();
    }
}

//Gets the date from a string
public static Date fromString(String dateString, String stringPattern) {
    try {
        return new SimpleDateFormat(stringPattern, Locale.getDefault()).parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}
}

Usage :

 Date currentDate = Calendar.getInstance().getTime(); // <- Your date object

 String currentDateSimpleString = DateUtils.toString(currentDate, DateUtils.SIMPLE_DATE_PATTERN); 

Output : 05/22/2019

 String currentDateUiString = DateUtils.toString(currentDate, DateUtils.UI_DATE_PATTERN);

Output : Wed, 05 May 2019, 17:09

 Date dateFromSimpleString = DateUtils.fromString(currentDateSimpleString , DateUtils.SIMPLE_DATE_PATTERN); 

Output equals currentDate

 Date dateFromUiString = DateUtils.fromString(currentDateUiString, DateUtils.UI_DATE_PATTERN); 

Output equals currentDate

Note : To retrieve a date from a string, the pattern must have its exact same format otherwise it will throw an exception.

Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34