Search in sources :

Example 56 with DatePicker

use of android.widget.DatePicker in project collect by opendatakit.

the class FixedDatePickerDialog method fixSpinner.

/**
 * Workaround for this bug: https://code.google.com/p/android/issues/detail?id=222208
 * In Android 7.0 Nougat, spinner mode for the DatePicker in DatePickerDialog is
 * incorrectly displayed as calendar, even when the theme specifies otherwise.
 * <p>
 * Source: https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f
 */
private void fixSpinner(Context context, DatePickerDialog dialog, int year, int month, int dayOfMonth) {
    // for versions < nougat leads to a crash trying to get DatePickerSpinnerDelegate
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
        try {
            // Get the theme's android:datePickerMode
            final int modeSpinner = 2;
            Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");
            Field datePickerStyleableField = styleableClass.getField("DatePicker");
            int[] datePickerStyleable = (int[]) datePickerStyleableField.get(null);
            final TypedArray a = context.obtainStyledAttributes(null, datePickerStyleable, android.R.attr.datePickerStyle, 0);
            Field datePickerModeStyleableField = styleableClass.getField("DatePicker_datePickerMode");
            int datePickerModeStyleable = datePickerModeStyleableField.getInt(null);
            final int mode = a.getInt(datePickerModeStyleable, modeSpinner);
            a.recycle();
            if (mode == modeSpinner) {
                Field datePickerField = findField(DatePickerDialog.class, DatePicker.class, "mDatePicker");
                if (datePickerField == null) {
                    Timber.w("Reflection failed: couldn't find 'mDatePicker' field on DatePickerDialog.");
                    return;
                }
                DatePicker datePicker = (DatePicker) datePickerField.get(dialog);
                Class<?> delegateClass = Class.forName("android.widget.DatePicker$DatePickerDelegate");
                Field delegateField = findField(DatePicker.class, delegateClass, "mDelegate");
                if (delegateField == null) {
                    Timber.w("Reflection failed: couldn't find 'mDelegate' field on DatePickerDialog.");
                    return;
                }
                Object delegate = delegateField.get(datePicker);
                Class<?> spinnerDelegateClass = Class.forName("android.widget.DatePickerSpinnerDelegate");
                // delegate is DatePickerCalendarDelegate
                if (delegate.getClass() != spinnerDelegateClass) {
                    // throw out the DatePickerCalendarDelegate!
                    delegateField.set(datePicker, null);
                    // remove the DatePickerCalendarDelegate views
                    datePicker.removeAllViews();
                    Constructor spinnerDelegateConstructor = spinnerDelegateClass.getDeclaredConstructor(DatePicker.class, Context.class, AttributeSet.class, int.class, int.class);
                    spinnerDelegateConstructor.setAccessible(true);
                    // Instantiate a DatePickerSpinnerDelegate
                    delegate = spinnerDelegateConstructor.newInstance(datePicker, context, null, android.R.attr.datePickerStyle, 0);
                    // set the DatePicker.mDelegate to the spinner delegate
                    delegateField.set(datePicker, delegate);
                    // Set up the DatePicker again, with the DatePickerSpinnerDelegate
                    datePicker.updateDate(year, month, dayOfMonth);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) TypedArray(android.content.res.TypedArray) DatePicker(android.widget.DatePicker)

Example 57 with DatePicker

use of android.widget.DatePicker in project pictureapp by EyeSeeTea.

the class NewReceiptBalanceFragment method initViews.

private void initViews(View view) {
    date = (TextView) view.findViewById(R.id.new_receipt_balance_date);
    rdt = (EditText) view.findViewById(R.id.new_receipt_balance_rdt);
    act6 = (EditText) view.findViewById(R.id.new_receipt_balance_act6);
    act12 = (EditText) view.findViewById(R.id.new_receipt_balance_act12);
    act18 = (EditText) view.findViewById(R.id.new_receipt_balance_act18);
    act24 = (EditText) view.findViewById(R.id.new_receipt_balance_act24);
    pq = (EditText) view.findViewById(R.id.new_receipt_balance_pq);
    cq = (EditText) view.findViewById(R.id.new_receipt_balance_cq);
    putTodayDate();
    view.findViewById(R.id.new_receipt_balance_back).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            backPressed();
        }
    });
    view.findViewById(R.id.new_receipt_balance_submit).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            submitPressed();
        }
    });
    final DatePickerFragment datePickerFragment = new DatePickerFragment();
    datePickerFragment.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            NumberFormat formatter = new DecimalFormat("00");
            date.setText(year + "/" + formatter.format(monthOfYear) + "/" + formatter.format(dayOfMonth));
        }
    });
    date.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            datePickerFragment.show(getFragmentManager(), TAG);
        }
    });
}
Also used : DatePickerFragment(org.eyeseetea.malariacare.views.DatePickerFragment) DatePickerDialog(android.app.DatePickerDialog) DecimalFormat(java.text.DecimalFormat) DatePicker(android.widget.DatePicker) View(android.view.View) TextView(android.widget.TextView) NumberFormat(java.text.NumberFormat)

Example 58 with DatePicker

use of android.widget.DatePicker in project android by testpress.

the class ProfileDetailsActivity method pickDate.

@OnClick(R.id.datepicker)
public void pickDate() {
    if (datePickerDate == null) {
        // if birthdate is null then set current date
        if (profileDetails.getBirthDate() != null) {
            datePickerDate = profileDetails.getBirthDate().split("-");
        } else {
            final Calendar calendar = Calendar.getInstance();
            datePickerDate = new String[] { Integer.toString(calendar.get(Calendar.YEAR)), Integer.toString(calendar.get(Calendar.MONTH) + 1), Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)) };
        }
    }
    DatePickerDialog datePickerDialog = new DatePickerDialog(ProfileDetailsActivity.this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // Display Selected date in textbox
            // add 0 if month r day less than 10 to make it in format 2015-01-01
            String month;
            if ((monthOfYear + 1) < 10) {
                // Month is 0 based so add 1
                month = "0" + (monthOfYear + 1);
            } else {
                month = Integer.toString(monthOfYear + 1);
            }
            String day;
            if ((dayOfMonth) < 10) {
                day = "0" + (dayOfMonth);
            } else {
                day = Integer.toString(dayOfMonth);
            }
            dateOfBirth.setText(year + "-" + month + "-" + day);
            // clear the error message if already exist
            dateOfBirth.setError(null);
            // when user click next time this date will be set in picker
            datePickerDate = new String[] { Integer.toString(year), month, day };
        }
    }, Integer.parseInt(datePickerDate[0]), Integer.parseInt(datePickerDate[1]) - 1, Integer.parseInt(datePickerDate[2]));
    datePickerDialog.show();
}
Also used : DatePickerDialog(android.app.DatePickerDialog) Calendar(java.util.Calendar) DatePicker(android.widget.DatePicker) OnClick(butterknife.OnClick)

Example 59 with DatePicker

use of android.widget.DatePicker in project CalendarApp by savie73.

the class AddTaskActivity method addTask.

public void addTask(View view) {
    // Model of the list data
    ListModel model = ListModel.getInstance();
    // Get the name of the task
    EditText text = (EditText) findViewById(R.id.taskInput);
    String taskName = text.getText().toString();
    // Add task to the list based on whether there is a due date
    Switch s = findViewById(R.id.dueDateSwitch);
    if (s.isChecked()) {
        DatePicker datePicker = findViewById(R.id.datePicker);
        GregorianCalendar dueDate = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        model.addTask(listName, taskName, dueDate);
    } else {
        model.addTask(listName, taskName);
    }
    // Return to the single list activity
    Log.i(TAG, "Creating intent to return to the single list activity");
    Intent intent = new Intent(this, SingleListActivity.class);
    intent.putExtra(Extra.LIST, listName);
    Log.i(TAG, "Intent created with extra message (list name).");
    Log.i(TAG, "Starting single list activity");
    startActivity(intent);
}
Also used : EditText(android.widget.EditText) Switch(android.widget.Switch) GregorianCalendar(java.util.GregorianCalendar) Intent(android.content.Intent) DatePicker(android.widget.DatePicker)

Example 60 with DatePicker

use of android.widget.DatePicker in project NienLuanChuyenNganh-Android by AuNguyenTrungNguyen.

the class AddJobActivity method setTime.

private void setTime(final TextView textView, String text) {
    String title = textView.getText().toString();
    // Lấy ngày hiện tại
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = new Date();
    String now = dateFormat.format(date);
    String[] splitNow = now.split("/");
    int dayDef = Integer.parseInt(splitNow[0]);
    int mouthDef = Integer.parseInt(splitNow[1]) - 1;
    int yearDef = Integer.parseInt(splitNow[2]);
    // Chọn lại ngày
    if (!title.equals(text)) {
        String[] split = title.split("/");
        dayDef = Integer.parseInt(split[0]);
        mouthDef = Integer.parseInt(split[1]) - 1;
        yearDef = Integer.parseInt(split[2]);
    }
    DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            String day, month, year;
            day = (i2 < 10) ? "0" + i2 : "" + i2;
            i1 += 1;
            month = (i1 < 10) ? "0" + i1 : "" + i1;
            year = "" + i;
            textView.setText(day + "/" + month + "/" + year);
        }
    }, yearDef, mouthDef, dayDef);
    dialog.show();
}
Also used : DatePickerDialog(android.app.DatePickerDialog) DatePicker(android.widget.DatePicker) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

DatePicker (android.widget.DatePicker)68 DatePickerDialog (android.app.DatePickerDialog)45 Calendar (java.util.Calendar)42 View (android.view.View)27 TextView (android.widget.TextView)20 DialogInterface (android.content.DialogInterface)13 Intent (android.content.Intent)13 TimePicker (android.widget.TimePicker)11 AlertDialog (android.app.AlertDialog)9 Dialog (android.app.Dialog)9 AdapterView (android.widget.AdapterView)9 Button (android.widget.Button)9 EditText (android.widget.EditText)9 LinearLayout (android.widget.LinearLayout)9 Date (java.util.Date)9 SimpleDateFormat (java.text.SimpleDateFormat)8 Context (android.content.Context)7 TimePickerDialog (android.app.TimePickerDialog)6 ArrayAdapter (android.widget.ArrayAdapter)6 ImageView (android.widget.ImageView)6