K. Ajay
K. Ajay

Reputation: 417

How to set two date pickers in Xamarin Android?

I am making an app by following this tutorial - Link my question is how to add two date pickers for two different buttons? in my case for eg: one for Start date and one for End Date.

Code update - (initialize variables)

    private System.Boolean sd;
    private System.Boolean ed;
    private const int Start_Date = 1;
    private int SDYear = 2018, SDMonth = 1, SDDay = 1;
    private const int End_Date = 2;
    private int EDYear = 2018, EDMonth = 1, EDDay = 1;
    start_date = FindViewById<EditText>(Resource.id.start_date_et)
    end_date = FindViewById<EditText>(Resource.id.end_date_et)

          (click events )


Start_date.Click += delegate {

                 sd = true;
                 ed = false; 
                ShowDialog(Resource.Id.start_date_et);
            };

            End_date.Click += delegate {

                 ed = true;
                 sd = false;
                ShowDialog(Resource.Id.end_date_et);
            };

(override methods for dialog)

 protected override Dialog OnCreateDialog(int id)
        {
            switch (id)
            {
                case (Resource.Id.start_date_et):
                    {
                        return new DatePickerDialog(this, Resource.Style.DialogTheme, this, SDYear, SDMonth, SDDay);
                        break;
                    }

                case (Resource.Id.end_date_et):
                    {
                        return new DatePickerDialog(this, Resource.Style.DialogTheme, this, EDYear, EDMonth, EDDay);
                        break;
                    }

                default:
                    break;
            }
            return null;

        }

(ondataset interface)

 public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
        {
          if(sd == true){
            this.SDYear = year;
            this.SDMonth = month;
            this.SDDay = dayOfMonth;
            start_date_text = (SDMonth + 1).ToString() + "-" + SDDay.ToString() + "-" + SDYear.ToString();
            Toast.MakeText(this, (SDMonth + 1)
                + "-" + (SDDay) + "-" + SDYear, ToastLength.Short).Show();
           }else if(ed == true){


            this.EDYear = year;
            this.EDMonth = month;
            this.EDDay = dayOfMonth;
            end_date_text = (EDMonth + 1).ToString() + "-" + EDDay.ToString() + "-" + EDYear.ToString();
            Toast.MakeText(this, (EDMonth + 1)
                + "-" + (EDDay) + "-" + EDYear, ToastLength.Short).Show();

}

        }

I want to go by this method but whenever i try to show the start_date_text and end_date_text on a textview, either start_date_text or end_date_text shows up but not both.

Upvotes: 0

Views: 345

Answers (2)

Naveen Tiwari
Naveen Tiwari

Reputation: 361

In OnDateSet Method you are assigning both text value with date without putting any condition, you should check with id which button is clicked and based on that id you should update you StartDate TextView and EndDate TextView.

public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
          {          

   if(StartDateIdClicked){
        this.SDYear = year;
        this.SDMonth = month;
        this.SDDay = dayOfMonth;
        start_date_text = (SDMonth + 1).ToString() + "-" + SDDay.ToString() + "-" + SDYear.ToString();
        Toast.MakeText(this, (SDMonth + 1)
            + "-" + (SDDay) + "-" + SDYear, ToastLength.Short).Show();

}

    elseif(EndDateIdClicked){
        this.EDYear = year;
        this.EDMonth = month;
        this.EDDay = dayOfMonth;
        end_date_text = (EDMonth + 1).ToString() + "-" + EDDay.ToString() +"-" + EDYear.ToString();
        Toast.MakeText(this, (EDMonth + 1)
            + "-" + (EDDay) + "-" + EDYear, ToastLength.Short).Show();
     }
   }

And You are assigning same value for End_Date=1 and Start_Date=1,when you are calling ShowDialog(Start_Date); and ShowDialog(End_Date); then both OnCreateDialog(), switch case will be true for Start_Date and End_Date

Upvotes: 0

Bikash
Bikash

Reputation: 368

Your cs code for date picker

var fragView = this.BindingInflate(Resource.Layout.yourView, null);
        dateselect = fragView.FindViewById<EditText>(Resource.Id.txtdeliverydateselect);

dateselect.Click += delegate
        {
            DateTime currentdate;
            currentdate = DateTime.Now.ToLocalTime();
var dialog = new DatePickerDialogFragment(Activity, currentdate, this);
            dialog.Show(FragmentManager, "date");
        };

your axml code

<LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="360dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">
            <EditText
                android:id="@+id/txtdeliverydateselect"
                android:hint="Monday 31 October"
                android:layout_width="fill_parent"
                android:layout_weight="0.9"
                 android:textSize="18dp"
                />
</LinearLayout>

User the same of other date picker

Upvotes: 2

Related Questions