I am very beginner in android development and i need somebody help for this question. I want to make an Alarm Manager (Simple alarm timer, for start), and i need to make a time picker. So, i want to make material time picker and get information from it. Can someone help me with it?
Next time I suggest you search in google first, you can make a time picker dialog like this
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
Related
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.
My app uses a DatePicker. When I open the DatePicker and click on Done the date gets choosen. The problem is, that the date also gets choosen, if the user clicks besides the DatePicker. See here:
The Listener should only react if the Done Button is clicked. But it also reacts if the surrounding (red) of the DatePicker is clicked.
My Question:
How do I prevent this?
If you want to see some code, pls leave a comment.
I already tried this: How to handle date picker dialog not to set in edittext when clicked outside of dialog android?
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//DatePickerDialog mDatePicker;
DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, new DatePickerDialog.OnDateSetListener() {
private boolean fired;
public void resetFired(){
fired = false;
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(view.isShown()){
if(fired){
setDate(year, monthOfYear, dayOfMonth);
//ListView erneuern
adapter.notifyDataSetChanged();
return;
}
fired = true;
}
}
}, mYear, mMonth, mDay);
mDatePicker.setCanceledOnTouchOutside(true);
mDatePicker.setTitle("Wähle ein Datum");
mDatePicker.show();
But it does not work correctly. The click on the background bug gets solved. But now the Done Button stops working.
A simple dialog containing an DatePicker.
So You can set to no-cancelable.
using setCancelable API
.setCancelable(false);
Dialog reference
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.
Current date is displaying twice in my date picker android app I don't
understand why this is happening.Can anyone tell me what is causing this.
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
t1.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
This is the image
What you are facing is the manner how the date picker is implemented. Usually, the title date should go away if you use it like this:
datePickerDialog.setTitle("");
this call have to be directly before datePickerDialog.show();
You need to give a "time" for the fragment to be rendered.
Try this:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 1000);
I'm learning Android so I made an "app" but when the app open the timepickerdialog it shows a 24h clock. I need to show 12h AM/PM.
I read that I need to use the is24HourView, but I dont know how and where. Can you show me where? Here is part of the code.
private void setDateTimeField() {
theHour.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
theHourPickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar theTime = Calendar.getInstance();
theTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
theTime.set(Calendar.MINUTE, minute);
theHour.setText(timeFormatter.format(theTime.getTime()));
}
}, newCalendar.get(Calendar.HOUR_OF_DAY), newCalendar.get(Calendar.MINUTE),true);
}
I tried to put theHourPickerDialog.is24HourView(false) but it did not work XD
Thanks!
Notice that you should call setIs24HourView(false) before you set time on here.