Search date with specific label through textview - java

i got some stuck because i want to use the textview to search the date and show the data i want. but only the hard code i can do, and i dunno how to make the search through the textview and display the data i search the date.
how do i search the date, because i set the date as label, but i need my recyclerview to know what my date is showed.
here's the sample
date = findViewById(R.id.DateTextView);
recyclerView = findViewById(R.id.staffList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
attendances = new ArrayList<Attendance>();
recyclerView.setVisibility(View.GONE);
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int YEAR = calendar.get(Calendar.YEAR);
int MONTH = calendar.get(Calendar.MONTH);
int DATE = calendar.get(Calendar.DATE);
DatePickerDialog datePickerDialog = new DatePickerDialog(ViewAttendActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, month);
calendar1.set(Calendar.DATE, day);
String dateText = DateFormat.format("MM-dd-yyyy", calendar1).toString();
date.setText(dateText);
}
}, YEAR, MONTH, DATE);
datePickerDialog.show();
}
});
String dateString = date.toString();
if (dateString.isEmpty()){
Toast.makeText(ViewAttendActivity.this, "Please Select Date First!!!", Toast.LENGTH_SHORT).show();
} else {
recyclerView.setVisibility(View.VISIBLE);
databaseReference = FirebaseDatabase.getInstance().getReference("Attendance").child("08-15-2021");
databaseReference = FirebaseDatabase.getInstance().getReference("Attendance").child(dateString);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()){
Attendance attendance = dataSnapshot.getValue(Attendance.class);
attendances.add(attendance);
}
editStaffAttendAdapter = new EditStaffAttendAdapter(attendances, ViewAttendActivity.this);
recyclerView.setAdapter(editStaffAttendAdapter);
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
Toast.makeText(ViewAttendActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}

You can retrieve the string showed by a textview using the .getText().toString() attribute, like this:
TextView date = (TextView) findViewById(R.id.DateTextView);
//Retrieve the text showed by the date TextView
String dateShowedByTextView = date.getText().toString();

Related

I want to pass Date and Time I got using Datepicker Dialog and Timepicker Dialog to next Activity in Android studio

This is the Java code that Datepicker and Timepicker work. Those two are working properly so I want to send that selected date and time to the next activity(Doctor_Time_Picking_data_page.java)
Doctor_Time_Picking_page.java
public class Doctor_Time_Picking_page extends AppCompatActivity {
public static final String TEXT_TO_SEND ="com.example.dogapp.TEXT_TO_SEND";
private DatePickerDialog datePickerDialog;
private Button dateButton;
private Button timeButton;
private Bundle savedInstanceState;
private Button saveButton;
private String DATE;
//On Create method-------------------------------------------------------------------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_time_picking_page);
initDatePicker();
dateButton = findViewById(R.id.datePickerButton);
timeButton = findViewById(R.id.timeButton);
saveButton = findViewById(R.id.date_time_save_button);
Intent intent = new Intent(getApplicationContext(),Doctor_Time_Picking_data_page.class);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent);
}
});
}
//-----------------------------------------------------------------------------------------------------------------------------------
private Bundle initDatePicker()
{
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day)
{
month = month + 1;
String date = makeDateString(day, month, year);
dateButton.setText(date);
}
};
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int style = AlertDialog.THEME_HOLO_LIGHT;
datePickerDialog = new DatePickerDialog(this, style, dateSetListener, year, month, day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return null;
}
private String makeDateString(int day, int month, int year)
{
return getMonthFormat(month) + " " + day + " " + year;
}
private String getMonthFormat(int month)
{
if(month == 1)
return "JAN";
if(month == 2)
return "FEB";
if(month == 3)
return "MAR";
if(month == 4)
return "APR";
if(month == 5)
return "MAY";
if(month == 6)
return "JUN";
if(month == 7)
return "JUL";
if(month == 8)
return "AUG";
if(month == 9)
return "SEP";
if(month == 10)
return "OCT";
if(month == 11)
return "NOV";
if(month == 12)
return "DEC";
//default should never happen
return "JAN";
}
public void openDatePicker(View view)
{
datePickerDialog.setTitle("Select Date");
datePickerDialog.show();
}
//Time Button
int hour, minute;
public void popTimePicker(View view)
{
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener()
{
#Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute)
{
hour = selectedHour;
minute = selectedMinute;
timeButton.setText(String.format(Locale.getDefault(), "%02d:%02d",hour, minute));
}
};
TimePickerDialog timePickerDialog = new TimePickerDialog(this, /*style,*/ onTimeSetListener, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
}
This is the page where I want to show the date and Time selected form my previous activity(Doctor_Time_Picking_page.java)
Doctor_Time_Picking_data_page.java:
public class Doctor_Time_Picking_data_page extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_time_picking_data_page);
Button Button1a = findViewById(R.id.doc_page4_btn1);
Button Button2a = findViewById(R.id.doc_page4_btn2);
Button Button3a = findViewById(R.id.doc_page4_btn3);
Dialog nDialog = new Dialog(this);
Button1a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_Appoinment_payment.class);
startActivity(intent);
}
});
Button2a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_Time_Picking_page.class);
startActivity(intent);
}
});
Button3a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nDialog.setContentView(R.layout.activity_doctor_delete_popup_msg);
nDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_delete_popup_msg.class);
startActivity(intent);
}
});
}
}
In my project there are three buttons those are Select Time, Select Date and save button when I select each button Datepicker and Timepicker dialogues appear when I select Date or time those Data is appearing on the Buttons I want to pass those data to my next activity which is Doctor_Time_Picking_data_page.java and display them to user. What I now want is I want to pass That data selected from those Pickers to next activity
You need to pass an intent extra to the other activity to get your result. Follow the steps:
Create a filed name date in your class:
private String date = "JAN 1 2022"; // I have given a sample date
You need to store the date inside the DateSetListener like this:
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day)
{
month = month + 1;
date = makeDateString(day, month, year);
dateButton.setText(date);
}
};
You need to pass that date on the click of the save button:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent.putExtra("date", date);
startActivity(intent);
}
});
Then you need to get that date on the other activity like this:
public class Doctor_Time_Picking_data_page extends AppCompatActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_time_picking_data_page);
String date = getIntent().getStringExtra("date", "");
// 👆 that is the date from the previous activity
...
});
...
}

get time and date in each button click android

I have this button that will generate time and date and set it as a child in a database tree, the problem is whenever I click buttons it generates the said time but the time is not changing,
for example i clicked on the button and it generates the time 12:27 Pm, and it will be recorded into the database, now i clicked the button again for the second time and it doesn't change still printing out the 12:27 Pm Instead 12:30 Pm.
the code looks like this
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer num1 = Integer.parseInt(edt1.getText().toString());
Integer num2 = Integer.parseInt(edt2.getText().toString());
Integer result = num1+num2;
String data = "Date Computed: " + Date + Hrs +"Result: "+ result;
DateView.setText(data);
HashMap<String, String> userMap = new HashMap<>();
userMap.put("Add", data);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("Users").child("UserID").child(Date);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
maxid = (snapshot.getChildrenCount());
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
reference.child("Transaction"+maxid).setValue(userMap);
}
});
edt1 = findViewById(R.id.edt1);
edt2 = findViewById(R.id.edt2);
btnadd = findViewById(R.id.btnadd);
btnmin = findViewById(R.id.btnmin);
DateView = findViewById(R.id.DateView);
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date = simpleDateFormat.format(calendar.getTime());
simpleHoursFormat = new SimpleDateFormat(" hh:mm:ss ");
Hrs = simpleHoursFormat.format(calendar.getTime());
Hi from the code it looks like you haven't updated your Date and Time in your onClickListener. The updated code should be like this :-
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// This will fetch your updated date and time
Date = simpleDateFormat.format(calendar.getTime());
Hrs = simpleHoursFormat.format(calendar.getTime());
Integer num1 = Integer.parseInt(edt1.getText().toString());
// Rest of your code
Try to use this
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();

Textview is updated only after picking date twice

I'm using two text views in a dialog, one is for from date and another is for to date. Now when i click on from date text view the date picker dialog opens and when i select the date it is not updated in the text view, if i open the date picker again and select the date for the second time the date is updated in the text view. can any one figure out why it is not updated the first time.
public static void datePickerDialog(final Context context) {
dialog = new Dialog(context);
dialog.setContentView(R.layout.date_picker_dialog);
fromDateText = dialog.findViewById(R.id.from_date);
toDateText = dialog.findViewById(R.id.to_date);
fromDateText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
datePicker(context);
}
});
toDateText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
datePicker(context);
}
});
dialog.show();
fromDateText.setText(fromDate);
toDateText.setText(toDate);
}
public static void datePicker(Context context){
fromDatePicker = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth
,fromDateListner, fromDay, fromMonth, fromYear);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
fromDatePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
fromDatePicker.show();
fromDateListner = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month+=1;
fromDate = dayOfMonth+"/"+month+"/"+year;
setDate();
}
};
}
private static void setDate() {
try {
dateFrom = simpleDateFormat.parse(fromDate);
dateTo = simpleDateFormat.parse(toDate);
} catch (ParseException e) {
e.printStackTrace();
}
fromDateText.setText(dateFrom.toString());
toDateText.setText(dateTo.toString());
}
fromDateListner is initialized after the dialog creation, so the first time the DatePickerDialog is created without listener.
Move the fromDateListner = new DatePickerDialog.OnDateSetListener() ... part before fromDatePicker = new DatePickerDialog(context ...
Can you try like this?
private static void setDate() {
try {
dateFrom = simpleDateFormat.parse(fromDate);
dateTo = simpleDateFormat.parse(toDate);
fromDateText.setText(dateFrom.toString());
toDateText.setText(dateTo.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
// Edit:
Check my code:
private void showDatePicker() {
final Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
setDate(myCalendar.getTime());
}
};
if (getActivity() != null) {
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
private void setDate(Date time) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
editText.setText(sdf.format(time));
}
Try this code ..
take boolean variable for selected textview ...
change method like this way..
public class DialogActiivty extends AppCompatActivity {
private TextView fromDateText,toDateText;
private String fromDate,toDate;
private Dialog dialog;
private DatePickerDialog fromDatePicker;
private SimpleDateFormat simpleDateFormat;
private Calendar calendar;
private int year, month, day;
private boolean fromSelected=false,toSelected=true;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datePickerDialog(DialogActiivty.this);
}
public void datePickerDialog(final Context context) {
calendar=Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
dialog = new Dialog(context);
dialog.setContentView(R.layout.date_picker_dialog);
fromDateText = dialog.findViewById(R.id.from_date);
toDateText = dialog.findViewById(R.id.to_date);
fromDateText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toSelected=false;
fromSelected=true;
datePicker(context);
}
});
toDateText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fromSelected=false;
toSelected=true;
datePicker(context);
}
});
dialog.show();
// fromDateText.setText(fromDate);
// toDateText.setText(toDate);
}
public void datePicker(Context context){
fromDatePicker = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth
,fromDateListner, year, month, day);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
fromDatePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
fromDatePicker.show();
}
DatePickerDialog.OnDateSetListener fromDateListner = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month+=1;
fromDate = dayOfMonth+"/"+month+"/"+year;
setDate(fromDate);
}
};
private void setDate(String fromDate) {
if (fromSelected) {
fromDateText.setText(fromDate);
}
if (toSelected) {
toDateText.setText(fromDate);
}
}
}

Android Java show error message when user selects date before today

Below is my code of Datepicker allowing user to select dates and updates the textview with the selected date. How do I update the textview with an error message when user selects the date before today?
public void startCalender() {
txtTrigger = (TextView) findViewById(R.id.calTrigger);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.DAY_OF_MONTH, day);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.YEAR, year);
updateLabel();
}
};
txtTrigger.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(DisplayCeeAct.this, date,
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabel() {
String no = "<font color='red'>NO</font>.";
String myFormat = "dd/MM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());
if (myCalendar.after(myCalendar.getTime())){
txtTrigger.setText(sdf.format(myCalendar.getTime()));
}
else {
txtTrigger.setText(Html.fromHtml(no), TextView.BufferType.SPANNABLE);
}
}
Seems this line is crashing:
txtTrigger.setText(Html.fromHtml(no), TextView.BufferType.SPANNABLE);
A better approach would be:
txtTrigger.setText("NO");
txtTrigger.setTextColor(Color.RED);

How can show just the date and not the time in android studio

#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year, month, day, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year,
month, day).getTime();
sendResult(Activity.RESULT_OK, date);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
I feel like getTime is what
My problem is, but I cant fix it
I just want to display the date
and not the time at all but it wont let me. It always comes back as MM DD nn hhmmss zzz. All I want is the time to be displayed. here is where I want to display it
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime, parent, false));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
mTitleTextView = (TextView) itemView.findViewById
(R.id.crime_tile);
mDateTextView = (TextView) itemView.findViewById
(R.id.crime_date);
}
public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
}
In my list view here it always shows the format with the time in it and I just want the day, month, day of month, and year. Not sure if this makes sense, but does anyone have any ideas?
This is how I handle dates:
public static String DATE_FORMAT_NOW="MMM d yyyy"
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
final Calendar cal = Calendar.getInstance();
final String timestamp = "Last Modified: " + sdf.format(cal.getTime());
You can change the DATE_FORMAT_NOW to your needs
This particular format will put the date as: Jul 15 2017
SimpleDateFormat might have a lint warning, you can add SuppressLint to it if you want. I've never had any issue doing it this way

Categories