hello and welcome to another Java and Android Studio tutorial my name is cal and in today's
video we're going to be creating a simple number picker in the first half of the
video we're going to use a number picker and populate it with some numbers sort of
the most basic and simple way should only take a couple of minutes
and then in the second half of the video we're going to populate our number pickup but with some
text in this example i'm going to use languages yeah number pickers they're super useful great way
to get user input from a user if you've only got a short list of things that you want to select from
and yeah it's a really easy way really easy component to implement
cool so just starting today's tutorial i'm just going to create a new android studio project
empty activity and i'm going to call this number picker tutorial cool and with our layout i'm just
going to keep using the constraint layout i'm just going to move the text view up a little bit
and change constraint vertical bias to 0.1 and i'm just calling the text number picker example and
i'm going to give it an id textview number picker and just making the text size a little bit bigger
and black next down below i'm going to add in our number picker going to give it an id so calling
it number picker and i'm just going to drag that below our text view and then i'm going to add in
this constraint top to bottom of and then give it our text view and then next we're just going to
copy and paste the constraint left of and right off equal to parent from our text view and put
that into our number picker cool so that was our very simple number picker layout all finished next
we're going to do is head into the main activity i'm just going to declare both the text view
and the number picker so just calling private text view and calling it text view and private
number picker and calling a number picker and then i'm just going to find both of those components by
their id so just casting text view to text view and i'm going to cast the number picker to number
picker and then find view by id and then it's those ids we gave earlier so text view number
picker and number picker and i'll just give a quick example of how you can use number pickers
with numbers so just calling number picker dot set max value to 10 and set min value to zero so you
can select anywhere between zero and ten as your range and then i'm going to say number picker dot
set value which is five so the initial value that it is so it's going to when it first loads it's
going to be up five and then i'm gonna say number picker dot set on value change listener and then
i'm going to say new on value change listener hit enter and we'll get that override method
and then in our on value change method i'm actually just going to change these in so the
first one is actually old value and the second one is new value and then i'm just going to
call textview.settext and then i'm going to say selected plus a little bit of space and new value
and if we build and run this you can see how easy it is to populate a number picker with some basic
numbers number pickers are a great way to get input from the user without needing to do any
other validation we can also use number pickers to display text have the user select from some
predetermined values so in this example i'm going to use languages i'm going to have it so you can
select from languages so i'm just going to create a new java class i'm going to call this language
making our language class public we're going to have an int id and a string name as our two
fields just want to right click and say generate constructor and then highlight both fields and
hit ok and then we're going to do the same thing so right click generate and this time getter and
setup for both fields next what we're going to do is declare a static array list so just something
to hold all of our languages in so i'm just calling this language arraylist and then we want
to create a static method called init languages we just want to create all of our language objects so
the first one is english new language and then giving an id of zero and the name is english
and then i'm just going to add that into our language arraylist and then do the same thing for
french and japanese and just changing the ids and the names cool so we also need a getter
for our language arraylist so i'm just going to right click and generate and then we're
going to create one more method this is again a static method and it's going to return all of our
language names in a string array and so i'm just going to create a new string array call it names
and then say new string array of the same size as our language array list using a for loop we're
just going to loop through our language array list and add each one of the names to our names array
and simply just return that names array cool so we came back into the main activity now and
we're going to get rid of this set value at the bottom here and we're just going to say language
init languages and then we're going to say set max value to language.getarraylist.size -1 set
displayed values to language.languagenames and then in our textview.settext we're going to say
language.getlanguagearraylist.getnewvalue.getname so basically what we're saying here is get our
language name based on the position of the number picker cool and just a little bit of tidying up
back in our language i forgot to initialize our arraylist so i'm just saying language arraylist is
equal to new arraylist and then i'm actually going to make the number picker layout with a little bit
wider just so it looks a little nicer but yeah if we build our app again you can see that when you
scroll through you select any of our languages and it puts it into our selected so in summary we've
learned how to use a number picker and populate our number picker with numbers and we've also
been able to populate our number picker with some text consider subscribing if you'd like to see
more android studio and app development tutorials but otherwise i'll catch you guys in the next one
you