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
Related
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();
}
});
}
Here is my code, in my ListViewAdapter. I want to show a timePicker to set time for my Adapter. But a cannot solve my onTimeSet
p is my Adapter
assert btnLeft != null;
btnLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
p.setOnHour(hourOfDay);
p.setOnMinutes(minute);
btnLeft.setText(hourOfDay + " : " + minute);
}
}, p.getOnHour(), p.getOnMinutes(), false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
notifyDataSetChanged();
}
According to the docs, OnTimeSetListener calls onTimeSet() when
... the user is done setting a new time and the dialog has closed.
You need to use an OnTimeChangedListener with the onTimeChanged() method.
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());
I have followed this tutorial to integrate a datepicker into my app, I have tailored the original to suit my needs and managed to get it working. Unfortunately the author doesn't go too much into the concepts of actually using the DatePicker dialog.
As a result, a full screen calendar is shown when the app is launched, whereas I am simply trying to pull the day, month and year when the button 'Pick Date' is clicked, what do I need to remove?
Code here:
public class MainActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
TabHost th;
Button changeDate;
TextView displayDate;
public static final int dDialogId = 1;
// date and time
private int mYear;
private int mMonth;
private int mDay;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Set activity to full screen!!
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
// one tab
specs.setContent(R.id.tab1);
// what appears on actual tab
specs.setIndicator("Tools");
th.addTab(specs);
specs = th.newTabSpec("tag2");
// one tab
specs.setContent(R.id.tab2);
// what appears on actual tab
specs.setIndicator("Calorie Calculator");
th.addTab(specs);
specs = th.newTabSpec("tag3");
// one tab
specs.setContent(R.id.tab3);
// what appears on actual tab
specs.setIndicator("Your Stats!");
th.addTab(specs);
initializeVariables();
}
private void initializeVariables() {
mapARun = (Button) findViewById(R.id.btnMapARun);
weightConverter = (Button) findViewById(R.id.btnWeightConverter);
changeDate = (Button) findViewById(R.id.btnChangeDate);
displayDate = (TextView) findViewById(R.id.tvDisplayDate);
changeDate.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
#Override
#Deprecated
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
}
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 void updateDisplay() {
displayDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnChangeDate:
DatePickerDialog DPD = new DatePickerDialog(this,
mDateSetListener, mYear, mMonth, mDay);
DPD.show();
break;
}
}
}
}
Apologies if you hit syntax errors when the codei s run, I cut out a lot of the other, unrelated functions,
Is there another way to just show the datepicker when the dialog is called?
Create yout main category with just the button.
Make the button call another activity with your code.
You'll get a screen with a button that calls another screen with a fullscreen datepicker.
Make the DatePicker Activity end itself when date is selected.
Make sure you are calling the activity with startActivityForResult and end it with finishWithResult.
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.