HomeОбразованиеRelated VideosMore From: Coding Demos

Android spinner in custom dialog

126 ratings | 17698 views
In this tutorial you will learn how to customize Android AlertDialog by adding Android spinner inside the AlertDialog. We will start first by defining a button, then will make it clickable by using Android OnClicklistener, inside the button OnClicklistener we will define an AlertDialog.Builder and then will create a separate Android resource file that will hold Android spinner. Last thing is we set custom layout as the layout for Android dialog and finally we show Android AlertDialog. Social media: ▶Twitter: https://twitter.com/codingdemos
Html code for embedding videos on your blog
Text Comments (34)
Mostafa Abd Elkader (1 month ago)
What abbout if I want to do this with Kotlin
Rijal Khalik (2 months ago)
Why i get some error in line mSpinner.setAdapter(adapter); ??
Kainat Nawaz (5 months ago)
B Wally (5 months ago)
Key statement never forget : - setDrop - if (mspinner.getSelectedItem.toString().equalsIgnoreCase() { ....} - mBuilder.setView(mView) - mBuilder.create()
CHANDRAN (7 months ago)
Very Nice. How to validate a spinner
Coding Demos (7 months ago)
Hi, you can email ([email protected]) me your code.
Faqih Abdillah (7 months ago)
hi sir i'm a new creator in android studio, but i have some problem there.. that's very hard to me because i need to change or switch the activity after choose and click yes/ok, can you help me sir ? so it is not just make some toast, but for switch the activity too.. thanks sir
Coding Demos (7 months ago)
Hi, just add the following code inside the button OnClick method: startActivity(new Intent(MainActivity.this, NewActivity.class));
Mohamed Kouyate (8 months ago)
good video
Coding Demos (8 months ago)
Happy coding :D
Sagar Verma (8 months ago)
i am having error in "mspinner" and "DialogInterface" ,i am using android studio version 3.0.1.
Coding Demos (8 months ago)
Hi, what is the error that you see?
박인규 (9 months ago)
Really helpful to me. thanks.
Coding Demos (9 months ago)
Your welcome :)
abdelmouttalib charkaoui (10 months ago)
You saved my life thanks
Coding Demos (9 months ago)
Your welcome, I'm happy to help :)
How to position text so it does not go over the background. https://i.stack.imgur.com/YlGPO.png
Coding Demos (1 year ago)
You can add this line to position it below the background: android:layout_below="@+id/background"
Mark Viana (1 year ago)
Thanks for the tutorial! I've learned a lot... Is there any way to change the color of the spinner text? For example: Macdonald --> red color and Burger King in green color.
Stan T. (1 year ago)
Thanks! Very clear and simple
nour khel (1 year ago)
very helpful thankyou! what do i do if i want to make more than one button that has a spinner? like two buttons both with spinners?
BRUHItsABunny (1 year ago)
Like coding demos said, but be aware if you want to add more than one of those kinda items(Spinners, Edittexts and textviews, etc etc), you might end up having to create your own layout, then add in those items to that kayout, and then add the layout to the dialog eg: LiniarLayout layout = new LiniarLayout(v.getContext()); //create your spinners (using spinner_1 and spinner_2 as example) layout.setView(spinner_1); layout.setView(spinner_2); //Assuming you called your dialog dialog_1 dialog_1.setView(layout); Else only one spinner will be shown
Coding Demos (1 year ago)
Hi, you can add another button and follow the steps that i showed in the video. I want to ask you is the second spinner that you want to add related to the first spinner? If they are, then i will suggest you to add 2 spinner in one dialog instead of having 2 dialogs with 2 spinners. For example: You have 1 spinner that contain list of restaurant inside a dialog and another spinner that contain list of dishes inside another dialog..that will make it kind of complicated for the user to make a choice, instead you can just add those 2 spinner under one dialog which will make it very easy for the user and less troublesome. Happy coding :)
mohammad syirhan (1 year ago)
can you show how can go to new page when i click the item on the spinner?
Coding Demos (1 year ago)
Hi, You can do it like this: mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if(!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a restaurant…")) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); dialogInterface.dismiss(); } } }); Happy coding :)
Giulio Montenero (1 year ago)
Nice tutorial. Thank you!
Giulio Montenero (1 year ago)
If I wanted to add dynamically other components to this custom dialog/layout, how could I do that? Hope you can help me!
Coding Demos (1 year ago)
Your welcome :)
Aerol Lozada (1 year ago)
How do you display the chosen value into a textview instead of a toast?
Coding Demos (1 year ago)
Your welcome :)
Aerol Lozada (1 year ago)
Thank you!
Coding Demos (1 year ago)
Hi, so let's say we want to show the chosen value in a textview inside the dialog..here are the steps: - Open dialog_spinner.xml file and create a simple textview like this: <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content"/> - Open MainActivity.java file and define the textview inside onClick method like this: TextView mValue = (TextView)mView.findViewById(R.id.textview); - Inside mBuilder.setPositiveButton onClick method you set the chosen value inside the new textview that we've just created like this: mValue.setText(mSpinner.getSelectedItem().toString()); - Don't forget to remove dialogInterface.dismiss() from mBuilder.setPositiveButton onClick method to be able to see the chosen value, otherwise the dialog will be dismissed and you won't be able to see the chosen value That's it..i hope that answer your question, please do let me know if you need more help. Thanks and happy coding :)
vadluri vivek (1 year ago)
Nice dude! keep up the good work
Coding Demos (1 year ago)
Thanks for watching :)

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.