Created
March 14, 2014 10:00
-
-
Save ishitcno1/9545088 to your computer and use it in GitHub Desktop.
Two DatePickerDialog in the same Activity of Android. Use inner class for conveniention. Use a flag to detect which EditText should be change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" | |
| android:paddingRight="@dimen/activity_horizontal_margin" | |
| android:paddingTop="@dimen/activity_vertical_margin" | |
| android:paddingBottom="@dimen/activity_vertical_margin" | |
| tools:context="com.edinstudio.app.samples.datepicker.MainActivity"> | |
| <EditText | |
| android:id="@+id/start_date" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:focusable="false" | |
| android:hint="Please pick a date" /> | |
| <EditText | |
| android:id="@+id/end_date" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:layout_below="@id/start_date" | |
| android:focusable="false" | |
| android:hint="Please pick a date" /> | |
| </RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.edinstudio.app.samples.datepicker; | |
| import android.app.DatePickerDialog; | |
| import android.app.Dialog; | |
| import android.os.Bundle; | |
| import android.support.v4.app.DialogFragment; | |
| import android.support.v7.app.ActionBarActivity; | |
| import android.view.View; | |
| import android.widget.DatePicker; | |
| import android.widget.EditText; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| public class MainActivity extends ActionBarActivity implements View.OnClickListener { | |
| private EditText mStartTime; | |
| private EditText mEndTime; | |
| private DatePickerDialogFragment mDatePickerDialogFragment; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| mStartTime = (EditText) findViewById(R.id.start_date); | |
| mEndTime = (EditText) findViewById(R.id.end_date); | |
| mDatePickerDialogFragment = new DatePickerDialogFragment(); | |
| mStartTime.setOnClickListener(this); | |
| mEndTime.setOnClickListener(this); | |
| } | |
| @Override | |
| public void onClick(View v) { | |
| int id = v.getId(); | |
| if (id == R.id.start_date) { | |
| mDatePickerDialogFragment.setFlag(DatePickerDialogFragment.FLAG_START_DATE); | |
| mDatePickerDialogFragment.show(getSupportFragmentManager(), "datePicker"); | |
| } else if (id == R.id.end_date) { | |
| mDatePickerDialogFragment.setFlag(DatePickerDialogFragment.FLAG_END_DATE); | |
| mDatePickerDialogFragment.show(getSupportFragmentManager(), "datePicker"); | |
| } | |
| } | |
| public class DatePickerDialogFragment extends DialogFragment implements | |
| DatePickerDialog.OnDateSetListener { | |
| public static final int FLAG_START_DATE = 0; | |
| public static final int FLAG_END_DATE = 1; | |
| private int flag = 0; | |
| @Override | |
| public Dialog onCreateDialog(Bundle savedInstanceState) { | |
| Calendar calendar = Calendar.getInstance(); | |
| int year = calendar.get(Calendar.YEAR); | |
| int month = calendar.get(Calendar.MONTH); | |
| int day = calendar.get(Calendar.DAY_OF_MONTH); | |
| return new DatePickerDialog(getActivity(), this, year, month, day); | |
| } | |
| public void setFlag(int i) { | |
| flag = i; | |
| } | |
| @Override | |
| public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | |
| Calendar calendar = Calendar.getInstance(); | |
| calendar.set(year, monthOfYear, dayOfMonth); | |
| SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); | |
| if (flag == FLAG_START_DATE) { | |
| mStartTime.setText(format.format(calendar.getTime())); | |
| } else if (flag == FLAG_END_DATE) { | |
| mEndTime.setText(format.format(calendar.getTime())); | |
| } | |
| } | |
| } | |
| } |
mDatePickerDialogFragment.show(getSupportFragmentManager(), "datePicker"); why my app crashes on this line of code i put it in try catch that stopped crash but it never worked
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i