I thought this was pretty cool…I used the AlertDialog.Builder, an ArrayAdapter along with the View Holder Android UI pattern to build an AlertDialog containing a list of choices with images for each entry.
Here’s a screen shot from the emulator of my dog/cat/bird selection AlertDialog.
By using a simple TextView as the widget in the animal_row.xml layout file for the ArrayAdapter the image comes for free as the compound drawable. Each row in the selection list is defined by a POJO holding the string to display, the image and the value to get when if the selection is chosen. Hopefully the rest of the code is documented well enough to understand. I would change the code a bit for performance by defining the AlertDialog in onCreate() then calling dialog.show() when the button is pressed.
public class HelloWorld extends Activity { private static final String TAG = "HelloWorld"; Animal[] _animals; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); loadAnimals(); } /** * Create all the choices for the list */ private void loadAnimals() { _animals = new Animal[3]; // define the display string, the image, and the value to use // when the choice is selected _animals[0] = new Animal( getString( R.string.dog ), getImg( R.drawable.dog ), getString( R.string.dog_val ) ); _animals[1] = new Animal( getString( R.string.cat ), getImg( R.drawable.cat ), getString( R.string.cat_val ) ); _animals[2] = new Animal( getString( R.string.bird ), getImg( R.drawable.bird ), getString( R.string.bird_val ) ); } private Drawable getImg( int res ) { Drawable img = getResources().getDrawable( res ); img.setBounds( 0, 0, 48, 48 ); return img; } /** * Handle the event to display the AlertDialog with the list */ public void handlePush( View target ) { // define the list adapter with the choices ListAdapter adapter = new AnimalAdapter( this, _animals ); final AlertDialog.Builder ad = new AlertDialog.Builder(this); // define the alert dialog with the choices and the action to take // when one of the choices is selected ad.setSingleChoiceItems( adapter, -1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // a choice has been made! String selectedVal = _animals[which].getVal(); Log.d(TAG, "chosen " + selectedVal ); dialog.dismiss(); } }); ad.show(); } /** * Definition of the list adapter...uses the View Holder pattern to * optimize performance. */ static class AnimalAdapter extends ArrayAdapter{ private static final int RESOURCE = R.layout.animal_row; private LayoutInflater inflater; static class ViewHolder { TextView nameTxVw; } public AnimalAdapter(Context context, Animal[] objects) { super(context, RESOURCE, objects); inflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if ( convertView == null ) { // inflate a new view and setup the view holder for future use convertView = inflater.inflate( RESOURCE, null ); holder = new ViewHolder(); holder.nameTxVw = (TextView) convertView.findViewById(R.id.animal_name_txtvw); convertView.setTag( holder ); } else { // view already defined, retrieve view holder holder = (ViewHolder) convertView.getTag(); } Animal cat = getItem( position ); if ( cat == null ) { Log.e( TAG, “Invalid category for position: ” + position ); } holder.nameTxVw.setText( cat.getName() ); holder.nameTxVw.setCompoundDrawables( cat.getImg(), null, null, null ); return convertView; } } /** * POJO for holding each list choice * */ class Animal { private String _name; private Drawable _img; private String _val; public Animal( String name, Drawable img, String val ) { _name = name; _img = img; _val = val; } public String getName() { return _name; } public Drawable getImg() { return _img; } public String getVal() { return _val; } } }
dog icon from Mac OS X iChat icons
Very useful for me. thanks you
ReplyDeleteNice Blog..Keep UPdating..
ReplyDeleteAndroid Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training
This comment has been removed by the author.
ReplyDeleteGreat post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
blueprism-training-in-pune
automation-anywhere-training-in-pune
Read all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeletejava training in marathahalli | java training in btm layout
java training in jayanagar | java training in electronic city
java training in chennai | java training in USA
selenium training in chennai
Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
ReplyDeletepython training in tambaram
python training in annanagar
python training in OMR
python training in chennai
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Blueprism training in Pune
I recently came across your blog and have been reading along. I thought I would leave my first comment.
ReplyDeletepython training in rajajinagar | Python training in btm | Python training in usa
Thanks for taking time to share this valuable information admin. Really informative, keep sharing like this.
ReplyDeleteAWS Training in Chennai
DevOps Training in Chennai
Data Science Course in Chennai
ccna course in Chennai
Python Training in Chennai
R Programming Training in Chennai
Angularjs Training in Chennai
RPA Training in Chennai
Blue Prism Training in Chennai
Your site is amazing and your blogs are informative. I have bookmarked this blog for my reference.
ReplyDeleteAutomation Anywhere Training in Chennai
Automation Training Institute in Chennai
Automation Anywhere course in Chennai
RPA course in Chennai
Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
I have read your blog its very attractive and impressive. I like your blog keep it up.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome. I will instantly grab your rss feed to stay informed of any updates you make and as well take the advantage to share some latest information about
ReplyDeleteCREDIT CARD HACK SOFTWARE which many are not yet informed of, the recent technology.
Thank so much for the great job.
Learn Amazon Web Services for making your career as a shining sun with Infycle Technologies. Infycle Technologies is the best AWS training centre in Chennai, providing complete hands-on practical training of professional specialists in the field. In addition to that, it also offers numerous programming language tutors in the software industry such as Python, AWS, Hadoop, etc. Once after the training, interviews will be arranged for the candidates, so that, they can set their career without any struggle. Of all that, 200% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.AWS training centre in Chennai
ReplyDeleteAre you looking for Big Data training in Chennai with placement opportunities? Then we, Infycle Technologies are with you to make your dream into reality. Infycle Technologies is one of the best Big Data Training Institute in Chennai, which offers various programs along with Big Data such as Oracle, Java, AWS, Hadoop, etc., in complete hands-on practical training with trainers, those are specialists in the field. In addition to the training, the mock interviews will be arranged for the candidates, so that they can face the interviews with the best knowledge. Of all that, 100% placement assurance will be given here. To have the words above in the real world, call 7502633633 to Infycle Technologies and grab a free demo to know more.
ReplyDeletehttps://infycletechnologies.com/big-data-training-in-chennai/
Grab Big Data Course in Chennai for making your career as a shining sun with Infycle Technologies. Infycle Technologies is the best software training center in Chennai, providing complete hands-on practical training of professional specialists in the field. It also offers numerous courses in the IT industry such as Oracle, Java, Python, AWS, Hadoop, etc. Once after the training, interviews will be arranged for the candidates, so that, they can set their career without any struggle. Of all that, 200% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.
ReplyDeletehttps://infycletechnologies.com/big-data-training-in-chennai/
If AWS is a job that you're dreaming of, then we, Infycle are with you to make your dream into reality. Infycle Technologies offers the best AWS Training in Chennai, with various levels of highly demanded software courses such as Oracle, Java, Python, Hadoop, Big Data, etc., in 100% hands-on practical training with specialized tutors in the field. Along with that, the pre-interviews will be given for the candidates, so that, they can face the interviews with complete knowledge. To know more, dial 7502633633 for more.
ReplyDeleteIf AWS is a job that you're dreaming of, then we, Infycle are with you to make your dream into reality. Infycle Technologies offers the best AWS Training in Chennai, with various levels of highly demanded software courses such as Oracle, Java, Python, Hadoop, Big Data, etc., in 100% hands-on practical training with specialized tutors in the field. Along with that, the pre-interviews will be given for the candidates, so that, they can face the interviews with complete knowledge. To know more, dial 7502633633 for more.
ReplyDeletebest python course in Chennai
Python Training in Chennai | Infycle Technologies
ReplyDeleteIf Python is a work you've always wanted, we at Infycle are here to help you make it a reality. Infycle Technologies provides Python Training in Chennai, with various levels of highly sought-after software courses such as Oracle, Java, Python, Big Data, and others, delivered through 100% hands-on practical training with industry experts. In addition, mock interviews will be conducted. For more details contact 7502633633 to grab a free demo.
best training center in Chennai
If you are dreaming of an IT job !!! Then AWS Course in Chennai!!Is the best choice for you. Yes, what you heard is Right Infycle offering you an AWS course for an Affordable price with experienced trainees, Practical Classes, Flexible timing, and more.
ReplyDeleteIt is extremely nice to see the greatest details presented in an easy and understanding manner.
ReplyDeletefull stack web development course malaysia
MMORPG OYUNLARI
ReplyDeleteinstagram takipçi satın al
Tiktok Jeton Hilesi
tiktok jeton hilesi
antalya saç ekimi
instagram takipçi satın al
instagram takipci satin al
mt2 pvp
instagram takipçi satın al
perde modelleri
ReplyDeletesms onay
Mobil Ödeme Bozdurma
Nft nasil alınır
Ankara evden eve nakliyat
trafik sigortası
dedektör
KURMA.WEBSİTE
aşk kitapları
SMM PANEL
ReplyDeleteSmm panel
iş ilanları
İnstagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
SERVİS
Jeton Hilesi
en son çıkan perde modelleri
ReplyDeleteuc satın al
nft nasıl alınır
en son çıkan perde modelleri
lisans satın al
özel ambulans
minecraft premium
yurtdışı kargo