Android: TimePicker setIs24HourView not working - android

I am trying to use the TimePicker in the 24 hour format and I am using setIs24HourView= true, but I am still not getting 24 hour format on the TimePicker. Here is my code in the onCreate of the Activity.
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
I even tried timePicker.setCurrentHour(cal.HOUR_OF_DAY) after setIs24HourView, but I am not able to see the 24 hour format on the TimePicker.
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110dp" />
Am I missing anything here?

ok, I see what the problem is now.
It's not correctly setting the currentHour to the new 24 hour format, which is why you got 9:05 instead of 21:05. I'm guessing it isn't 9am where you are!
It is a bug, as mentioned in this question
seems the only work around, for now, is to do something like:
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY));
I see you tried cal.HOUR_OF_DAY, but didn't provide the code you actually used, but try this and see if it helps.

I notice this problem in jelly bean You can set the time to show in 24 hour view
TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
picker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
int h = calendar.get(Calendar.HOUR_OF_DAY);
int m = calendar.get(Calendar.MINUTE);
picker.setCurrentHour(h);
picker.setCurrentMinute(m);

You can just use the device settings and thus let the user make this decision;
String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);

setting timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY)) solved the problem

timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
We need to add this:
Calendar calendar = Calendar.getInstance();
timePicker.setCurrentHour(calendar.get(calendar.HOUR_OF_DAY));

Related

Change UI of Date Picker for Android TV app

I'm writing an android tv app with lean-back and I want to use android standard date picker and I want to make this like this pic
(eg: Made year part bigger when the user is selecting year and so on)
How can I achieve this?
You cannot use the standard date picker in Android TV apps because it was not designed for navigation using D-Pad. However, you can create your own widgets for day, month and year selection. You can simply use VerticalGridView to create them. For instance, you can do something similar to this for days:
<androidx.leanback.widget.VerticalGridView
android:id="#+id/vgv_days"
android:background="#color/transparent"
android:layout_marginEnd="10dp"
android:layout_width="40dp"
android:layout_height="35dp" />
customArrayObjectAdapterDays = new CustomArrayObjectAdapter(new DatePartPresenter(getContext()));
for (int i = 1; i < 31; i++) {
customArrayObjectAdapterDays.add(i);
}
vgvDays = (VerticalGridView) view.findViewById(R.id.vgv_days);
vgvDays.setNumColumns(1);
vgvDays.setAdapter(new ItemBridgeAdapter(customArrayObjectAdapterDays));
You also have to set the number of days (28, 29, 30, 31) depending on the year and selected month.

datepicker dialog can't swich to spinner on Android 7.0 device

Before Android N, I can use the code below to instance a spnnier mode datepicker dialog:
new DatePickerDialog(getContext(), AlertDialog.THEME_HOLO_LIGHT, null, 2016, 9, 18);
but the above code is not work on Android N device, it always show the calander mode, is there something different in Android N? How can I instance a spinner mode datepicker dialog?
Have you tried something like : yourDatePickerDialog.getDatePicker().setCalendarViewShown(false);

Removing AM/PM in Android Time picker Widget

Is there any way to remove the AM/PM in a Time Picker Widget?
I have this function in my application but its purpose is to select only Hour and Minutes not including AM/PM, I tried to setIs24HourView(true) but it makes the time 24hours, I only want 12 hours.
It seems there is no public method in TimePicker to directly hide or show the AM/PM chooser. However, a look at the source code will give us the name of the resource ID for that View, which we can get with the system Resources. Then it's simply a matter of finding the View, and setting its visibility to GONE.
private void hideAmPmLayout(TimePicker picker) {
final int id = Resources.getSystem().getIdentifier("ampm_layout", "id", "android");
final View amPmLayout = picker.findViewById(id);
if(amPmLayout != null) {
amPmLayout.setVisibility(View.GONE);
}
}

Able to choose future date when setting max date in datepicker

I'm targeting for and building with API level 21, using AppCompat v21.
It gives me a nicely looking new date picker, which has the unexpected property of allowing me to choose a future date when max date has been set using
datePicker.setMaxDate(Calendar.getInstance().getTimeInMillis())
The future dates are greyed out, but I can still choose any one of them. Is that a bug? Am I doing it wrong? Is there a way to prevent the user from being able to pick a future date in the date picker?
The old Holo date picker did not allow picking a future date when setting a maximum date.
UPDATE:
While it is not working properly on my Nexus 4 running stock 5.0, it is working properly on my Nexus 6 running stock Android 5.1.1. Perhaps it was a bug in Android 5.0 and it was fixed in 5.1? Can anyone confirm?
So to answer my own question:
I've looked at the Android source of DatePickerCalendarDelegate.java at grepcode, specifically at public void setMaxDate(long maxDate) for both Android versions 5.0 and 5.1.
What's new in 5.1 in setMaxDate() is that
mDayPickerView.goTo(getSelectedDay(), false, true, true);
has been changed to:
mDayPickerView.setMaxDate(maxDate);
It seems they fixed it there, which corresponds with my observation that it works as expected in 5.1 but not in 5.0.
And so it seems we're stuck with dealing with the fact that it doesn't work properly in Android 5.0 (days after max date are greyed out but can still be chosen).
I am using this and its working correctly
Call this function to open date picker
public void openDatePicker() {
DatePickerDialog dpd = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
dpd.show();
}
Here is the mDateSetListener
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
date.setText(dayOfMonth + " / " + (monthOfYear + 1) + " / " + year); // Here date is a TextView to display date
date.setVisibility(View.VISIBLE);
}
};
You can use a method in which u can pass the value of the date entered by the user and compare it for example to the current date , if it's a future date u can reset the value of date used to a specific one (for example the current date) and show a toast to inform him.
you can work around it on your CSS by finding the right element (with a "disabled" class) and setting a pointer events to none.`
.bs-datepicker-body table td.disabled {
pointer-events: none;
}

How To Show The Current Time in TextView in Widget

I want to show the current time in updatable textView
I can't find the true way to make it
I want it to be updated every minute
Please anyone can help me ?
Thanks
Calendar cal = Calendar.getInstance();
SimpleDateFormat oSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");
String MyString= oSimpleDateFormat.format(cal.getTime());
EditText oEditText =(EditText) findViewById(R.id.MyEdit);
oEditText.setText(MyString);
try this
You should use ScheduledThreadPoolExecutor.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
use the digital clock tag Digital Clock built in android studio 2.1.
Here it is built in tag to show time
< Digital Clock > don't use space b/w digital and clock, it was having problem while posting the answer.

Categories

Resources