Mikky
Mikky

Reputation: 345

how to make simple datepicker dialog with buttons + and -?

I tried to make a DatePicker dialog based on some examples I read. I didn't achieve the desired result. I would appreciate any help and tutorials on this subjects for beginners.

Thanks in advance!

Any tutorial with example, please.

Upvotes: 1

Views: 4814

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You can use the below for reference

pickerdate class

  public class pickerdate extends Activity {
    static TextView mDateDisplay;
    private Button mPickDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mDateDisplay = (TextView) findViewById(R.id.textView1);
        mPickDate = (Button) findViewById(R.id.button1);
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });
    }   
    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;

    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);
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

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

        mDateDisplay .setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
    }
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="94dp"
    android:text="TextView" />

</RelativeLayout>

Upvotes: 3

Related Questions