Dev
Dev

Reputation: 78

Need help in xamarin.android with timepicker

I am new in developing android apps using Xamarin. Am developing a app which contains many dialog and activities, one of which dialog has layout like this, a timepicker, two buttons labeled as "CANCEL" and "OK" and a textview. I want to do something like this, when user click on "OK" button the time selected in timepicker will display on textview. (or i want to extract value of timepicker in button click event)

Upvotes: 0

Views: 1399

Answers (1)

XTL
XTL

Reputation: 1452

It's pretty simple. Check this out:

Your Main.axml(layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TimePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/picker" />
    <Button
        android:text="OK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnOK" />
    <Button
        android:text="Cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCancel" />
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt"
        android:textSize="40dp" />
</LinearLayout>  

and your OnCreate() method must contains this:

  protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            Button OK = FindViewById<Button>(Resource.Id.btnOK);
            Button Cancel = FindViewById<Button>(Resource.Id.btnCancel);
            TimePicker picker = FindViewById<TimePicker>(Resource.Id.picker);
            TextView txt = FindViewById<TextView>(Resource.Id.txt);

            OK.Click += (sender, e) => 
                {
                    //getting values from TImePicker via CurrentHour/CurrentMinutes
                    txt.Text = String.Format("{0} : {1}",picker.CurrentHour,picker.CurrentMinute);
                };

        }

Upvotes: 2

Related Questions