Ad

DatePickerDialog Opens Two Times

I want to show my date in the Edit-Text with this format: YYYY-MM-DD.
First, when I touch the Edit-Text of the date then the keyboard is still open then Date-Picker-Dialog appears two times. If I set this one time, then again the dialog picker appears. When I set the date the second time, then the date is set in the Edit-Text.
The first Fragment is attached to the first Activity and the second one is attached to the second Activity.

Second Fragment code:

package com.example.week9_fragment_task;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Calendar;


public class SecondFragment extends Fragment
{
    private Button btnadd,btncancel;
    private Spinner spinner;
    private EditText date;
    Calendar calendar=Calendar.getInstance();
    DBTools dbTools;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnadd=(Button)getActivity().findViewById(R.id.btn_add_fragment2);
        btncancel=(Button)getActivity().findViewById(R.id.btn_cancel_fragment2);
        spinner=(Spinner)getActivity().findViewById(R.id.spinner);
        date=(EditText)getActivity().findViewById(R.id.datepicker);

        ArrayAdapter adapter=ArrayAdapter.createFromResource(getActivity(),R.array.health_type,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        date.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Date_Alert(v);
                return false;
            }

            private void Date_Alert(View v)
            {

                DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
                    {
                        date.setText(year+"-"+month+"-"+dayOfMonth);
                    }

                    },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });

Second Fragment XML Code

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    tools:context=".SecondFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <EditText
            android:id="@+id/txtname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:hint="@string/Hint_Name"
            android:inputType=""
            android:importantForAutofill="no"
            android:autofillHints="" tools:targetApi="o" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="150dp"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/datepicker"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="date"
            android:autofillHints=""
            android:hint="@string/datepicker"
            android:importantForAutofill="no" tools:targetApi="o" />

        <EditText
            android:id="@+id/timepicker"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="time"
            android:hint="@string/timepicker"
            android:autofillHints="" tools:targetApi="o" />
</LinearLayout>

</FrameLayout>
Ad

Answer

    date.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Date_Alert(v);
                    return true;
                }

                private void Date_Alert(View v)
                {

    getActivity().getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    );                
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        //date_str = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        mMonthName = MONTHS[monthOfYear];
                        Log.d("before", "onDateSet: " + mYear + "" + mMonthName + "" + mDay);
                        start_date_str = mDay + " " + mMonthName + " " + mYear;

                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.show();
}
Ad
source: stackoverflow.com
Ad