Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- Remove Calendarview from DatePickerDialogFragment

I have a Datepickerdialog fragment which works fine.When i click the button the datepickerdialog opens whixh has spinner date selection on the left side and calendar view on the right side.

For my app in one place i want just the spinner date selection part alone to be shown(calendar view part must be removed or hidden)

In other place i want calendar view part must be shown and the spinner date selection must be hidden or removed.

But i tried a lot with various things like for hiding calendar view like

 Method m = minDateSelector.getClass().getMethod("setCalendarViewShown", boolean.class);
 m.invoke(dp, false);

but when i add this i get error only...

Same problem for this person too Datepicker created with DialopFragment showing Calender also

my code is

DatePickerActivity.java

import java.util.Calendar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class DatePickerActivity extends Activity {
    DateDialogFragment frag;
    Button button;
    Calendar now;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        now = Calendar.getInstance();
        button = (Button)findViewById(R.id.date_button);
        button.setText(String.valueOf(now.get(Calendar.MONTH)+1)+"-"+String.valueOf(now.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(now.get(Calendar.YEAR)));
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog();   
            }
        });  
    }
    public void showDialog() {
        FragmentTransaction ft = getFragmentManager().beginTransaction(); //get the fragment
        frag = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
            public void updateChangedDate(int year, int month, int day){
                button.setText(String.valueOf(month+1)+"-"+String.valueOf(day)+"-"+String.valueOf(year));
                now.set(year, month, day);
            }
        }, now);

        frag.show(ft, "DateDialogFragment");
    }
    public interface DateDialogFragmentListener{
        //this interface is a listener between the Date Dialog fragment and the activity to update the buttons date
        public void updateChangedDate(int year, int month, int day);
    }  
}

DateDialogFragment.java

import java.util.Calendar;

import com.zeroe.DatePickerActivity.DateDialogFragmentListener;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;

public class DateDialogFragment extends DialogFragment {

    public static String TAG = "DateDialogFragment";
    static Context mContext; //I guess hold the context that called it. Needed when making a DatePickerDialog. I guess its needed when conncting the fragment with the context
    static int mYear;
    static int mMonth;
    static int mDay;
    static DateDialogFragmentListener mListener;

    public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener, Calendar now) {
        DateDialogFragment dialog = new DateDialogFragment();
        mContext = context;
        mListener = listener;

        mYear = now.get(Calendar.YEAR);
        mMonth = now.get(Calendar.MONTH);
        mDay = now.get(Calendar.DAY_OF_MONTH);
        /*I dont really see the purpose of the below*/
        Bundle args = new Bundle();
        args.putString("title", "Choose Date");
        dialog.setArguments(args);//setArguments can only be called before fragment is attached to an activity, so right after the fragment is created


        return dialog;
    }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(mContext, mDateSetListener, mYear, mMonth, mDay);
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;

            mListener.updateChangedDate(year, monthOfYear, dayOfMonth);
        }
    };
}

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="03-18-2012"
        android:onClick="clickMe"/>

</LinearLayout>
like image 461
sdg Avatar asked Dec 27 '22 10:12

sdg


1 Answers

I have used Datepicker Dialog Fragment, to show the Date picker in dialog.

Here is my 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>
like image 123
Iam4fun Avatar answered Feb 16 '23 10:02

Iam4fun