RANGE INPUT GENERATORXamarin Datepicker control is a very powerful control to handle date. In software development, Date Picker play a helpful role to maintain organization data. Xamarin platform allow to drag and drop Date Piker control on Xamarin Layout,but the problem is that , the control always visible full calendar mode on the view. You can also write on your Layout.axml file to display DatePiker control. But same problem , it will display full calendar on the view.
But in real life, we need a DatePicker to be open when required ,after you select the date,it should be closed.DatePicker will update corresponding value to the particular EditText box or a Textview.
Here is the step , how to do this.
Here we have taken a Edittext and a Imageview. We have added an image from drawable folder.Below is the axml code design.
xml version="1.0" encoding="utf-8"?>
We have handle the image click event and dynamically created a datepicker instance. Here is the code
Here is the full code of this activity
Below is the output
But in real life, we need a DatePicker to be open when required ,after you select the date,it should be closed.DatePicker will update corresponding value to the particular EditText box or a Textview.
Here is the step , how to do this.
Here we have taken a Edittext and a Imageview. We have added an image from drawable folder.Below is the axml code design.
xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:background="#194575">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="15sp"
android:textColor="#ffffff"
/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:textColor="#000000"
android:textSize="15sp"
android:background="#FFFFFF"
/>
<ImageView
android:id="@+id/btlnCalender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/calender"
/>
</LinearLayout>
We have handle the image click event and dynamically created a datepicker instance. Here is the code
ImageView
imgView = FindViewById(Resource.Id.btlnCalender);
imgView.Click
+= (sender, e) =>
{
System.DateTime
today = System.DateTime.Today;
DatePickerDialog
dialog = new
DatePickerDialog(this,
OnDateSet, today.Year, today.Month - 1, today.Day);
dialog.DatePicker.MinDate
= today.Millisecond;
dialog.Show();
};
Here is the full code of this activity
using
Android.App;
using
Android.Widget;
using
Android.OS;
using
Android.Support.V7.App;
namespace
App5
{
[Activity(Label
= "@string/app_name",
Theme = "@style/AppTheme",
MainLauncher = true)]
public
class
LoginActivity
: AppCompatActivity
{
protected
override
void
OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
ImageView
imgView = FindViewById(Resource.Id.btlnCalender);
imgView.Click
+= (sender, e) =>
{
System.DateTime
today = System.DateTime.Today;
DatePickerDialog
dialog = new
DatePickerDialog(this,
OnDateSet, today.Year, today.Month - 1, today.Day);
dialog.DatePicker.MinDate
= today.Millisecond;
dialog.Show();
};
}
public
void
OnDateSet(object
sender, DatePickerDialog.DateSetEventArgs e)
{
EditText
objDate = FindViewById(Resource.Id.editText);
objDate.Text
= e.Date.ToShortDateString();
}
}
}