Theory
date field જે 32-13-2026 ને accept કર્યું
FestConnect Mobile ની booking screen એ visit date ને plain EditText માં માંગ્યું. harvest, day 1: 32/13/2026, kal, Garba day, અને એક emoji.
તમે BCA404 ના MaskedTextBox philosophy થી cure ને જાણો છો: make invalid input impossible to type. dates અને times માટે Android નું version એ છે કે users ને type કરવા દેવું નહીં: calendar pops થાય છે, તેઓ date ને TAP કરે છે, અને ફક્ત real dates exist કરે છે. meantime save જે follow થાય છે તેને ત્રીજું widget જોઈએ છે: waiting વિશે કંઈક honest.
Theory
DatePickerDialog: box માં calendar
standard pattern: date field પર tap એ DatePickerDialog ને open કરે છે:
new DatePickerDialog(this, listener, year, month, day).show();
- last 3 arguments preselect કરે છે date, usually today નું, fetched from
Calendar.getInstance() - listener (OnDateSetListener) એ callback છે જે choice ને receive કરે છે:
(view, year, month, dayOfMonth)
અને અહીં stands કરે છે Android નો most famous small trap: month 0-based છે: January 0 છે, December 11. Chosen 15 March arrive થાય છે month = 2 તરીકે; તેને display કરો month + 1 તરીકે.
Practical
Pick date, pick time, show both
public class BookingActivity extends AppCompatActivity {
void pickDateTime() {
Calendar now = Calendar.getInstance();
new DatePickerDialog(this,
(view, year, month, day) -> {
// month 0-BASED છે: March 2 તરીકે arrive થાય છે
txtDate.setText(day + "/" + (month + 1) + "/" + year);
},
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)).show();
new TimePickerDialog(this,
(view, hour, minute) ->
txtTime.setText(String.format("%02d:%02d", hour, minute)),
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
true).show(); // true = 24-hour clock
}
}
Theory
ProgressBar: honest waiting
Booking saved to server 2 seconds લે છે: 2 seconds જેમાં silent screen frozen લાગે છે. ProgressBar patience ને buy કરે છે 2 styles માં:
- indeterminate (default): spinner: "working, duration unknown"
- determinate: measurable work માટે filling bar: XML માં
style="?android:attr/progressBarStyleHorizontal"set કરો, પછીsetMax(100)અનેsetProgress(n)જેમ work advance થાય
usual choreography: slow work પહેલાં progressBar.setVisibility(View.VISIBLE), તેના completion callback માં View.GONE: same async bookkeeping જેમ આ year નો દરેક callback.
Quiz
user DatePickerDialog માં 15 March 2026 ને pick કરે છે. OnDateSetListener શું receive કરે છે (year, month, dayOfMonth) તરીકે?
- (2026, 3, 15)
- (2026, 2, 15): month 0-based છે, તેથી March 2 છે
- (2026, 3, 14): days 0-based છે
- એક single Date object, 3 numbers નહીં
Show the answer
(2026, 2, 15): month 0-based છે, તેથી March 2 છે
Months alone Calendar world માં 0-based છે (old java.util.Calendar ની inheritance): January 0, March 2, December 11: જ્યારે year અને dayOfMonth humans expect કરે તેમ arrive થાય છે. દરેક Android developer એ off-by-one-month bug ને once ship કર્યું છે; exams make sure કરે છે કે તમે તેને meet કર્યું હોય production પહેલાં. Option C symmetry ને invent કરે છે જે exist નથી કરતી: days 1-based છે. Option D later Java ના nicer APIs ને describe કરે છે; આ listener તમને 3 ints ને hands કરે છે, અને display માટે +1 એ તમારું job છે.
Think first
કયું ProgressBar style, અને GONE ક્યાં જાય છે?
booking flow માં બે waits: (a) booking ને server પર save કરવું (unknowable duration), (b) તમારા fest pass ના 5 photos ને upload કરવા, એક પછી એક. દરેક માટે style ને pick કરો, અને precisely state કરો કે code માં WHERE bar ને ફરીથી hide કરવું જોઈએ, tap કરતા પહેલાં.
Show the answer
(a) indeterminate spinner: network round-trip માટે કોઈ meaningful percentage exist નથી કરતું. (b) determinate horizontal bar: setMax(5), દરેક photo પછી setProgress: real, measurable fractions. Hiding: work ના completion callback માં (success AND failure paths), ક્યારેય તેને start કર્યા પછીની line પર નહીં: async call ની પછીની code immediately runs થાય છે (lesson જે jQuery effects, AJAX અને Node બધાએ શીખવ્યું), તેથી ત્યાં hide કરવું bar ને work શરૂ થાય તે પહેલાં blank કરે છે. Async model, fourth appearance.
Watch out
Picker અને progress traps
raw month ને Displaying કરવું: March visit માટે ticket પર 15/2/2026: હંમેશા month + 1 humans માટે (અને stored text થી picker ને feed કરતી વખતે minus 1).
Midnight formatting: %02d 9:05 ને 09:05 માં pad કરે છે; તેના વગર, times broken લાગે છે.
bar જે ક્યારેય leave નથી થતો: ProgressBar ને દરેક outcome path માં hide કરો; eternal spinner hung app તરીકે reads થાય છે: users તેને kill કરે છે.
Widget vs dialog: XML <DatePicker> layout માં full calendar ને embed કરે છે; dialog on demand pops થાય છે: બંને exist કરે છે તેને know કરો, dialog ને default દ્વારા use કરો.
Theory
Constraints beat corrections
Step back અને see કરો design principle જે આ widgets share કરે છે: picker input ને CONSTRAINS કરે છે (ફક્ત real dates exist કરે છે), progress bar state ને CONFESSES કરે છે (working, frozen નહીં): બંને later scolding error messages ની need ને remove કરે છે. BCA402-02 નો UI/UX subject આ ideas ને name કરશે; તમે already તેમને practise કરી રહ્યા છો. next lesson: જ્યારે content screen ને outgrow કરે: ScrollView, અને તેનો one-child law.
Summary
Key takeaways
- DatePickerDialog(context, listener, y, m, d).show(): calendar pops થાય છે, listener choice ને receive કરે છે.
- MONTH 0-BASED છે: January 0, March 2: display month + 1. Year અને day human-normal છે.
- TimePickerDialog hour અને minute ને return કરે છે; last argument true એ 24-hour clock means કરે છે.
- Initial values Calendar.getInstance() થી આવે છે.
- ProgressBar: default દ્વારા indeterminate spinner; measurable work માટે horizontal style + setMax/setProgress.
- bar ને slow work પહેલાં show કરો, દરેક completion path માં hide કરો.
- Memory hook: pickers nonsense ને forbid કરે છે, months zero થી start થાય છે, spinners ને die થવું જોઈએ.