Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 18.51 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Custom DatePicker Dialog restrict to showing Future dates
  2. This datepickerdialog has the ability to restrict future date from given max date.Its works in on all api version of android.    
  3. //main.xml
  4.  
  5.     <?xml version="1.0" encoding="utf-8"?>
  6.     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.          >
  10.     <Button
  11.         android:text="DatePicker Dialog"
  12.         android:id="@+id/time_btn"
  13.       android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         />
  16.        <TextView
  17.  
  18.            android:layout_centerInParent="true"
  19.            android:id="@+id/date_txt"
  20.  
  21.           android:textStyle="bold"
  22.           android:textSize="25dp"
  23.           android:textColor="#ffffff"
  24.           android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.  
  27.            />
  28.  
  29.     </RelativeLayout>
  30.  
  31.     //customdatepickerdialog.xml
  32.  
  33.     <?xml version="1.0" encoding="utf-8"?>
  34.     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  35.         android:layout_width="fill_parent"
  36.         android:layout_height="wrap_content"
  37.          android:paddingBottom="1dp"
  38.         android:background="#ffffff"
  39.          >
  40.     <RelativeLayout
  41.         android:id="@+id/first"
  42.         android:layout_margin="1dp"
  43.         android:layout_width="fill_parent"
  44.         android:layout_height="wrap_content"
  45.         android:background="#000000"
  46.         >
  47.         <ImageView
  48.             android:id="@+id/clock"
  49.             android:layout_margin="2dp"
  50.             android:src="@drawable/clock"
  51.             android:layout_width="40dp"
  52.         android:layout_height="40dp"
  53.  
  54.  
  55.             />
  56.  
  57.          <TextView
  58.              android:id="@+id/actualtime"
  59.              android:layout_toRightOf="@+id/clock"
  60.              android:textColor="#ffffff"
  61.              android:text="Sunday, Jan 1, 1970"
  62.              android:textSize="20dp"
  63.             android:textStyle="bold"
  64.             android:padding="10dp"
  65.  
  66.             android:layout_width="wrap_content"
  67.         android:layout_height="wrap_content"
  68.              android:singleLine="true"
  69.             android:ellipsize="marquee"
  70.             android:marqueeRepeatLimit ="marquee_forever"
  71.             android:focusable="true"
  72.             android:focusableInTouchMode="true"
  73.             android:scrollHorizontally="true"
  74.  
  75.             />
  76.  
  77.  
  78.     </RelativeLayout>
  79.     <RelativeLayout
  80.         android:id="@+id/second"
  81.        android:layout_below="@+id/first"
  82.         android:layout_marginLeft="1dp"
  83.         android:layout_marginRight="1dp"
  84.         android:layout_marginBottom="1dp"
  85.         android:layout_width="fill_parent"
  86.         android:layout_height="wrap_content"
  87.         android:background="#000000"
  88.         >
  89.        <DatePicker
  90.           android:padding="5dp"
  91.             android:editable="false"
  92.             android:focusableInTouchMode="false"
  93.             android:gravity="center"
  94.             android:layout_width="fill_parent"
  95.             android:layout_height="wrap_content"
  96.             android:id="@+id/datepick"
  97.             />
  98.     </RelativeLayout>
  99.     <RelativeLayout
  100.         android:id="@+id/third"
  101.         android:layout_below="@+id/second"
  102.  
  103.         android:layout_marginLeft="2dp"
  104.         android:layout_marginRight="2dp"
  105.         android:layout_width="fill_parent"
  106.         android:layout_height="wrap_content"
  107.         android:background="#000000"
  108.         android:paddingBottom="5dp"
  109.         >
  110.         <Button android:textSize="20dp"
  111.             android:textStyle="bold"
  112.             android:id="@+id/set"
  113.             android:layout_margin="10dp"
  114.  
  115.              android:layout_width="130dp"
  116.         android:layout_height="wrap_content"
  117.         android:text="Set"
  118.         android:textColor="#000000"
  119.  
  120.  
  121.             />  
  122.             <Button android:textSize="20dp"
  123.             android:textStyle="bold"
  124.                  android:id="@+id/cancel"
  125.                 android:layout_toRightOf="@+id/set"
  126.             android:layout_margin="10dp"
  127.  
  128.              android:layout_width="135dp"
  129.         android:layout_height="wrap_content"
  130.         android:text="Cancel"
  131.         android:textColor="#000000"
  132.  
  133.  
  134.             />
  135.         </RelativeLayout>
  136.     </RelativeLayout>
  137.  
  138.     //CustomDatePickerDialogActivity.Java
  139.  
  140.     public class CustomDatePickerDialogActivity extends Activity {
  141.  
  142.         TextView tv;  
  143.         Button time_btn;
  144.         String date_selected="";
  145.         String checkdate="";
  146.         final Calendar c = Calendar.getInstance();
  147.         int maxYear = c.get(Calendar.YEAR);
  148.         int maxMonth = c.get(Calendar.MONTH);
  149.         int maxDay = c.get(Calendar.DAY_OF_MONTH);
  150.         int weekday= c.get(Calendar.DAY_OF_WEEK);
  151.          int minYear = 1900;
  152.          int minMonth = 0; // january
  153.          int minDay = 1;
  154.  
  155.         /** Called when the activity is first created. */
  156.         @Override
  157.         public void onCreate(Bundle savedInstanceState) {
  158.             super.onCreate(savedInstanceState);
  159.             requestWindowFeature(Window.FEATURE_NO_TITLE);
  160.             setContentView(R.layout.main);
  161.             tv=(TextView)findViewById(R.id.date_txt);
  162.             time_btn=(Button)findViewById(R.id.time_btn);
  163.             time_btn.setOnClickListener(new OnClickListener() {
  164.  
  165.                 public void onClick(View v) {
  166.                     // TODO Auto-generated method stub
  167.  
  168.                     showcustomdatepickerDialog();
  169.                 }
  170.             });
  171.  
  172.  
  173.         }
  174.     public void showcustomdatepickerDialog()
  175.     {
  176.         final Dialog dialog=new Dialog(CustomDatePickerDialogActivity.this);
  177.         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  178.         dialog.setContentView(R.layout.customdatepickerdialog);
  179.         final TextView actualtime=(TextView)dialog.findViewById(R.id.actualtime);
  180.          final DatePicker  BirthDateDP = (DatePicker)dialog.findViewById(R.id.datepick);
  181.         Button set=(Button)dialog.findViewById(R.id.set);
  182.         Button cancel=(Button)dialog.findViewById(R.id.cancel);
  183.         dialog.show();
  184.         String monthstring=getmonthString(maxMonth);
  185.         String weekstring=getweekString(weekday);
  186.         actualtime.setText(weekstring+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  187.            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  188.        imm.hideSoftInputFromWindow(BirthDateDP.getWindowToken(), 0);
  189.        setDisabledTextViews(BirthDateDP);
  190.  
  191.       if(tv.getText().toString().equalsIgnoreCase(""))
  192.       {
  193.        BirthDateDP.init(maxYear, maxMonth, maxDay, new OnDateChangedListener()
  194.        {
  195.  
  196.        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
  197.        {
  198.            if (year < minYear)
  199.            {
  200.                view.updateDate(minYear, minMonth, minDay);
  201.                String monthstring=getmonthString(minMonth);
  202.                Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  203.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  204.                  String day=f.format(date);
  205.  
  206.                actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  207.            }
  208.            else if (monthOfYear < minMonth && year == minYear)
  209.                {
  210.                view.updateDate(minYear, minMonth, minDay);
  211.                String monthstring=getmonthString(minMonth);
  212.                Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  213.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  214.                  String day=f.format(date);
  215.                actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  216.                }
  217.            else if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
  218.                {
  219.                view.updateDate(minYear, minMonth, minDay);
  220.                String monthstring=getmonthString(minMonth);
  221.                Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  222.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  223.                  String day=f.format(date);
  224.                actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  225.                }
  226.  
  227.            else if (year > maxYear)
  228.                {
  229.                view.updateDate(maxYear, maxMonth, maxDay);
  230.                String monthstring=getmonthString(maxMonth);
  231.                Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  232.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  233.                  String day=f.format(date);
  234.                actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  235.                }
  236.            else if (monthOfYear > maxMonth && year == maxYear)
  237.                {
  238.                view.updateDate(maxYear, maxMonth, maxDay);
  239.                String monthstring=getmonthString(maxMonth);
  240.                Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  241.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  242.                  String day=f.format(date);
  243.                actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  244.                }
  245.            else if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
  246.                {
  247.                view.updateDate(maxYear, maxMonth, maxDay);
  248.                String monthstring=getmonthString(maxMonth);
  249.                Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  250.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  251.                  String day=f.format(date);
  252.                actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  253.                }
  254.            else
  255.            {
  256.                view.updateDate(year, monthOfYear, dayOfMonth);
  257.                String monthstring=getmonthString(monthOfYear);
  258.                Date date= (new GregorianCalendar(year, monthOfYear, dayOfMonth)).getTime();
  259.                SimpleDateFormat f = new SimpleDateFormat("EEEE");
  260.                  String day=f.format(date);
  261.                actualtime.setText(day+","+" "+monthstring+" "+dayOfMonth+","+" "+year);
  262.            }
  263.        }});
  264.       }
  265.       else
  266.       {
  267.           String tmp[]=tv.getText().toString().split("-");
  268.           String monthistring=getmonthString(Integer.parseInt(tmp[0].trim())-1);
  269.           Date date= (new GregorianCalendar(Integer.parseInt(tmp[2].trim()), Integer.parseInt(tmp[0].trim())-1,Integer.parseInt(tmp[1].trim()))).getTime();
  270.           SimpleDateFormat f = new SimpleDateFormat("EEEE");
  271.             String day=f.format(date);
  272.  
  273.           actualtime.setText(day+","+" "+monthistring+" "+Integer.parseInt(tmp[1].trim())+","+" "+Integer.parseInt(tmp[2].trim()));
  274.           BirthDateDP.init(Integer.parseInt(tmp[2].trim()), Integer.parseInt(tmp[0].trim())-1,Integer.parseInt(tmp[1].trim()), new OnDateChangedListener()
  275.           {
  276.  
  277.           public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
  278.           {
  279.               if (year < minYear)
  280.               {
  281.                   view.updateDate(minYear, minMonth, minDay);
  282.                   String monthstring=getmonthString(minMonth);
  283.                   Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  284.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  285.                     String day=f.format(date);
  286.  
  287.                   actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  288.               }
  289.               else if (monthOfYear < minMonth && year == minYear)
  290.                   {
  291.                   view.updateDate(minYear, minMonth, minDay);
  292.                   String monthstring=getmonthString(minMonth);
  293.                   Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  294.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  295.                     String day=f.format(date);
  296.                   actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  297.                   }
  298.               else if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
  299.                   {
  300.                   view.updateDate(minYear, minMonth, minDay);
  301.                   String monthstring=getmonthString(minMonth);
  302.                   Date date= (new GregorianCalendar(minYear, minMonth, minDay)).getTime();
  303.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  304.                     String day=f.format(date);
  305.                   actualtime.setText(day+","+" "+monthstring+" "+minDay+","+" "+minYear);
  306.                   }
  307.  
  308.               else if (year > maxYear)
  309.                   {
  310.                   view.updateDate(maxYear, maxMonth, maxDay);
  311.                   String monthstring=getmonthString(maxMonth);
  312.                   Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  313.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  314.                     String day=f.format(date);
  315.                   actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  316.                   }
  317.               else if (monthOfYear > maxMonth && year == maxYear)
  318.                   {
  319.                   view.updateDate(maxYear, maxMonth, maxDay);
  320.                   String monthstring=getmonthString(maxMonth);
  321.                   Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  322.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  323.                     String day=f.format(date);
  324.                   actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  325.                   }
  326.               else if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
  327.                   {
  328.                   view.updateDate(maxYear, maxMonth, maxDay);
  329.                   String monthstring=getmonthString(maxMonth);
  330.                   Date date= (new GregorianCalendar(maxYear, maxMonth, maxDay)).getTime();
  331.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  332.                     String day=f.format(date);
  333.                   actualtime.setText(day+","+" "+monthstring+" "+maxDay+","+" "+maxYear);
  334.                   }
  335.               else
  336.               {
  337.                view.updateDate(year, monthOfYear, dayOfMonth);
  338.                //Integer.parseInt(tmp[2].trim()), Integer.parseInt(tmp[0].trim())-1,Integer.parseInt(tmp[1].trim())
  339.                String monthstring=getmonthString(monthOfYear);
  340.                Date date= (new GregorianCalendar(year, monthOfYear, dayOfMonth)).getTime();
  341.                   SimpleDateFormat f = new SimpleDateFormat("EEEE");
  342.                     String day=f.format(date);
  343.                   actualtime.setText(day+","+" "+monthstring+" "+dayOfMonth+","+" "+year);
  344.               }
  345.           }});
  346.  
  347.       }
  348.        cancel.setOnClickListener(new OnClickListener() {
  349.  
  350.         public void onClick(View v) {
  351.             // TODO Auto-generated method stub
  352.             dialog.dismiss();
  353.         }
  354.     });
  355.  
  356.        set.setOnClickListener(new OnClickListener() {
  357.  
  358.         public void onClick(View v) {
  359.             // TODO Auto-generated method stub
  360.             int year=BirthDateDP.getYear();
  361.             int monthOfYear=BirthDateDP.getMonth();
  362.             int dayOfMonth=BirthDateDP.getDayOfMonth();
  363.  
  364.             // TODO Auto-generated method stub
  365.  
  366.             if(monthOfYear < 10 && !String.valueOf(monthOfYear).contains("0"))
  367.             {
  368.                 if(dayOfMonth < 10 && !String.valueOf(dayOfMonth).contains("0"))
  369.                 {
  370.                     date_selected = "0"+String.valueOf(monthOfYear+1)+" -0"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
  371.                     //selectedDateForDb = String.valueOf(year)+"-"+"0"+String.valueOf(monthOfYear+1)+"-0"+String.valueOf(dayOfMonth);
  372.                 }
  373.                 else
  374.                 {
  375.                     date_selected = "0"+String.valueOf(monthOfYear+1)+" -"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
  376.                     //selectedDateForDb = String.valueOf(year)+"-0"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(dayOfMonth);
  377.                 }
  378.             }
  379.             else if(dayOfMonth < 10 && !String.valueOf(dayOfMonth).contains("0"))
  380.             {
  381.                 date_selected = String.valueOf(monthOfYear+1)+" -0"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
  382.                 //selectedDateForDb = String.valueOf(year)+"-"+String.valueOf(monthOfYear+1)+"-0"+String.valueOf(dayOfMonth);
  383.             }
  384.             else
  385.             {
  386.                 date_selected = String.valueOf(monthOfYear+1)+" -"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
  387.                 //selectedDateForDb = String.valueOf(year)+"-"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(dayOfMonth);
  388.  
  389.             }
  390.             tv.setText(date_selected);
  391.             dialog.dismiss();
  392.  
  393.         }
  394.     });
  395.     }
  396.     String getmonthString(int month_num)
  397.     {
  398.         String str="";
  399.  
  400.         if(month_num==0)
  401.         {
  402.             str="Jan";  
  403.         }
  404.         else if(month_num==1)
  405.         {
  406.             str="Feb";  
  407.         }
  408.         else if(month_num==2)
  409.         {
  410.             str="Mar";  
  411.         }
  412.         else if(month_num==3)
  413.         {
  414.             str="Apr";  
  415.         }
  416.         else if(month_num==4)
  417.         {
  418.             str="May";  
  419.         }
  420.         else if(month_num==5)
  421.         {
  422.             str="Jun";  
  423.         }
  424.         else if(month_num==6)
  425.         {
  426.             str="Jul";  
  427.         }
  428.         else if(month_num==7)
  429.         {
  430.             str="Aug";  
  431.         }
  432.         else if(month_num==8)
  433.         {
  434.             str="Sep";  
  435.         }
  436.         else if(month_num==9)
  437.         {
  438.             str="Oct";  
  439.         }
  440.         else if(month_num==10)
  441.         {
  442.             str="Nov";  
  443.         }
  444.         else if(month_num==11)
  445.         {
  446.             str="Dec";  
  447.         }
  448.         return str;
  449.  
  450.  
  451.     }
  452.     String getweekString(int dayno)
  453.     {
  454.        String str="";
  455.  
  456.  
  457.           if(dayno==1)
  458.         {
  459.             str="Sunday";  
  460.         }
  461.         else if(dayno==2)
  462.         {
  463.             str="Monday";  
  464.         }
  465.         else if(dayno==3)
  466.         {
  467.             str="Tuesday";  
  468.         }
  469.         else if(dayno==4)
  470.         {
  471.             str="Wednesday";    
  472.         }
  473.         else if(dayno==5)
  474.         {
  475.             str="Thursday";
  476.         }
  477.         else if(dayno==6)
  478.         {
  479.             str="Friday";  
  480.         }
  481.         else if(dayno==7)
  482.         {
  483.             str="Satuarday";    
  484.         }
  485.           return str;
  486.     }
  487.      private void setDisabledTextViews(ViewGroup dp)
  488.         {
  489.             for (int x = 0, n = dp.getChildCount(); x < n; x++)
  490.             {
  491.                 View v = dp.getChildAt(x);
  492.  
  493.                 if (v instanceof TextView)
  494.                 {
  495.                     v.setEnabled(false);
  496.                 }
  497.                 else if (v instanceof ViewGroup)
  498.                 {
  499.                      setDisabledTextViews((ViewGroup)v);
  500.                 }
  501.             }
  502.         }
  503.     }