I'm stuck at here in this program,I want to access parameters of DatePickerFragment into TimePickerFragment.Please help.
public class datepicker extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
Button btn = (Button) findViewById(R.id.datepick);
Button btn2 = (Button) findViewById(R.id.timepick);
// TextView textview = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dFragment = new DatePickerFragment();
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dFragment = new TimePickerFragment();
// Show the time picker dialog fragment
dFragment.show(getFragmentManager(),"Time Picker");
}
});
}
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK, this, year, month, day);
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the chosen date
Button textview = (Button) getActivity().findViewById(R.id.datepick);
textview.setText(day + ":" + (month + 1) + ":" + year);
}
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog tpd = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, hour, minute, false);
return tpd;
}
public void onTimeSet(TimePicker timePicker, int i, int i1) {
Button tv = (Button) getActivity().findViewById(R.id.timepick);
tv.setText(i + ":" + i1);
DatePickerFragment b = new DatePickerFragment();
b.onDateSet(view ,int year,int month,int day);
}
}}
I need year,month,day in TimePickerFragment method.Please solve it asap.Thanks in advance.
Related
I have datepicker, time picker and edittext on my app. I want set notification on specific date and time with edittext variables. It can works but it work only time picker. It set notification with edittext only timepicker variables. How can I set notification with Time and Date picker variables.
mainactivty.class
public class mainactivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
Calendar now = Calendar.getInstance();
Button hatirlaticitarihsaat,backhome;
EditText editisim,editaciklama;
TimePickerDialog tpd;
DatePickerDialog dpd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hatirlatici);
editisim=findViewById(R.id.hatbaslik);
editaciklama=findViewById(R.id.hataciklama);
hatirlaticitarihsaat= findViewById(R.id.taarihsaatbutton);
hatirlaticitarihsaat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "date picker");
}
});
}
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
intent.putExtra("notificationId", notificationId);
intent.putExtra("messagex", editaciklama.getText().toString());
intent.putExtra("titlex", editisim.getText().toString());
intent.setAction("ALERT_RECIEVER");
PendingIntent alarmintent = PendingIntent.getBroadcast(
hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar startalarm = Calendar.getInstance();
startalarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
startalarm.set(Calendar.SECOND, 0);
startalarm.set(Calendar.MINUTE, minute);
long alarmstartTime = startalarm.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmstartTime, alarmintent);
}
private void startNotification(Calendar now){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlertReciever.class);
intent.putExtra("Isım_ID", editisim.getText().toString());
intent.putExtra("Aciklama", editaciklama.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);
}
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
now.set(Calendar.DAY_OF_MONTH, day);
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
String Datex = DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(now.getTime());
zaman.setText(Datex);
Calendar startalarm = Calendar.getInstance();
startalarm.set(Calendar.DAY_OF_MONTH,day );
startalarm.set(Calendar.YEAR, year);
startalarm.set(Calendar.MONTH, month);
}
}
TimePickerFragment.java
public class TimePickerFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour,minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
DatePickerFragment.java
public class DatePickerFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
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(), (DatePickerDialog.OnDateSetListener) getActivity(),year ,month,day);
}
}
I was trying to make an EditText where when I click on it a DatePickerDialog will show up so I can choose my birthday and then the date I choose will be written in the EditText. I found this code online but I am having problems with it as the line where it says:
new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar..
The datePickerListener is colored red and I don't understand why? If someone can help me I would really appreciate it.
//some of the needed imports
import android.widget.DatePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
//private field variables in the Activity class
private Calendar myCalendar = Calendar.getInstance();
private EditText etDate;
//onCreate method of the Activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etDate = (EditText) findViewById(R.id.et_date);
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
etDate.setText(sdf.format(myCalendar.getTime()));
}
};
}
You need to do some changes as below
Initialize the myCalendar to avoid NullPointerException
myCalendar = new GregorianCalendar();
Declare datePickerListener before using it to avoid Unresolved
variable
Change TheClassName.this to your activity class name to get the
context; I assumed below it as MainActivity.this
Make EditText & DatePickerDialog.OnDateSetListener as final in order to access them from the EditText OnClickListener callback
So with these changes in place, you can replace your code with below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etDate = (EditText) findViewById(R.id.et_date);
myCalendar = new GregorianCalendar();
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
etDate.setText(sdf.format(myCalendar.getTime()));
}
};
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, datePickerListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
I don't get any errors but this doesn't work every time I try to change date in the calendar my app stops working and even when I press the save button it also keeps stop working before I add the code with the array it was working properly
public class Calendar extends AppCompatActivity {
private CalendarView calendarView;
private TextView currentDate;
private Button btnsavenote;
private EditText newNote, showNote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
calendarView = (CalendarView) findViewById(R.id.calen_v);
currentDate = (TextView) findViewById(R.id.currentDate);
btnsavenote = (Button) findViewById(R.id.save_note);
newNote = (EditText) findViewById(R.id.newNote);
showNote = (EditText) findViewById(R.id.shownote);
final String [][] array = new String[31][12];
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int year, int month, int dayOfMonth) {
String tDate = (dayOfMonth) + "/" + month + "/" + year;
currentDate.setText(tDate);
}
});
btnsavenote.setOnClickListener(new View.OnClickListener() {
String MytextMessage = newNote.getText().toString();
#Override
public void onClick(View v) {
int a;
int b;
a = Integer.parseInt(array [30][2]);
b = Integer.parseInt(array [31][2]);
array[a][b] = MytextMessage;
}
});
}
}
So, I try to set an OnDismissListener on a dialog thing.
Datepicker dialog = new Datepicker(v);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface Dialog) {
mGoogleMap.clear();
setMap(mGoogleMap);
}
});
But it says
"Cannot resolve method 'setOnDismissListener(anonymous android.content.DialogInterface.OnDismissListener)'"
The Datepicker class extends DialogFragment, so it should have the setOnDismissListener method?
I've imported android.content.DialogInterface'. Is it confused on thenew DialogInterface.onDismissListener()` for some reason?
Edit: Someone asked for some of the Datepicker code. So here's the constructor and stuff. Let me know if you need anything else.
public class Datepicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
public Datepicker(View v){
txtDate = (EditText)v;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
You have to resolve this issue with a help of interface
public class Main3Activity extends AppCompatActivity implements DatePickerListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
//Initialise or create your dialog like this
DatePicker dialog = new DatePicker(v,this);
}
#Override
public void onDatePickerDismissed() {
//Here You receive the dialog dissmiss listner
mGoogleMap.clear();
setMap(mGoogleMap);
}
}
Create a interface class like
public interface DatePickerListener {
public void onDatePickerDismissed();
}
Change you dialog picker like this
#SuppressLint("ValidFragment")
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
DatePickerListener datePickerListener;
#SuppressLint("ValidFragment")
public DatePicker(View v, DatePickerListener _datePickerListener){
txtDate = (EditText)v;
this.datePickerListener = _datePickerListener;
}
public DatePicker() {
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(android.widget.DatePicker datePicker, int i, int i1, int i2) {
}
#Override
public void onDismiss(DialogInterface dialog) {
datePickerListener.onDatePickerDismissed();
super.onDismiss(dialog);
}
}
Hope it will help you :)
I am trying to set an editText with a DatePickerDialog inside a .setOnClickListener. where, when the user clicks on the editText the DatePickerDialog will appear and then after the user selects the desirable date it will set it on the editText... something like this : Android:DatePickerDialog on EditText Click Event
and here is my code mycode
Any ideas how to do that?
I tried the above example and many more but nothing seems to work inside the .SetOnClickListener
Use this code:
public class MainActivity extends AppCompatActivity {
EditText text;
private int year,currentYear;
private int month,currentMonth;
private int day,currentDay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.edit);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
currentDay = mDay;
currentMonth = mMonth+1;
currentYear = mYear;
text.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
}
},year,month,day).show();
}
});
}
}
I have store the user selected year, month, day in currentYear,currentMonth,currentDay respectively.