N
N
newdancer2015-11-16 17:03:31
Java
newdancer, 2015-11-16 17:03:31

How to implement DatePickerDialog in Dialog Fragment?

How to implement DatePickerDialog in Dialog Fragment?
Dialog class code:

public class MyDialog extends DialogFragment implements View.OnClickListener
{
    int idDialog;
    final String LOG = "myLogs";
    View rootView;
    String srezults="", str="";
    TextView incTextDate;
    EditText incEditMoney, incEditComent;
    Button incBtnAdd;
    String datestr="";
    Context context;
    private static final String TAG = "myLogs";
    int w, h; // ширина и высота экрана

    int myYear = 2015;
    int myMonth = 11;
    int myDay = 14;

    static MyDialog newInstance(int num, String srezults)
    {
        MyDialog f = new MyDialog();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("id", num);

        args.putString("srezults", srezults);
        f.setArguments(args);
        return f;
    }

    public interface onMyDialigEventListener {
        public void someEvent(String s);
    }

    onMyDialigEventListener categoryEventListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //this.activity = activity;
        context=activity;
        setRetainInstance(true);

        try {
            categoryEventListener = (onMyDialigEventListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement onSomeEventListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        // ставим прозрачный цвет
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        // убираем title из окна
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //getDialog().setTitle("Simple Dialog");

        idDialog = getArguments().getInt("id");
        srezults = getArguments().getString("srezults");
        if(idDialog==1)
        {
            rootView = inflater.inflate(R.layout.dialog_income, container, false);
            incEditMoney = (EditText)rootView.findViewById(R.id.incEditMoney);
            incEditComent = (EditText)rootView.findViewById(R.id.incEditComent);
            incTextDate = (TextView)rootView.findViewById(R.id.incTextDate);
            incBtnAdd = (Button)rootView.findViewById(R.id.incBtnAdd);
            incBtnAdd.setOnClickListener(this);

            incTextDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    DatePickerDialog.OnDateSetListener myCallBack = new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                              int dayOfMonth) {
                            myYear = year;
                            myMonth = monthOfYear;
                            myDay = dayOfMonth;
                            incTextDate.setText(myDay + "/" + myMonth + "/" + myYear);
                        }
                    };
                    DatePickerDialog tpd = new DatePickerDialog(context, myCallBack, myYear, myMonth, myDay);
                }
            });
        }
        return rootView;
    }
}

xml file of dialog_income template:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout1"
    android:background="#80000000"
    android:orientation="vertical"
    android:gravity="center_vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialogokno"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"
            android:layout_margin="10dp"
            android:text="@string/alert_income"
            android:textAppearance="?android:attr/textAppearanceLarge">
        </TextView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/orange">
        </TextView>
        <LinearLayout
            android:orientation="vertical"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:padding="4dp">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:padding="2dp">
                <TextView
                    android:id="@+id/incTextDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:singleLine="true"
                    android:layout_marginRight="2dp"
                    android:clickable="true"
                    android:onClick="onclick"
                    android:focusable="false"
                    android:text="@string/alert_edit_date"
                    android:focusableInTouchMode="false"
                    style="@style/LogInEditTextStyle"/>

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:layout_weight="1"
                    android:layout_marginLeft="2dp"
                    android:singleLine="true"
                    android:imeOptions="actionNext"
                    style="@style/LogInEditTextStyle"
                    android:id="@+id/incEditMoney" />

            </LinearLayout>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@color/Gray"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:padding="2dp">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:layout_weight="1"
                    android:layout_marginLeft="2dp"
                    android:singleLine="true"
                    android:text="@string/alert_edit_coment"
                    android:imeOptions="actionDone"
                    style="@style/LogInEditTextStyle"
                    android:id="@+id/incEditComent" />
            </LinearLayout>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="@string/alert_add"
                android:id="@+id/incBtnAdd" />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yurii Nekrasov, 2015-11-26
@driversti

Look here: DatePickerDialog. Date picker widget

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question