I have got a TimePickerDialog working to set time which is set to a TextView in order to display it. Now, I need help to set that TimePicker (inside the TimePickerDialog) minutues interval to 15 minutes. I have seen there is a post with 15 minutes interval issue related to TimePicker, but I don't know how to apply it to the TimePickerDialog because I don't know how to use the TimePicker that it is created inside the TimePickerDialog. I am new to Android and completely lost in this matter. Thanks in advance.
Using a combination of this from #Rizwan and this other thread, I came up with a combined solution that allows arbitrary minute increments in a TimePickerDialog. The main issue is that most of the functionality is hidden by the android TimePickerDialog and TimePicker classes and it doesn't appear to allow
Extend TimePickerDialog to allow us easier access
Use reflection to reach inside the display and access the required bits (see below)
rewire the minute 'NumberPicker' to display our values
rewire the TimePicker to receive and return values form the NumberPicker honoring our custom increment.
block onStop() so that it doesn't reset the value on close.
Warning
The main issue with reaching inside the UI is that elements are referenced by ids which are likely to change, and even the name of the id is not guaranteed to be the same forever. Having said that, this is working, stable solution and likely to work for the foreseeable future. In my opinion the empty catch block should warn that the UI has changed and should fall back to the default (increment 1 minute) behaviour.
Solution
private class DurationTimePickDialog extends TimePickerDialog
{
final OnTimeSetListener mCallback;
TimePicker mTimePicker;
final int increment;
public DurationTimePickDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, int increment)
{
super(context, callBack, hourOfDay, minute/increment, is24HourView);
this.mCallback = callBack;
this.increment = increment;
}
#Override
public void onClick(DialogInterface dialog, int which) {
if (mCallback != null && mTimePicker!=null) {
mTimePicker.clearFocus();
mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute()*increment);
}
}
#Override
protected void onStop()
{
// override and do nothing
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
Class<?> rClass = Class.forName("com.android.internal.R$id");
Field timePicker = rClass.getField("timePicker");
this.mTimePicker = (TimePicker)findViewById(timePicker.getInt(null));
Field m = rClass.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker)mTimePicker.findViewById(m.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60/increment)-1);
List<String> displayedValues = new ArrayList<String>();
for(int i=0;i<60;i+=increment)
{
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
constructor accepts the increment value and retains some other references. Note that this omits error checking and we'd prefer 60%increment==0
onCreate uses the name of the UI fields and reflection to discover the current location. Again this omits error checking and should be 'fail-safe' ie revert to default behaviour if something goes wrong.
onClick overridden to return the correct minute value to the callback listener
onStop overridden to prevent the (incorrect) index value being returned a second time, when the dialog closes. Go on, try it yourself.
Most of this comes from digging into the TimePickerDialog source.
UPDATE FOR ANDROID 11+
The reflection used in the example above was marked as greylist-max-q, meaning it would no longer be possible after Android 10 (API 29 - Q). In the short term (until August 2021) it is possible to drop the target version back to API 29 while still building against Android 11 (API 30 - R) but after this date the Play Store will no longer accept these builds and will require API 30 as the build target. This eventual failure was anticipated and noted in the warning above.
well thats fine if you used time-picker instead of time-picker-dialog. But there is a solution for this actually.. here is what I used to meet the same requirement.. I used CustomTimePickerDialog. Every thing will be same, only TimePickerDialog will change to CustomTimePickerDialog in the code.
CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(myActivity.this, timeSetListener,
Calendar.getInstance().get(Calendar.HOUR),
CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance().get(Calendar.MINUTE) + CustomTimePickerDialog.TIME_PICKER_INTERVAL),
false
);
timePickerDialog.setTitle("2. Select Time");
timePickerDialog.show();
Here is my CustomTimePickerDialog class... Just use this class in your project and change TimePickerDialog to CustomTimePickerDialog..
public class CustomTimePickerDialog extends TimePickerDialog{
public static final int TIME_PICKER_INTERVAL=10;
private boolean mIgnoreEvent=false;
public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute,
boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
}
/*
* (non-Javadoc)
* #see android.app.TimePickerDialog#onTimeChanged(android.widget.TimePicker, int, int)
* Implements Time Change Interval
*/
#Override
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
super.onTimeChanged(timePicker, hourOfDay, minute);
this.setTitle("2. Select Time");
if (!mIgnoreEvent){
minute = getRoundedMinute(minute);
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;
}
}
public static int getRoundedMinute(int minute){
if(minute % TIME_PICKER_INTERVAL != 0){
int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL);
minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0);
if (minute == 60) minute=0;
}
return minute;
}
}
After using this CustomTimePickerDialog class, All you will need to do is use CustomTimePickerDialog instead of TimePickerDialog in your code to access/override default functions of TimePickerDialog class. In the simple way, I mean your timeSetListener will be as following after this...
private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}// using CustomTimePickerDialog
You can use a regular AlertDialog and use setView to include a custom TimePicker view.
/**
* Set TimePicker interval by adding a custom minutes list
* TIME_PICKER_INTERVAL = Enter your Minutes;
* #param timePicker
*/
private void setTimePickerInterval(TimePicker timePicker) {
try {
int TIME_PICKER_INTERVAL = 10;
NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
"minute", "id", "android"));
minutePicker.setMinValue(0);
minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e);
}
}
Related
It seems TimePicker is made of three inner NumberPickers. Is it possible to access them? it's needed to access and modify each of them.
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
TimePicker dialog = new TimePicker(getActivity(), this, 22, 30,
DateFormat.is24HourFormat(getActivity()));
// Create a new instance of TimePickerDialog and return it
return dialog;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
In dialog, I've created a TimePicker to be shown, but I have to modify TimePicker minutes column(spinner) and add 15min intervals. I want to know if there is any way to access those NumberPickers alone.
UPDATE
i posted answer below, however it was one of the hardest modification in android built-in components for me, i finally found the answer and shared it here but idk why it got down-vote?
finally i found a solution. i'm getting minute column here and setting a 15 minute interval there.
changeTimepicker(R.id.from_time_picker, 15);
method implementation:
try {
Class<?> rClass = Class.forName("com.android.internal.R$id");
// Field timePicker = rClass.getField(name);
TimePicker mTimePicker = (TimePicker) findViewById(id);
Field m = rClass.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker) mTimePicker.findViewById(m.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60 / increment) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += increment) {
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
New to JAVA and Android coding and trying my first practical project.
I don't understand how to make processing wait until timekeeperdialog returns a value.
In my Main Activity I have created getters and setters to variables (first time doing this btw):
private int pickhour;
private int pickminute;
public MainActivity(){
pickhour = 0;
pickminute = 0;
}
public void setpickhour(int pickhour) {
this.pickhour = pickhour;
}
public int getpickhour(){
return this.pickhour;
}
public void setpickminute(int pickminute) {
this.pickminute = pickminute;
}
public int getPickminute(int pickminute) {
return this.pickminute;
}
I call the dialog box with this, and then expecting processing to pause until the TimePicker returns a value, I have a Toast to show results. The Toast fires as soon as the Timepicker appears.
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(),"TimePicker");
Toast.makeText(getApplicationContext(), "Time Picker" + String.valueOf(pickhour) + ":" + String.valueOf(pickminute), Toast.LENGTH_LONG).show();
And my fragment looks like this :
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
private MainActivity ma = new MainActivity();
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current time as the default values for the time picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Create and return a new instance of TimePickerDialog
return new TimePickerDialog(getActivity(),this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
//onTimeSet() callback method
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
//Do something with the user chosen time
//Get reference of host activity (XML Layout File) TextView widget
ma.setpickhour(hourOfDay);
ma.setpickminute(minute);
}
}
I tried looping through a boolean set by the ontimeset method to force waiting on a return value, but my application just hung.
What fundamental concept am I missing? I'm on vacation, so spent a day & a half trying to figure this out.
You can try this -
public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener{
private MainActivity ma = new MainActivity();
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current time as the default values for the time picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Create and return a new instance of TimePickerDialog
return new TimePickerDialog(getActivity(),this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
//onTimeSet() callback method
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
//Do something with the user chosen time
//Get reference of host activity (XML Layout File) TextView widget
ma.setpickhour(hourOfDay);
ma.setpickminute(minute);
Toast.makeText(getApplicationContext(), "Time Picker" + String.valueOf(pickhour) + ":" + String.valueOf(pickminute), Toast.LENGTH_LONG).show();
}
}
And if you are only using fragment for timePicker there is no need of the fragment.
In this case you can try this in MainActivity -
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
setpickhour(hourOfDay);
setpickminute(minute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Time Picker");
mTimePicker.show();
Does your code work? Because you cannot pass "this" as second argument in TimePickerDialog constructor.
I have 2 buttons in my app
starttime and endtime
Now I want the device to turn in silent mode during the start and end time duration set by the user.....how can I do this?
My code for taking input for starttime and endtime
mainactivity.java code
package com.example.h.manualsilent;
public class MainActivity extends AppCompatActivity {
TimePickerDialog tpd;
SimpleDateFormat simpleDateFormat;
String time;
Button sttime;
Button entime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sttime=(Button)findViewById(R.id.startbtn);
entime=(Button)findViewById(R.id.endbtn);
}
public void starttime(View view){
Calendar cal=Calendar.getInstance();
simpleDateFormat=new SimpleDateFormat("hh:mm a");
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
//int inst=cal.get(Calendar.AM_PM);
tpd=new TimePickerDialog(MainActivity.this, new
TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Time time = new Time(hourOfDay, minute,0);
//little h uses 12 hour format and big H uses 24 hour format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
//format takes in a Date, and Time is a sublcass of Date
String s = simpleDateFormat.format(time);
sttime.setText(s);
}
},hour,minute,false);
tpd.show();
}
public void endtime(View view){
Calendar cal=Calendar.getInstance();
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
//int inst=cal.get(Calendar.AM_PM);
tpd=new TimePickerDialog(MainActivity.this, new
TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Time time = new Time(hourOfDay, minute,0);
//little h uses 12 hour format and big H uses 24 hour format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
//format takes in a Date, and Time is a sublcass of Date
String s = simpleDateFormat.format(time);
entime.setText(s);
}
},hour,minute,false);
tpd.show();
}
}
You can use the AudioManager class.
In this class you're looking for setRingerMode() function.
AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
The values you can pass into the function are:
The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or
RINGER_MODE_VIBRATE.
You have to add this into the manifest file:
android.permission.MODIFY_AUDIO_SETTINGS
I saw this here - https://stackoverflow.com/a/3738768/8214839
You can use AudioManager for changing from genral mode to silent mode.
Code:
AudioManager audioManager;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// changing to silent mode
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
You have to give the required permission also in manifest file.
android.permission.MODIFY_AUDIO_SETTINGS
You can use the above code to set the phone to silent based on the condition that you need.
You can also change back to VIBRATE or GENERAL using AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_NORMAL respectively.
I'm looking for some solution which will be faster than mine. I get a time difference between current time and my saved, next step is to put the time in TextView. This solution works but it makes my app very slowly. Does anyone know faster solution?
Thread counter = new Thread(){
public void run()
{
long saved
long difference;
while(true)
{
saved = Long.parseLong(getString(c, "0", "saved"));// sharedPreferences
difference = System.currentTimeMillis() - saved;
seconds = (int) (difference / 1000) % 60;
difference-=seconds;
minutes= (int)((difference/ 60000 ) % 60);
difference=minutes;
hours = (int) ((difference/ 3600000) % 24);
days = (int) ((difference/86400000) % 30);
difference-=days;
months= (int) ((difference/2592000)/1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
myTextView5.setText(Integer.toString(seconds));
myTextView4.setText(Integer.toString(minutes));
myTextView3.setText(Integer.toString(hours));
myTextView2.setText(Integer.toString(days));
myTextView1.setText(Integer.toString(months));
}
});
Your precision is 1s - so you can Thread.sleep (1000) in the while without missing a beat.
I would recommend the use of JodaTime library.
DateTime saved = //getSaved;
DateTime diff = DateTime.now().minus(saved);
//update TextView
txtView.setText(diff.getSecondsOfMinute().toString());
According to my understanding you are using runOnUiThread that created temporary shortlife objects that is the main reason your code is running slow because in each iteration it does it created a new instance of Runnable class and then updates your UI.
Read this topic to get a good understanding of Communication with Ui Thread
http://developer.android.com/training/multiple-threads/communicate-ui.html
You can use Handlers to overcome this issue all you need to do is pass message to handler and it will do the job for you.
This is how you do it.
First of all create a custom handler class
public class CustomMessageHandler extends Handler {
private IUpdateAudioProgressListView iUpdateRef;
public void setOnProgressUpdatedListner(IUpdateAudioProgressListView ref) {
this.iUpdateRef = ref;
}
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
YOUR HANDLING CODE HERE ......................
}
}
then in order to send message to UI thread from a custom thread you need to do following things
initialize a class level variable in your thread class like
public class Test {
CustomMessageHandler handler;
public Test() {
handler = new CustomMessageHandler();
Thread counter = new Thread() {
public void run() {
long saved
long difference;
while (true) {
saved = Long.parseLong(getString(c, "0", "saved"));// sharedPreferences
difference = System.currentTimeMillis() - saved;
seconds = (int) (difference / 1000) % 60;
difference -= seconds;
minutes = (int) ((difference / 60000) % 60);
difference = minutes;
hours = (int) ((difference / 3600000) % 24);
days = (int) ((difference / 86400000) % 30);
difference -= days;
months = (int) ((difference / 2592000) / 1000);
Message msg = handler.obtainMessage();
msg.obj = // send all your variables via a class or whatever logic you use
handler.sendMessage(msg);
}
}
};
}
}
If you don't want to use an external library, I would suggest the use of java.util.Calendar (Don't forget to import that!). For your solution, every time your value in SharedPreferences is changed, just run the code below.
Code to calc diff
Calendar c = Calendar.getInstance();
c.setTimeInMillis(difference);
int months = c.get(Calendar.MONTH);
int days = c.get(Calendar.DAY_OF_MONTH);
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
myTextView5.setText(Integer.toString(seconds));
myTextView4.setText(Integer.toString(minutes));
myTextView3.setText(Integer.toString(hours));
myTextView2.setText(Integer.toString(days));
myTextView1.setText(Integer.toString(months));
Detect change in preferences
public class MyPreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// handle the preference change here
}
}
I am trying to get a ValueAnimator to repeat once it has ended. I am using it with a SeekBar in a ListView. For some reason, the ValueAnimator will finish, trigger the onAnimationEnd() go again, but then when it reaches the end the onAnimationEnd() is never called a second time.
#Override
public View getContentView(int position, View convertView, ViewGroup parent) {
...
setupTimerBar(t);
...
}
private AnimatorListenerAdapter generateNewAnimatorListenerAdapter(final TylersContainer t) {
return new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
setupTimerBar(t);
}
};
}
private void setupTimerBar(TylersContainer t)
{
View view = t.getView();
BusTime arrivalTime = t.getBusTime();
int minutes = BusTime.differenceInMiuntes(arrivalTime, BusTime.now());
long milliseconds = minutes * 60 * 1000;
final TimerBar seekBar = (TimerBar) view.findViewById(R.id.SeekBar);
int progress = Utility.setProgress(arrivalTime, seekBar.getMax());
long duration = Utility.setAnimationDuration(progress);
seekBar.setProgress(progress);
seekBar.setAnimationDuration(duration);
seekBar.setAnimationStartDelay(milliseconds);
seekBar.setAnimatorListenerAdapter(generateNewAnimatorListenerAdapter(t));
}
The seekBar object is actually an custom object which contains a SeekBar and a ValueAnimator, here are the relevant bits:
//Constructor
public TimerBar(Context context) {
super(context);
startTime = Calendar.getInstance();
valueAnimator = ValueAnimator.ofInt(0, getMax());
//Override the update to set this object progress to the animation's value
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animProgress = (Integer) animation.getAnimatedValue();
setProgress(animProgress);
}
});
}
//Specify the start time by passing a long, representing the delay in milliseconds
public void setAnimationStartDelay(long milliseconds){
//Set the delay (if need be) and start the counter
if(milliseconds > 0)
valueAnimator.setStartDelay(milliseconds);
valueAnimator.setIntValues(this.getProgress(), this.getMax());
valueAnimator.start();
}
//Set the duration of the animation
public void setAnimationDuration(long duration){
valueAnimator.setDuration(duration);
}
public void setAnimatorListenerAdapter(AnimatorListenerAdapter ala){
valueAnimator.addListener(ala);
}
I can't figure out why it isn't repeating more than twice.
I've tried using the Repeat attribute and setting that to INIFINITI but that didn't help either.
Edit: To be clear, what I am trying to get is an animation that repeats itself indefinitely, each time with a different duration.
I have made the error of setting the RepeatMode to infinite which did not work, this must be set as the RepeatCount:
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
In case you are using Animator, and then bind it with Animator.AnimatorListener
The function AnimatorListener.onAnimationEnd() is used in case your animation repeats for finite times, and is only called once
In case your animation is repeating, for number of times, you should be using the function AnimatorListener.onAnimationRepeat() instead, which will apply everytime your animation repeats after the end of each repeatance
From what I understand, what you need is onAnimationRepeat(), so if you just move the code that you want to be executed after each repeat from onAnimationEnd() to onAnimationRepeat(), this should fix it
Reference: http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html