Pop Up Date Picker Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another java and android  studio tutorial my name is cal and in this   tutorial we're going to be having a look at date  pickers we're just going to create a very simple   pop-up date picker so basically we're going to  have a button when you click on the button it's   going to show up then alert dialog with the  date picker in it the user then selects the   date and then we put that date back into the  buttons text really simple shouldn't take too   long there's a link in the description to the  source code with all that said let's get started getting started i'm just going to create a  new android studio project empty activity i'm   going to call this date picker tutorial and our  programming language is java and hit finish to   create our new android studio project next i'm  going to head into our themes and just delete   all of the values just because that's a new  android studio thing that they've put in there   and just keeping this as simple as possible i'm  going to remove all that heading back into the   main xml i'm just going to add in an id to our  textview changing the text to date and the text   size to 30sp the text color is black and i'm going  to remove that top constraint there so that the   text view aligns at the top and then just say  margin top 30dp and i'm actually going to change   that to select date it's a bit nice a bit of  text there and then i'm going to add in a button   i'm going to give it a width of 250dp as well as  height wrap content and then i'm going to give it   a text of January 1st 2020 text size 30 and color  black as well and then i'm just going to say style   and then i'm going to give it a question mark and  say android spinner style and then i'm going to   copy down those two constraints to the left option  to the right of parent just so it centers in the   view and then i'm also going to give it the  constraint top to bottom of our text view   as well as margin top 10 dp cool and finally  we're just going to give our button an id   calling it date pick a button as well as an on  click listener so just saying on click say open   date picker click on the error message and create  that in the main activity cool and then heading   up to the top of our main activity we're going  to declare a date picker dialog and i'm just   naming this date picker dialog as well as a button  just going to call this date button then in our   own create method we're going to create another  method called init date picker below that method   we're just going to say date button is equal  to find view by id and then r dot id dot date   picker button which is the same id that we gave  in the xml and then in our init date pickup method   we're just going to say date picker dialog dot on  date set listener date set listener is equal to   new date picker dialog on date set listener  hit enter to implement this override method   on date set and then we're just going to change  those in so they're actually year month and day   and then it's got a funny little thing where  the month is actually zero so January is zero   so we're just going to say month is equal to  month plus one so January is equal to one string   date is equal to make date string and then create  a new method called make date string which takes   three ins day month and year and then i'm just  going to return month with a bit of space plus day   with a little bit of space plus the year and we  could just leave it as is and you get an all-digit   date format but in my experience in software  development it's worth using this state format   with the string like so jan feb march because most  people around the world can read and understand   and are happy with this date format if you start  doing the digit one then you know some countries   will say oh no you put the month first and some  countries will say you know you put the day first   and i'm sure there's a country out there that  says no you should put the year first either way   if you use this date format generally speaking  people are pretty happy and then just returning   jan as a default the method shouldn't really  ever reach here though so you could probably   put in whatever you like cool so now we've got  our nicely formatted date string we're just going   to say datebutton.settext with our date and then  below that i'm just going to say calendar cal is   equal to calendar dot get instance and i'm going  to say into year is equal to cal dot get calendar   year and then copy and paste that down twice more  and get month and day so what we're doing here is   just getting today's date and we're going to use  today's date as our default selected date for our   date picker next we're going to declare one more  int calling it style and i'm going to say style   is equal to alert dialog dot theme holo light so  you could pretty much choose whatever style you   like here but i like to look at this one next date  picker dialog is equal to new date pick a dialogue   and giving a context of this given then giving  it our style our date set listener year month and   day now that we've initialized our date picker  dialogue we just need to go into our open date   picker method and just say datepickerdialog.show  and then finally we're just going to set the date   button to show today's date by default so i'm just  going to say date button dot set text is equal   to get today's date so just creating a new method  which returns a string and then i'm going to copy   what we did for getting today's date from the  calendar and then just giving it month is equal   to month plus one because of that funny little  month nuance and then set return make date string   with our day month and year cool and if we build  and run this now we've got a pretty basic pop-up   date picker so just selecting say December  15th and it's populated that in our date button   and then let's pick a future date so boxing day  December 26 2020. one other thing i will show you   if you want to set the maximum date so you can't  pick any dates into the future and date pick a   dialogue we're just going to call get date picker  and then set max date giving it system . current   time and build and run this once more you can see  our popup date picker you can't select any dates   into the future you could do the same thing for  min date so set min date so say you only wanted   to pick dates into the future and nothing behind  it's another nice way you can just change set max   date to set min date cool and that wraps up our  date picker tutorial in android studio using java   if you like the video give it a like otherwise  i will catch you guys in the next tutorial you
Info
Channel: Code With Cal
Views: 15,772
Rating: 4.909091 out of 5
Keywords: java, tutorial, programming, coding, example, android, studio, program, code, app, development, developer, software, indie, how, to, date, picker, date picker, build, create, learn, dialog, date picker, month, year, day, pop, up, button, alert, datepicker, activity, project, themes, simple, as, possible, text, view, constraint, layout, xml, style, spinnerstyle, format, dateformat, select, datepickerdialog, on, set, listener, formatting, Calendar, cal, with, spinner, datespinner, open, datepicekr, min, max, system, months, years, days, pop-up, pop up
Id: qCoidM98zNk
Channel Id: undefined
Length: 5min 39sec (339 seconds)
Published: Sat Dec 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.