fragment_custom_time_picker_dialog.xml
<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" tools:context="iterator.ir.a017.CustomTimePickerDialogFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TimePicker android:id="@+id/timePickerMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:timePickerMode="spinner"> </TimePicker> <Button android:id="@+id/buttonDone" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/done"/> </LinearLayout> </FrameLayout>
CustomTimePickerDialogFragment.java
public class CustomTimePickerDialogFragment extends DialogFragment { private OnCustomTimePickerClickListener mListener; Button buttonDone; TimePicker timePickerMain; public CustomTimePickerDialogFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_custom_time_picker_dialog, null); buttonDone= (Button) view.findViewById(R.id.buttonDone); timePickerMain= (TimePicker) view.findViewById(R.id.timePickerMain); buttonDone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.onCustomTimePickerClick(timePickerMain); } dismiss(); } }); return view; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnCustomTimePickerClickListener) { mListener = (OnCustomTimePickerClickListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnCustomTimePickerClickListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnCustomTimePickerClickListener { void onCustomTimePickerClick(View view); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="iterator.ir.a017.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="25dp" android:id="@+id/buttonShow" android:text="@string/show" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements CustomTimePickerDialogFragment.OnCustomTimePickerClickListener { Button buttonShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonShow= (Button) findViewById(R.id.buttonShow); buttonShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomTimePickerDialogFragment customTimePickerDialogFragment=new CustomTimePickerDialogFragment(); customTimePickerDialogFragment.show(getSupportFragmentManager(),"customDialog1"); } }); } @Override public void onCustomTimePickerClick(View view) { TimePicker timePickerMain= (TimePicker) view; String time=String.format("%s:%s",timePickerMain.getCurrentHour(),timePickerMain.getCurrentMinute()); Toast.makeText(getBaseContext(),time,Toast.LENGTH_LONG).show(); } }
References