Rishabh Agrawal
Rishabh Agrawal

Reputation: 2106

how can i put date and time picker in a dialog in android

i an new in android and struggling with how can i add these two date picker and time picker in a single dialog box please guide me how can i do it i created a dialog box and use a function setview to inflate it but i got the exception

            AlertDialog.Builder dialog = new AlertDialog.Builder(
                    MainActivity.this);
            dialog.setTitle("Begning");
            dialog.setPositiveButton("ok",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    });
            dialog.setNegativeButton("cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
             View pickers=getLayoutInflater().inflate(R.id.linearLayout1,
             null);
            dialog.setView(picker);

            dialog.show();

        }
    });

above is my code and hear is the xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

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

enter image description here

i want to create something like this please help me

    10-06 15:49:24.261: E/Trace(9287): error opening trace file: No such file or directory (2)
10-06 15:49:27.434: W/dalvikvm(9287): threadid=1: thread exiting with uncaught exception (group=0x411903a8)
10-06 15:49:27.454: E/AndroidRuntime(9287): FATAL EXCEPTION: main
10-06 15:49:27.454: E/AndroidRuntime(9287): android.content.res.Resources$NotFoundException: Resource ID #0x7f080000 type #0x12 is not valid
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2150)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.content.res.Resources.getLayout(Resources.java:860)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.example.datetimedialog.MainActivity$1.onClick(MainActivity.java:51)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.View.performClick(View.java:4102)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.View$PerformClick.run(View.java:17085)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Handler.handleCallback(Handler.java:615)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Looper.loop(Looper.java:155)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.app.ActivityThread.main(ActivityThread.java:5520)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at java.lang.reflect.Method.invoke(Method.java:511)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at dalvik.system.NativeStart.main(Native Method)

this is the logcat which i got while running

Upvotes: 3

Views: 3707

Answers (2)

starkshang
starkshang

Reputation: 8578

You should use R.layout.linearLayout1,not R.id.linearLayout1,like this:

View pickers=getLayoutInflater().inflate(R.layout.linearLayout1,null);

assuming the name of your layout file is linearLayout1.

Upvotes: 1

Ramesh Prasad
Ramesh Prasad

Reputation: 371

I have created a project on github for this. I have taken a different approach than yours and have created the custom dialog with date and time picker, by extending the dialog class.

https://github.com/ramesh130/DateTimePickerDialog

Upvotes: 1

Related Questions