I want to show month and year only, how i can do it? I have been try to search some solution but for me it's not work. May be i need to create my custom date picker dialog ? Here is my code:
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()
{
public int Syear;
public int Smonth;
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH,monthOfYear);
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// Auto-generated method stub
}
});
}
};
public void setDate()
{
new DatePickerDialog(InboxActivity.this,d,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
#Override
public void onClick(View v)
{
this.setDate();
}
May be this may work... not a tested solution though
import java.util.date;
SimpleDateFormat dateformat=new SimpleDateFormat("MM/yyyy");
dateformat.format(new Date());
Related
So i got this from another question since i can comment, i am making the question as something new
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
tv_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
the code on top does a date piker but i want a different theme i want "AlertDialog.THEME_HOLO_DARK"
but i dont know where to add this...
original solution fund here
Datepicker: How to popup datepicker when click on edittext
I'm learning about screen rotation for an App, I came across this example and I tried it.
My issue is that when I try this with a Button widget, it's text is still blank. When the button is clicked a date picker dialog appears to pick a date. But if the screen is rotated after the selection is made, the previously selected date is lost.
This is my code:
startDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setDate(startDate);
}
});
public void setDate(final Button B)
{
final Calendar calender = Calendar.getInstance();
day = calender.get(Calendar.DAY_OF_MONTH);
month = calender.get(Calendar.MONTH);
year = calender.get(Calendar.YEAR);
DatePickerDialog pickDate = new DatePickerDialog(add_event.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
calender.set(i,i1,i2);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MM-yyyy");
String dateString = dateFormat1.format(calender.getTime());
B.setText(dateString);
}
},year,month,day);
pickDate.show();
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("startDate", startDate.getText().toString());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
startDate.setText(savedInstanceState.getString("startDate"));
}
Here are some images:
Try using the version of onSaveInstanceState(Bundle outState) with just one argument. The other is for when you have an attribute set in the manifest for the activity to persist across reboots.
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.
hi i my app i have placed two edit text boxes, when i touch on it the date picker dialog box gets appeared.
Now the problem is when i touch the first edit box the dialog opens and after setting it displays at the EditText1. Then when i touch the second edit box the dialog opens and after setting some other date, it is not displayed in EditText2, instead it is show in the EditText1 and the former date gets changed
I want the dates to be displayed in respective boxes.
The following is my code
{
et1 =(EditText)findViewById(R.id.widget29);
et1.setHint("DOB");
et2 =(EditText)findViewById(R.id.widget32);
et2.setHint("DOF");
et1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
et2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
private void updateDisplay()
{
this.et1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append("-"));
}
private void updateDisplay1()
{
this.et2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append("-"));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay1();
}
};
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
}
the following are some more problems i am facing here,
Whenever i touch the edit text box the keyboard also gets opened how to hide the keyboard
in the EditText box i want the order to be viewed as Year/Month/Date
Is there any way to change the date picker dialog box as the below figure
please help me in this
Check this code..
public class Main extends Activity {
EditText et1,et2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_ID1 = 1;
private int mYear;
private int mMonth;
private int mDay;
private int mYear1;
private int mMonth1;
private int mDay1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1=(EditText)findViewById(R.id.EditText01);
et2=(EditText)findViewById(R.id.EditText02);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
et1.setText("month/year");
et2.setText("month/year");
et1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
return false;
}
});
et2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID1);
return false;
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1,
mDay1);
}
return null;
}
// updates the date in the TextView
private void updateDisplay() {
et1.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mYear));
}
private void updateDisplay1() {
et2.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth1 + 1).append("-").append(mYear1));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year1, int monthOfYear1,
int dayOfMonth1) {
// TODO Auto-generated method stub
mYear1 = year1;
mMonth1 = monthOfYear1;
mDay1 = dayOfMonth1;
updateDisplay1();
}
};
}
You're setting the same id for both show dialog methods...
et1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
et2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
...and your switch/case block only has one 'case'....
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
As for the other issues, split them into separate questions.