Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:13:11+00:00 2026-06-14T21:13:11+00:00

I have two date pickers which are connected to each other as when I

  • 0

I have two date pickers which are connected to each other as when I change the date of the first picker the second ones date is also changed automatically ,
the two pickers have a listener called on date change listener, as follows

  public class birthDate extends Activity{

    Calendar c = Calendar.getInstance();
    int currentYear = c.get(Calendar.YEAR); 
    int currentMonth = c.get(Calendar.MONTH);
    int currentDay = c.get(Calendar.DAY_OF_MONTH);

    TextView birthDatetv;
    DatePicker birthDayDatePicker,periodDatePicker;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  

        setContentView(R.layout.birthdate);

        birthDatetv = (TextView)findViewById(R.id.textViewDateBirth);

        birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
        periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);


        periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
        {

            @Override
            public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
                // TODO Auto-generated method stub
                birthDateCalculations();
            }
        });

        birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {

            @Override
            public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
                // TODO Auto-generated method stub

                periodDateCalculations();

                }
        });

}

As I mentioned before, the pickers are connected, so when I change one picker date the other is also changed so the two listeners for the two date pickers will be invoked,, I just want the listener that is involved with the picker that the user changed its date to be invoke ,, Any idea how to do that ?

Here is the methods that the listeners do if it may help

public void birthDateCalculations ()
{

    Calendar start = Calendar.getInstance();

    int periodYear = periodDatePicker.getYear();
    int periodMonth = periodDatePicker.getMonth();
    int periodDay = periodDatePicker.getDayOfMonth();

    start.set(periodYear, periodMonth, periodDay);
    birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

    Date periodDate = start.getTime();
    int daysToAdd = 280;
    Calendar cal = Calendar.getInstance();

    cal.setTime(periodDate);
    cal.add(Calendar.DAY_OF_MONTH,daysToAdd );
    System.err.println("-----" +cal.getTime());
    int birthYearAfterCalc = cal.getTime().getYear()+1900;
    System.err.println("birthYearAfterCalc-----" + birthYearAfterCalc);
    int birthMonthAfterCalc = cal.getTime().getMonth();
    System.err.println("birthMonthAfterCalc----" + birthMonthAfterCalc);
    int birthDayAfterCalc = cal.getTime().getDate();
    System.err.println("birthDayAfterCalc"+birthDayAfterCalc);

    //user edit period to get birth
    if( periodDay <= currentDay && periodMonth <= currentMonth && periodYear <= currentYear){
    //the program runs normally
    birthDayDatePicker.updateDate(birthYearAfterCalc, birthMonthAfterCalc, birthDayAfterCalc);

        }
        else {


        new AlertDialog.Builder(birthDate.this)

                        .setTitle("Wrong Data Input!")

                        .setMessage("Error in period date input")

                        .setNeutralButton("Ok",

                        new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,

                        int which) {

                        }

                        }).show();

          periodDatePicker.updateDate(currentYear,currentMonth, currentDay);
            birthDayDatePicker.updateDate(currentYear, currentMonth, currentDay);   


                    }


}

public void periodDateCalculations ()
{
    Calendar start2 = Calendar.getInstance();

    // get the chosen date from birth date picker
    int birthYear = birthDayDatePicker.getYear();
    int birthMonth = birthDayDatePicker.getMonth();
    int birthDay = birthDayDatePicker.getDayOfMonth();

    //set the chosen date to calendar instance

    start2.set(birthYear, birthMonth, birthDay);

    birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

    //get the date 
    Date birthDate = start2.getTime();
    //int constrain = currentMonth + 9;

    int daysToAdd = -280;

    //another instance from calendar 
    Calendar cal2 = Calendar.getInstance();

    //set birth date chosen to calendar
    cal2.setTime(birthDate);

    //add -280 day to birth day to get period date
    cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
    System.err.println("-----" +cal2.getTime());
    int periodYearAfterCalc = cal2.getTime().getYear()+1900;
    System.err.println("periodYearAfterCalc-----" + periodYearAfterCalc);
    int periodMonthAfterCalc = cal2.getTime().getMonth();
    System.err.println("periodMonthAfterCalc----" + periodMonthAfterCalc);
    int periodDayAfterCalc = cal2.getTime().getDate();
    System.err.println("periodDayAfterCalc"+periodDayAfterCalc);

    //edit birth date picker from user  
        if(birthDay >= currentDay && birthMonth >= currentMonth && birthYear >= currentYear){
            //the program runs normally
            periodDatePicker.updateDate(periodYearAfterCalc, periodMonthAfterCalc, periodDayAfterCalc);     

            }
            else{


                            new AlertDialog.Builder(birthDate.this)

                            .setTitle("Wrong Data Input!")

                            .setMessage("birth day input error")

                            .setNeutralButton("Ok",

                            new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,

                            int which) {


                            }

                            }).show();

                            birthDayDatePicker.updateDate(currentYear,currentMonth, currentDay);
                            periodDatePicker.updateDate(currentYear, currentMonth, currentDay); 


                        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:13:13+00:00Added an answer on June 14, 2026 at 9:13 pm

    what you could do is set the corresponding listener to null when the other one takes action, and then after the task, you could turn it back on.

    for example:

    periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
    {
    
        @Override
        public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
            // TODO Auto-generated method stub
            birthDayDatePicker.init(currentYear, currentMonth, currentYear, null);
            birthDateCalculations();
            restartBirthDayDatePickerListener(); 
            // ^here you turn back on that init command that we have just set to null.
        }
    });
    

    not a glamorous solution, but perhaps you can make heads or tails of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two DateTime variables first period and second period and with each period
I have two date pickers one for startDate and another one is for endDate.
HI i have two date pickers. datepicker1, datepicker2. I need datepicker1.date to compare with
I have two date picker components and a JComboBox component with their associated Action
I have two date pickers. A startdate and an enddate . The way I
I have two inputs which take a date such as 11/04/2010 (im using a
I've got two date pickers in one form. They have different id's so this
hey guys I have two divs- with id=form1 which is inline and the other
I have two datepicker fields, one is ' start date ' the other is
I have a compare validator which validates on two <telerik:RadDatePicker> Start date and End

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.