Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize DatePicker. Android

is there the way to customize a DatePicker? That is my Layout-Code.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" 
        android:orientation="vertical">
        <DatePicker 
            android:id="@+id/ad_date_picker"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_margin="5dp"/>
        <TimePicker         
            android:id="@+id/ad_time_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_margin="5dp"/>
    </LinearLayout>
</ScrollView>

In that case the date format of the DatePicker is MM-DD-YYYY and I'd like to have it in DD-MM-YYYY, and if it's also possible I'd like to have month's shortname instead of number.

I've tried to find, how to do it, but without success.

Thank you,

Mur

like image 856
Tima Avatar asked Dec 01 '10 14:12

Tima


2 Answers

You can't customize the default one.

From User Interface page:

Android provides a set of fully implemented widgets, like buttons, checkboxes, and text-entry fields, so you can quickly build your UI. Some widgets provided by Android are more complex, like a date picker, a clock, and zoom controls. But you're not limited to the kinds of widgets provided by the Android platform. If you'd like to do something more customized and create your own actionable elements, you can, by defining your own View object or by extending and combining existing widgets.

Read more in Building Custom Components.

like image 123
Pentium10 Avatar answered Sep 20 '22 22:09

Pentium10


try this code

put below code in xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linear_bottom_par"
        android:background="@color/gray_dark_more"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <DatePicker
                    android:id="@+id/dlgDatePicker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TimePicker
                    android:id="@+id/timePicker1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </ScrollView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_bottom_par"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/gray_dark_more"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/linear_select_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_padding_10"
            android:layout_marginEnd="@dimen/margin_padding_20"
            android:layout_marginLeft="@dimen/margin_padding_20"
            android:layout_marginRight="@dimen/margin_padding_20"
            android:layout_marginStart="@dimen/margin_padding_20"
            android:layout_marginTop="@dimen/margin_padding_10"
            android:background="@drawable/orange_ripple_effect"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="@dimen/margin_padding_15">

            <TextView
                android:id="@+id/tv_selecttime"
                style="@style/textappearance_large_white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/select_time"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

put below code in your activity or fragment

        final Dialog dialog = new Dialog(ConfirmBookingActivity.this, android.R.style.Theme_Holo_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_date_picker);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        final DatePicker dp = (DatePicker) dialog.findViewById(R.id.dlgDatePicker);
        final TimePicker tp = (TimePicker) dialog.findViewById(R.id.timePicker1);

        LinearLayout linear_select_time = (LinearLayout) dialog.findViewById(R.id.linear_select_time);

        dp.setCalendarViewShown(false);
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
        cal.add(Calendar.HOUR_OF_DAY, 1);

        dp.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
                calDay = dayOfMonth;
                calMonth = month + 1;
                calYear = year;

                checkAndUpdateValue(tp);
            }
        });


        linear_select_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

            });
like image 29
gpuser Avatar answered Sep 24 '22 22:09

gpuser