Wednesday 17 August 2016

Datepicker


Creating a DatePickerDialog is just like creating a TimePickerDialog. The only difference is the dialog you create for the fragment.
http://i.stack.imgur.com/gsPyb.png

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/LinearLayout1"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:background="#FF550D"
   
>
    <
DatePicker
       
android:id="@+id/datepicker1"
       
android:layout_width="match_parent"
        
android:layout_height="match_parent"></DatePicker>

</
LinearLayout>

XML Attribute and related method
1.     android:calendarTextColor      
The text color list of the calendar.
                               android:calendarTextColor="#ffffff"

2.     android:calendarViewShown    Whether the calendar view is shown.
It take boolean value. If false then calender of datepicker not showing otherwise if true then visible. By default it is true.
android:calendarViewShown="false"
 
Programmatically
        datePicker.setCalendarViewShown(false);
 

3.     android:datePickerMode         
Defines the look of the widget. It show datepicker as a calender or display datepicker as a spinner.
Two value of  datePickerMode is "spinner / calender".
         android:datePickerMode="spinner"

4.     android:firstDayOfWeek           
The first day of week according to Calendar.
It value between 1 to 7.
         android:firstDayOfWeek="2"


5.     android:headerBackground      The background for the selected date header.
It's value should be color.
               android:headerBackground="#F94C7F"

6.     android:headerDayOfMonthTextAppearance    
The text appearance for the day of month

7.     android:maxDate         
The maximal date shown by this calendar view in mm/dd/yyyy format.
         android:maxDate="25/06/2016"


8.     android:minDate         
9.     The minimal date shown by this calendar view in mm/dd/yyyy format.
android:maxDate="25/06/2016"

10.  android:spinnersShown          
Whether the spinners are shown.
It take boolean value. If false then spinner of datepicker not showing otherwise if true then visible. By default it is true.

               android:spinnersShown="false"



Programmatically
        datePicker.setSpinnersShown(false);
 

Method:

1. GetDayOfMonth
int mDate=datePicker.getDayOfMonth();
 
2. GetFisrtDayOfWeek
 
int mDate=datePicker.getFirstDayOfWeek();
 
3. GetMonth
int mDate=datePicker.getMonth()+1;
 
4. GetYear

int mDate=datePicker.getYear();



activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#FF550D"

    >

    <TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Time Show"

        android:textSize="30dp"

        android:background="#ffffff"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Pick Time"

        android:onClick="goDayofmonth" />



    <DatePicker

        android:id="@+id/datepicker1"

        android:layout_width="match_parent"

        android:layout_height="fill_parent"

        ></DatePicker>

</LinearLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {

    TextView textView1;

    DatePicker datePicker;

    Calendar c;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textView1=(TextView)findViewById(R.id.textView1);



        datePicker= (DatePicker)findViewById(R.id.datepicker1);



        }

    public  void  goDayofmonth(View view)

    {

        int mDate=datePicker.getYear();



        /*

        * You can use below code instead of  int mDate=datePicker.getDayOfMonth()to get different value

        * int mDate=datePicker.getDayOfMonth();

        * int mDate=datePicker.getFirstDayOfWeek();

        * */

        String str=String.valueOf(mDate);

        textView1.setText(str);

    }



}


DatePicker with Dialog
activity_main.java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#FF550D"

    >

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="pick_date"

        android:onClick="showDatePickerDialog" />

</LinearLayout>

MainActivity.java
package sichlearning.example.com.testone;





import android.app.DatePickerDialog;

import android.app.Dialog;

import android.app.DialogFragment;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.DatePicker;

import android.widget.TextView;



import java.util.Calendar;



public class MainActivity extends AppCompatActivity {

    TextView textView1;

    DatePicker datePicker;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        }

    public void showDatePickerDialog(View v) {

        DialogFragment newFragment = new DatePickerFragment();

        newFragment.show(getFragmentManager(), "datePicker");

    }



    public static class DatePickerFragment extends DialogFragment

            implements DatePickerDialog.OnDateSetListener {



        @Override

        public Dialog onCreateDialog(Bundle savedInstanceState) {

            // Use the current date as the default date in the picker

            final Calendar c = Calendar.getInstance();

            int year = c.get(Calendar.YEAR);

            int month = c.get(Calendar.MONTH);

            int day = c.get(Calendar.DAY_OF_MONTH);



            // Create a new instance of DatePickerDialog and return it

            return new DatePickerDialog(getActivity(), this, year, month, day);

        }



        public void onDateSet(DatePicker view, int year, int month, int day) {

            // Do something with the date chosen by the user

        }

    }



}




















































22

No comments:

Post a Comment