How to show timepickerdialog with 12h AM/PM - java

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.

Related

How to make timePicker in Android Studio 3

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();

datepickerdialog theme_holo_light not displayed correctly in Samsung J7 Core

I use java code to display datepickerdialog with a theme THEME_HOLO_LIGHT.
My compiled SDK version is 27. The theme is displayed perfectly in an oppo device (Nougat 7.0). However, in samsung J7 Core, the output is a different THEME.
Sample output of THEME_HOLO_LIGHT
The result of an output in Samsung is THEME_DEVICE_DEFAULT_DARK.
I want the THEME_HOLO_LIGHT to display. Please help.
datePickerDialog = new DatePickerDialog(this,AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
USERBIRTHDATE = dateFormat.format(new Date(year - 1900, month, dayOfMonth));
dateTxt.setText(USERBIRTHDATE);
flag = true;
}
}, 1997, 0, 01);
Instead of AlertDialog.THEME_HOLO_LIGHT write android.R.style.Theme_Holo_Light.
If this doesn't work, replace it with this:
datePickerDialog = new DatePickerDialog(this,new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light), new DatePickerDialog.OnDateSetListener() {..}

Auto Silent android device with timer set my user

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.

Date displaying twice in date picker

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);

Setting user picked date from DatePicker to Calendar

So I need for user to select date from DatePicker Dialog and then when he selects date I want it to be shown on Calendar. For example, if user selects 01/01/2016 I want that date to be shown as some red marked date.
Here's my code:
displayDatumaDodajPisaniIspit = (TextView)dialog. findViewById(R.id.displayDatumaDodajPisaniIspit);
set = (Button)dialog. findViewById(R.id.set);
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(HomeScreen.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
And the listener
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
displayDatumaDodajPisaniIspit.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
calendarView.setDateSelected();
}
};
I don't have idea how to start with it. Tried with some methods in listener but it didn't work.
You should first convert the chosen date into millis -
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
long millis = calendar.getTimeInMillis();
and then set the date in your CalendarView like this -
calendarView.setDate(millis);
As you mentioned you are using MaterialCalendarView, then I guess you can use -
calendarView.setSelectedDate(CalendarDay.from(year,monthOfYear,dayOfMonth));

Categories

Resources