S.Aofled S.Aofled - 1 month ago 27
Java Question

Android TimePicker at intervals for 5 minutes

My problem is that I need to implement TimePicker at intervals for 5 minutes.

All the examples that I was looking for here

Android - TimePicker minutes to 15

and here

How do i change the Android TimePicker minute intervals?

for me does not work.

If I may, I would like to see a full working example.

This code works, but I do not know how to pass the time here to install, and do not know how to get the time

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

TimePicker pickStartTime;
TextView textwiew;
private int TIME_PICKER_INTERVAL = 5;
NumberPicker minutePicker;
List<String> displayedValues;


final Calendar cal = Calendar.getInstance();

private int myHourS = cal.get(Calendar.HOUR_OF_DAY);
private int myMinuteS = cal.get(Calendar.MINUTE);

int nextMinute = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textwiew =(TextView)findViewById(R.id.text1);

pickStartTime = (TimePicker)findViewById(R.id.timePicker1);
pickStartTime.setIs24HourView(true);

setTimePickerInterval(pickStartTime);
}


private void setTimePickerInterval(TimePicker timePicker) {
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
// Field timePickerField = classForid.getField("timePicker");

Field field = classForid.getField("minute");
minutePicker = (NumberPicker) timePicker
.findViewById(field.getInt(null));

minutePicker.setMinValue(0);
minutePicker.setMaxValue(11);
displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
// for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
// displayedValues.add(String.format("%02d", i));
// }
minutePicker.setDisplayedValues(displayedValues
.toArray(new String[0]));
minutePicker.setWrapSelectorWheel(true);
} catch (Exception e) {
e.printStackTrace();
}
}

public void onClick (View v) {
int Minut = pickStartTime.getCurrentMinute();
textwiew.setText(Minut);
}


}

Answer

do not know how to get the time

Using OnTimeChangedListener

pickStartTime = (TimePicker)findViewById(R.id.timePicker1);
pickStartTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Log.d("Time", "You picked "
                + String.valueOf(view.getCurrentHour()) + ":"
                + String.valueOf(view.getCurrentMinute()));
    }

});