New Coder New Coder - 6 months ago 40
Android Question

How to add datepicker onclick of a TextViiew. Have multiple date fields in the form

I'm completely new to Android Programming, Sorry if I'm asking a dumb question. How to add datepicker onclick of a TextView and then on selection of the date it should update the corresponding TextViews. I have multiple date fields in the form that I'm making. And also have many Date Fields in other fragments too. I have seen many answers but not sure how to apply it. I mean which code goes where. Please kindly explain me in steps.




XML:

<TextView
android:id="@+id/DateOfEventForm"
android:textSize="15dp"
android:background="@drawable/roundwhite"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"
android:textColor="@android:color/black" />





Activity Fragment:

public class FragmentReportAFault extends Fragment {
private View mRootView;
private Button mNextButton, mSaveButton, mDeleteButton;
private LinearLayout mFirstLayoutForm, mSecondLayoutForm;
private TextView mSelectorOne, mSelectorTwo;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_reportafault, container, false);
uiInitialize();
setListners();
return mRootView;
}


private void uiInitialize() {
mFirstLayoutForm = (LinearLayout) mRootView.findViewById(R.id.FirstLayoutForm);
mSecondLayoutForm = (LinearLayout) mRootView.findViewById(R.id.SecondLayoutForm);
mNextButton = (Button) mRootView.findViewById(R.id.btnNext);
mSaveButton = (Button) mRootView.findViewById(R.id.btnSave);
mDeleteButton = (Button) mRootView.findViewById(R.id.btnDelete);
mSelectorOne = (TextView) mRootView.findViewById(R.id.selector1);
mSelectorTwo = (TextView) mRootView.findViewById(R.id.selector2);
}

private void setListners() {
mNextButton.setOnClickListener(onClick);
mSaveButton.setOnClickListener(onClick);
mDeleteButton.setOnClickListener(onClick);
mFirstLayoutForm.setVisibility(View.VISIBLE);
mSecondLayoutForm.setVisibility(View.GONE);
mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner_white);
}

View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnNext:{
mFirstLayoutForm.setVisibility(View.GONE);
mSecondLayoutForm.setVisibility(View.VISIBLE);
mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner);
mSelectorTwo.setTextColor(Color.WHITE);

}break;
case R.id.btnSave:{

}break;
case R.id.btnDelete:{

}
break;

}
}
}; }

Answer

Made changes in your code. I did not test it .Try this

private View mRootView;
private Button mNextButton, mSaveButton, mDeleteButton;
private LinearLayout mFirstLayoutForm, mSecondLayoutForm;
private TextView mSelectorOne, mSelectorTwo;

//added
private TextView DateOfEventForm;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.fragment_reportafault, container, false);
    uiInitialize();
    setListners();
    return mRootView;
}


private void uiInitialize() {
    mFirstLayoutForm = (LinearLayout) mRootView.findViewById(R.id.FirstLayoutForm);
    mSecondLayoutForm = (LinearLayout) mRootView.findViewById(R.id.SecondLayoutForm);
    mNextButton = (Button) mRootView.findViewById(R.id.btnNext);
    mSaveButton = (Button) mRootView.findViewById(R.id.btnSave);
    mDeleteButton = (Button) mRootView.findViewById(R.id.btnDelete);
    mSelectorOne = (TextView) mRootView.findViewById(R.id.selector1);
    mSelectorTwo = (TextView) mRootView.findViewById(R.id.selector2);
    DateOfEventForm= (TextView)mRootView.findViewById(R.id.DateOfEventForm);

}

private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        DateOfEventForm.setText(year +" -"+(monthOfYear+1)+ "-"+dayOfMonth);
    }
};

private void setListners() {
    mNextButton.setOnClickListener(onClick);
    mSaveButton.setOnClickListener(onClick);
    mDeleteButton.setOnClickListener(onClick);
    DateOfEventForm.setOnClickListener(onClick);
    mFirstLayoutForm.setVisibility(View.VISIBLE);
    mSecondLayoutForm.setVisibility(View.GONE);
    mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
    mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner_white);
}

View.OnClickListener onClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btnNext:{
                mFirstLayoutForm.setVisibility(View.GONE);
                mSecondLayoutForm.setVisibility(View.VISIBLE);
                mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
                mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner);
                mSelectorTwo.setTextColor(Color.WHITE);

            }break;
            case R.id.btnSave:{

            }break;
            case R.id.btnDelete:{

            }
            break;
           case R.id.DateOfEventForm:{

                  Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dateDialog = new DatePickerDialog(getActivity(),      dateSetListener, year, month, day);
    dateDialog.show();
            }
            break;

        }
    }
}; }
Comments