Reputation: 1072
I tried to give datepicker by following android studio tutorial. This is my MainActivity.java
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),"datePicker");
}
}
DatePickerFragment.java looks like
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),"datePicker");
}
}
When i try to run the code the following error i got in cosole ::: Error:(18, 20) error: no suitable method found for show(android.support.v4.app.FragmentManager,String) method DialogFragment.show(android.app.FragmentManager,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager) method DialogFragment.show(FragmentTransaction,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)
Finally my activity_main.xml looks like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.acknotech.kiran.dateexample.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
</LinearLayout>
where i made wrong?
Thanks & Regards
Upvotes: 1
Views: 14798
Reputation: 740
Add this in your layout file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.geelani.datepicker.MainActivity">
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/pick_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick date"
android:onClick="showDatePickerDialog" />
and add this to your activity
public class MainActivity extends AppCompatActivity {
private int mYear,mMonth,mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button pickDate = (Button) findViewById(R.id.pick_date);
final TextView textView = (TextView) findViewById(R.id.date);
final Calendar myCalendar = Calendar.getInstance();
final 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);
// myCalendar.add(Calendar.DATE, 0);
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
textView.setText(sdf.format(myCalendar.getTime()));
}
};
pickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
if (year < mYear)
view.updateDate(mYear,mMonth,mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear,mMonth,mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear,mMonth,mDay);
textView.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
dpd.show();
}
});
}
}
Upvotes: 3
Reputation: 984
Here is an example to use DatePickerDialog:
final Calendar calendar = Calendar.getInstance();
dateFormat = new SimpleDateFormat("MM/dd/yyyy");
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(year, monthOfYear, dayOfMonth);// Selected Date Goes Here.
// You Can Do Anything You Want Here
}
}, calendar.get(Calendar.YEAR)
, calendar.get(Calendar.MONTH)
, calendar.get(Calendar.DAY_OF_MONTH));
Upvotes: 0
Reputation: 565
Try this, as picker has its own view that would pop up on using show and adding these values make it this as default date:
// On the click of registration field:
Calendar now = Calendar.getInstance()
new DatePickerDialog(classname.this, date, now
.get(Calendar.YEAR), now(Calendar.MONTH),
now(Calendar.DAY_OF_MONTH)).show(); // this shows the picker
Upvotes: 0