Datepicker created using DialopFragment showing Calender also

I am new to Android programming and I am using a basic Datepicker created using DialogFragment. My DatePicker displays fine when pressed, etc., but the problem is that it also displays the Calendar view. I do not want this. I have been looking for a solution for the day, and now I do not want to use custom Datepickers, since I want to get familiar with the basics first.

I also read sentences like

yourDatepicker.setCalendarViewShown(false); 

or set it to false in XML. But my DatePicker comes from the DialogFragment dialog, so how do I access this dumper? How to set the final look? I do not want to make changes to the source code, as I am still studying the ropes.

My datpike code:

 public class DueDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override 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); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day){ // Code to set Due Date TextField to the selected date. TextView reminderText = (TextView)getActivity().findViewById(R.id.addTaskFormDueDateHolder); String dueDateStr = year + "-" + month + "-" + day; reminderText.setText(dueDateStr); } } 
0
source share
2 answers

I used a snippet of the Datepicker dialog box to display the date picker dialog in the dialog box.

Here is my full code

MainActivity.java

 package com.example.testmydrag; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.DatePicker; import android.support.v4.app.FragmentActivity; import android.app.Dialog; import android.app.DatePickerDialog; import android.support.v4.app.DialogFragment; import java.util.Calendar; public class MainActivity extends FragmentActivity { EditText mEdit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void selectDate(View view) { DialogFragment newFragment = new SelectDateFragment(); newFragment.show(getSupportFragmentManager(), "DatePicker"); } public void setTheDate(int year, int month, int day) { mEdit = (EditText)findViewById(R.id.Text); mEdit.setText(month+"/"+day+"/"+year); } /* * Date Picker Dialog */ public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int yy = c.get(Calendar.YEAR); int mm = c.get(Calendar.MONTH); int dd = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE); /*Calendar View if you want to Remove Set it to False*/ dialog.getDatePicker().setCalendarViewShown(true); /*Spinner View if you want to Show Set it to True*/ dialog.getDatePicker().setSpinnersShown(false); dialog.setTitle("Pick a date"); return dialog; } public void onDateSet(DatePicker view, int yy, int mm, int dd) { setTheDate(yy, mm+1, dd); } } } 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:text="@+string/date_text" android:id="@+id/Text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+string/pick_date" android:onClick="selectDate" /> </LinearLayout> 
+1
source

I found a temporary solution, although I'm not quite happy with that. To do this, you need a minimum SDK version 11.

I basically added the call to getDatePicker () to my onAcreateDialog function specified in DueDatePickerFragment, and then changed its settings. I don’t understand why I need it and why it does not look right in my emulator. It seems that someone else does not have this problem. Any explanation would be highly appreciated. Here is the code:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState){ Log.i("DUEDATEPICKERDIALOG", "Inside onCreateDIALOG"); 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); DatePickerDialog datePickerDialogObj = new DatePickerDialog(getActivity(), this, year, month, day); DatePicker dueDatePicker = datePickerDialogObj.getDatePicker(); float scaleVal = (float) 0.6; dueDatePicker.setScaleX(scaleVal); dueDatePicker.setScaleY(scaleVal); dueDatePicker.setCalendarViewShown(false); return datePickerDialogObj; } 

Hope this helps someone.

0
source

All Articles