Reputation:
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.
Update : I'm able to inflate the datepicker onclick of different TextViews now, But only one TextView is updated on selection of the date instead on the corresponding TextView.
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" />
<TextView
android:id="@+id/TimeDateOccurence"
android:layout_width="match_parent"
android:textSize="15dp"
android:background="@drawable/roundwhite"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"
android:textColor="@android:color/black" />
This is how the Activity Fragment Looks now
public class FragmentReportAFault extends Fragment {
private View mRootView;
private Button mNextButton, mSaveButton, mDeleteButton;
private LinearLayout mFirstLayoutForm, mSecondLayoutForm;
private TextView mSelectorOne, mSelectorTwo;
private TextView DateOfEventForm,TimeDateOccurence;
int clicked;
@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);
TimeDateOccurence= (TextView)mRootView.findViewById(R.id.TimeDateOccurence);
}
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (clicked == 0) {
DateOfEventForm.setText(year + " -" + (monthOfYear + 1) + "-" + dayOfMonth);
}
else if (clicked == 1){
DateOfEventForm.setText(year + " -" + (monthOfYear + 1) + "-" + dayOfMonth);
}
}
};
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);
DateOfEventForm.setOnClickListener(onClick);
TimeDateOccurence.setOnClickListener(onClick);
}
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);
clicked =0;
DatePickerDialog dateDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
dateDialog.show();
}
break;
case R.id.TimeDateOccurence:{
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
clicked =1;
DatePickerDialog dateDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
dateDialog.show();
}
break;
}
}
};}
Upvotes: 1
Views: 317
Reputation: 683
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;
int clicked;
//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) {
if(clicked==0) {
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);
clicked =0;
DatePickerDialog dateDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
dateDialog.show();
}
break;
}
}
}; }
Upvotes: 1