Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How should I Implementing the android EditText to trigger the date chooser dialog and store the date?

I am working on an android application that requires a user to sign up. While signing up, the user will be required to provide their date of birth. I really don't know how to implement this but, i found it cleaner to implement an EditText to trigger the date chooser dialog and store the date.

My sign up xml code is below

    <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_horizontal"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".SignUpActivity">

    <ScrollView
        android:id="@+id/signInScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/signInFormWrappper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fullnameSignUpInput"
                android:hint="@string/fullnameHint"
                android:maxLines="1"
                android:singleLine="true"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:id="@+id/emailSignUpInput"
                android:hint="@string/emailHint"
                android:maxLines="1"
                android:singleLine="true"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:id="@+id/passwordSignUpInput"
                android:maxLines="1"
                android:singleLine="true"
                android:hint="@string/passwordHint"/>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="10"
                    android:inputType="date"
                    android:ems="10"
                    android:id="@+id/dobSignUpInput" />

                <!--android:src="@drawable/ic_datepicker"-->
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/imageButton"
                    android:cropToPadding="true"
                    android:onClick="selectDate"
                    android:contentDescription="@string/selectDate" />

            </LinearLayout>

            <Button
                android:id="@+id/signInButton"
                style="?android:textAppearanceMedium"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/signUpButtonLabel"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/signInActivityTriggerLabel"
                android:id="@+id/signInActivityTrigger"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="10dp"
                android:padding="5dp"
                android:layout_gravity="center_horizontal" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

1 Answer

Using the date dialog box must be defined in the java code. Dialog boxes have their own classes and methods. Attaching this to an edittext is not recommended as edittext will display the keyboard when selected, you can suppress this but kinda removes the functionality of edittext. Why not create a textview and button and set an onclicklistener and run a dialog box from there. I have attached a useful webpage on dialogs below.

http://developer.android.com/guide/topics/ui/dialogs.html