Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Luke Glazebrook
13,564 PointsEditing AlertDialog XML
Hey so recently I have been working on my latest app and I have made some awesome progress with it so far!
However, I have had one problem. The EditText on my AlertDialog is only supposed to accept doubles and ints. I need to edit the XML for this AlertDialog so that I can tell it to only accept numeric values. Sadly though I cannot seem to find where this XML would be and I don't know how to go about creating it myself.
Could anyone help me out with this? Thank you to anyone who takes their time to help me out. I hope you are all having great days.
-Luke
1 Answer
Daniel Hartin
18,106 PointsHi Luke
If you add the line below into the EditText item it will only allow numbers to be entered and will only display the numeric keypad on a device.
android:numeric="integer"
note: word of warning though. if you set the data type returned from this as integer bear in mind integer values only handle numbers up to 2,147,483,647 so if there is any chance the number could be larger do some error checking first before you process the number i.e store it as a string then use Integer.parseInt inside a try catch block to catch any potential errors.
Hope this helps
Daniel
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsI don't have the EditText item Daniel. Could you point me to where it is located?
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi Like,
Sorry I mistook your question and didn't explain properly.
You can set the alertdialog to have a custom layout (just like any other layout) just use the .setView method in your alertdialog declaration and set it to a view like you would any other view. That way you can declare an edit text item in your custom view and use the code I put earlier that way.
There is good documentation in the link below
http://developer.android.com/guide/topics/ui/dialogs.html
Hope this clears things up
Daniel
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsIt's fine Daniel don't worry about it!
So all I have to do is type: alert.setView() and then in my XML type <EditText />?
Thanks for the link to the documentation also.
-Luke
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi Luke
Basically yes. if alert is your AlertDialog variable just use
alert.setView(R.layout.my_dialog_layout); //or whatever layout name you decide
It will of of course take the entire screen so you may want to set it up as a fragment! (See documentation). To be honest the best thing to do is create a new layout set it to the view and try it out see what it looks like and if it suits your needs. You can be as creative as you like but it does get more challenging obviously.
My advice would be to use the sample code in the documentation to see what works for you and what doesn't if you get stuck with a particular error let us know and we can help out.
Sorry I can't be of more help but I suppose it's down to your own design now!
Hope this was of some help
Daniel
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsAlright thanks for the advice Daniel!
My AlertDialog doesn't take up the full screen it just takes up a portion of it however. I shall give it a try and report back if I have any problems with it.
Alright, I will refer to the documentation and post back onto this thread if I continue to have any problems.
Thank you for all the help thus far!
-Luke
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsAfter looking through my code I already have my alert set to a view.
I have: