user5346283
user5346283

Reputation:

How to set a EditText that popup a calender to select date and display in EditText in Tab Activity

How to set a EditText that popup a calender to select date and display in EditText in Tab Activity.

My Tab activity as below. I need the code to post before the return V.

Here the tab activity

package com.artificers.subin.inspection;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Subin on 13-10-2015.
 */
public class Tab3Fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.tab3_view, container, false);

        return V;
    }
}

Thanks

Upvotes: 2

Views: 96

Answers (1)

vguzzi
vguzzi

Reputation: 2428

You can display a DatePicker dialog and onDateSet listener like so:

    private int year;
    private int month;
    private int day;

    public void displayDatePicker() {
        DatePickerDialog.OnDateSetListener pDateSetListener =
                new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        Tab3Fragment.this.year = year;
                        Tab3Fragment.this.month = monthOfYear;
                        Tab3Fragment.this.day = dayOfMonth;
                        updateDisplay();
                    }
                };
    }

    private void updateDisplay() {
        // update your EditText here
    }

Just call displayDatePicker when you would like the user to pick a date and format a date String to go into your EditText in the updateDisplay method. Hope this helps.

EDIT: Implemented with your code as requested

    private int year;
    private int month;
    private int day;

    public class Tab3Fragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View V = inflater.inflate(R.layout.tab3_view, container, false);
            displayDatePicker();
            return V;
        }
    }


    public void displayDatePicker() {
        DatePickerDialog.OnDateSetListener pDateSetListener =
                new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        Tab3Fragment.this.year = year;
                        Tab3Fragment.this.month = monthOfYear;
                        Tab3Fragment.this.day = dayOfMonth;
                        updateDisplay();
                    }
                };
    }

    private void updateDisplay() {
        // update your EditText here
    }

I cannot fill out the updateDisplay method for you as I don't know what your EditText is called or how it is set up in your layout.

Upvotes: 1

Related Questions