I want a app that sets a alarm according to a time-picker and not a data-picker, i want the alarm to go off every day at the time the time-picker is set at.
I don't want to have a button which becomes a picker i want to use the time-picker icon.
If you want to have a Button without Text, you can use
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_icon"
... />
If you want to set up an Alarm create a new class, that shows TimePicker in FragmentDialog
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Here you get the selected Time, do whatever you want
}
}
And in the Activity, where is the Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pick_time"
android:onClick="showTimePickerDialog" />
Set OnclickListener, which starts FragmentDialog with TimePicker
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker
I dont really understand, what are you actually asking for. Clarify your question please.
Related
I am new in android, and I tried to add time picker dialog to my app to take the time from the user.
but the dialog was not appear.
I am using android studio.
this is the code:
Calendar calendar=Calendar.getInstance();
int hour= calendar.get(Calendar.HOUR_OF_DAY);
int minutes1= calendar.get(Calendar.MINUTE);
EditText time=(EditText) findViewById(R.id.TimeEdT);
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(hourOfDay+":"+minute);
}
},hour, minutes1, true
);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
the dialog is not appear when i tried to run the application.
my goal is to take the time from the user and use it as input to text, then use it for alarm notification.
please help
Your time View is an EditText.
When you click in an EditText it puts the cursor within the text allowing you to edit it, bringing up a soft keyboard if required.
It seems you want to get rid of that functionality and bring up a TimePickerDialog instead when you click on it. So if you don't need that extra functionality, just use a TextView instead.
I am using following code to pick date, however i wanted to pick the date as well as time (in a single dialog box)...
DatePickerDialog.OnDateSetListener beginDate = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,monthOfYear);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
setBeginDateOnView();
}
};
public void beginDateOnClick(View view) {
new DatePickerDialog(CreateEventActivity.this,beginDate,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
public void setBeginDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
edtBeginDate.setText(sdf.format(c.getTime()));
There is no built-in "date and time picker in one" solution for Android, so you'll have to combine a DatePicker and a TimePicker into a dialog by yourself, or use a library that does it.
For a library, i can suggest using SlideDateTimePicker, i used it in a project a while back and it works well.
Or you can write your own, by simply creating a new class that extends AlertDialog, setting up 2 tabs inside it and then setting the tabs to include a DatePicker and a TimePicker.
I am new to Android programming. Please help.
I am using Fragment that creates Material design DatePickerDialog on click of EditText. Trouble is it is set to current date (set by me). But, if user has to select the date in the past ... say, 10 years ago, user has to scroll each month which is painful. e.g. shown below:
Is there a way to make your select the year? This way user can navigate to the year.
At least on Android 7, you can click on the year and get a year selection dialog:
I think than that is because you don't have a minimum date or maxim date. If you try to create a custom dialog with DatePicker, it works. I have a same example with an Edittext. I call this method when the user to click in Edittext.
private void showDateDialog() {
mLayoutInflater = getLayoutInflater();
mCustomDatePicker = mLayoutInflater.inflate(R.layout.custom_date_picker, null);
mDatePicker = (DatePicker) mCustomDatePicker.findViewById(R.id.datePicker);
mDatePicker.setMaxDate(Calendar.getInstance().getTime().getTime());
mCalendar = Calendar.getInstance();
mDialog = new AlertDialog.Builder(this);
mDialog.setView(mCustomDatePicker);
mDialog.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mCalendar.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
mBirthdayEdit.setText(mFormatDate.format(mCalendar.getTime()));
}
}).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mDialog.create();
mDialog.show();
}
You layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<DatePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_gravity="center_horizontal"
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_alignParentTop="true"/>
</RelativeLayout>
public void setMaxDate() {
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new
DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int
monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
//use this date as per your requirement
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(EditProfileActivity.this, dateSetListener,
currentDate.get(Calendar.YEAR),
currentDate.get(Calendar.MONTH ),
currentDate.get(Calendar.DAY_OF_MONTH));
// Limiting access to past dates in the step below:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -14);
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
This is working code ,you can set 14 year ago max date
Enjoy...!
Over the past 24 hours I have been trying to find articles about my issue but am still confused. If I have missed a place where this question was previously answered I apologize but I hope that you all will still help me understand what I am missing.
Synopsis: I want to populate a few text boxes with a date picker and am having trouble understanding how to return the selected date so that I can populate the appropriate textbox.
Basically I'm trying to implement a standard date picker targeting android devices >= 4.0.
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
You will notice that this is the code from the android documentation example that can be found here: http://developer.android.com/guide/topics/ui/controls/pickers.html
I am implementing this code in a larger project and the date picker comes up fine, the date is picked fine etc.
What I am having trouble with is getting the date after the user selects it. Basically the user selects the date and then nothing happens. What I want to do is populate a specific text box with the date after selection. This will happen in a few text boxes so I can't just hard code it into the onDateSet function.
My thought was to pass the textBox's id with the creation of the datepicker but I am not completely sure how I would do that.
This is my first android project and focused on comprehension and not simply getting a working project, thanks.
The "onDateSet" method should be called when the user selects a date.
In this method, you can use the year, month and day passed in parameter.
I don't known if that is the best way to do it, but I solve it this way:
I have two buttons, A and B.
When i click button A, a DatePicker appears and the user selects the date.
After confirm the date, the text in Button A is set to that date that was selected.
Same behavior for button B.
That is the function when button A or B is pressed:
public void selectDateButtons(View view){
if(view.getId() == R.id.buttonA)
{
clickedButton = R.id.buttonA;
}else{
clickedButton = R.id.buttonB;
}
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
clickedButton is a Static global variable.
Then declare the sub-class
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if(clickedButton == R.id.ButtonA)
{
ButtonA.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
} else {
ButtonB.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
}
}
}
Don't forget, your main activity should extends FragmentActivity.
Hope that works for you too!
Best regards!
When user clicks the edtText the first time the keypad appears, the user needs to click another time so the dialog appears. Even while debugging it reaches the show method of the timePickerDialog, but still on the first time the keypad appears, on the second click the dialog appears
public class NewAssignment extends FragmentActivity implements DatePickerDialog.OnDateSetListener,
TimePickerDialog.OnTimeSetListener
{
EditText timePicker;
timePicker = (EditText) findViewById(R.id.timePicker);
timePicker.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showTimePickerDialog();
}
});
public void showTimePickerDialog()
{
final Calendar c = Calendar.getInstance();
int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
TimePickerDialog td = new TimePickerDialog(this, this, hourOfDay, minute, DateFormat.is24HourFormat(this));
td.show();
}
}
i encountered the same problem, while implementing time and date pickers. following code did it for me
timePicker.setInputType(InputType.TYPE_NULL);
but then you would have to click twice to make the time picker dialog appear. so add
timePicker.setFocusable(false);
and everything should work as expected.