I am wondering if I can customize the DatePicker and TimePicker which are available by default in Android. I mainly use spinner mode, but I want to be able to customize various fonts such as font, size, and spin, not just changing the background color (it was changed by entering the drawble created in the android:background property). I wonder if it is possible to change it to something like the TimePicker used by ios by default.
Asked
Active
Viewed 54 times
1 Answers
2
To show TimePicker using TimePickerDialog, you need to create a fragment by extending DialogFragment class and implementing onCreateDialog method. In onCreateDialog method, you need to instantiate TimePickerDialog setting current time and return it.
In the activity where you need to show TimePicker, you need to instantiated the Fragment and show it in response to user event such as button click.
import java.util.Calendar;
public class MyTimePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), timeSetListener, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
private TimePickerDialog.OnTimeSetListener timeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(getActivity(), "selected time is "
+ view.getHour() +
" / " + view.getMinute()
, Toast.LENGTH_SHORT).show();
}
};
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new MyTimePickerFragment();
newFragment.show(getSupportFragmentManager(), "time picker");
}
}

Karthickyuvan
- 428
- 2
- 16