Skip to content

Instantly share code, notes, and snippets.

@CiTuX
Created December 11, 2014 14:24
Show Gist options
  • Save CiTuX/545f20baf0ea0a71ae29 to your computer and use it in GitHub Desktop.
Save CiTuX/545f20baf0ea0a71ae29 to your computer and use it in GitHub Desktop.
Dark Time Picker
package ch.citux.datetimepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.android.datetimepicker.date.DatePickerDialog;
import com.android.datetimepicker.time.RadialPickerLayout;
import com.android.datetimepicker.time.TimePickerDialog;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends Activity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
private static final String TIME_PATTERN = "HH:mm";
private TextView lblDate;
private TextView lblTime;
private Calendar calendar;
private DateFormat dateFormat;
private SimpleDateFormat timeFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
calendar = Calendar.getInstance();
dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
timeFormat = new SimpleDateFormat(TIME_PATTERN, Locale.getDefault());
lblDate = (TextView) findViewById(R.id.lblDate);
lblTime = (TextView) findViewById(R.id.lblTime);
update();
}
private void update() {
lblDate.setText(dateFormat.format(calendar.getTime()));
lblTime.setText(timeFormat.format(calendar.getTime()));
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDatePicker:
DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show(getFragmentManager(), "datePicker");
break;
case R.id.btnTimePicker:
TimePickerDialog dialog = TimePickerDialog.newInstance(this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
dialog.setThemeDark(true);
dialog.show(getFragmentManager(), "timePicker");
break;
}
}
@Override
public void onDateSet(DatePickerDialog dialog, int year, int monthOfYear, int dayOfMonth) {
calendar.set(year, monthOfYear, dayOfMonth);
update();
}
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment