android - Custom Dialog Boxes to show image, text and setTitle -
i have little glitch in program i've been trying figure out i'm missing. in .java file, have gridview of images setonitemsclicklistener on items in gridview. have 2 xml files, 1 .java , other set contentview popup window on click on of gridview items. want whenever user clicks on item in gridview, custom dialog box pops up, show image clicked, plus brief description on image.
so far, dialog pops out quite alright, no details passed unto dialog box.
this activity_first.xml code:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" > <gridview android:id="@+id/gridview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numcolumns="3" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:columnwidth="90dp" android:gravity="center" > </gridview> </linearlayout>
this activity_custom_dialog.xml file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background"> <tablelayout android:id="@+id/tablelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 2 columns --> <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="5dp" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" /> </tablerow> <!-- button span 2 column --> <tablerow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <button android:id="@+id/button1" android:text="ok" /> </tablerow> </tablelayout> </linearlayout>
finally, how image.java file looks like:
package com.example.custom; import android.app.activity; import android.app.dialog; import android.content.context; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.baseadapter; import android.widget.button; import android.widget.gridview; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class imageactivity extends activity { // images display integer[] imageids = { r.drawable.firefighter, r.drawable.hydrant, r.drawable.fire, r.drawable.hose, r.drawable.truck, r.drawable.ladder, r.drawable.safety_clothing, r.drawable.gloves, }; /** * oncreate * * called when activity first created. * should of normal static set up: create views, bind data lists, etc. * method provides bundle containing activity's frozen state, if there one. * * followed onstart(). * * @param savedinstancestate bundle */ protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview (r.layout.activity_first); settitlefromactivitylabel (r.id.title_text); gridview gridview = (gridview) findviewbyid(r.id.gridview1); gridview.setadapter(new imageadapter(this)); gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { final dialog dialog = new dialog(context); dialog.setcontentview(r.layout.activity_custom_dialog); switch(v.getid()) { case r.drawable.firefighter: dialog.settitle("firefighter"); textview text = (textview) dialog.findviewbyid(r.id.textview1); text.settext("to insert text..."); imageview image = (imageview) dialog.findviewbyid(r.id.imageview1); image.setimageresource(r.drawable.firefighter); break; case r.drawable.hydrant: dialog.settitle("hydrant"); textview text1 = (textview) dialog.findviewbyid(r.id.textview1); text1.settext("to insert text..."); imageview image1 = (imageview) dialog.findviewbyid(r.id.imageview1); image1.setimageresource(r.drawable.hydrant); break; case r.drawable.fire: dialog.settitle("fire"); textview text2 = (textview) dialog.findviewbyid(r.id.textview1); text2.settext("to insert text..."); imageview image2 = (imageview) dialog.findviewbyid(r.id.imageview1); image2.setimageresource(r.drawable.fire); break; case r.drawable.hose: dialog.settitle("hose"); textview text3 = (textview) dialog.findviewbyid(r.id.textview1); text3.settext("to insert text..."); imageview image3 = (imageview) dialog.findviewbyid(r.id.imageview1); image3.setimageresource(r.drawable.hose); break; case r.drawable.truck: dialog.settitle("truck"); textview text4 = (textview) dialog.findviewbyid(r.id.textview1); text4.settext("to insert text..."); imageview image4 = (imageview) dialog.findviewbyid(r.id.imageview1); image4.setimageresource(r.drawable.truck); break; case r.drawable.ladder: dialog.settitle("ladder"); textview text5 = (textview) dialog.findviewbyid(r.id.textview1); text5.settext("to insert text..."); imageview image5 = (imageview) dialog.findviewbyid(r.id.imageview1); image5.setimageresource(r.drawable.ladder); break; case r.drawable.gloves: dialog.settitle("gloves"); textview text6 = (textview) dialog.findviewbyid(r.id.textview1); text6.settext("to insert text..."); imageview image6 = (imageview) dialog.findviewbyid(r.id.imageview1); image6.setimageresource(r.drawable.gloves); break; } // set custom dialog components - text, image , button button dialogbutton = (button) dialog.findviewbyid(r.id.button1); // if button clicked, close custom dialog dialogbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { dialog.dismiss(); } }); dialog.show(); /* toast.maketext(getbasecontext(), "pic "+(position+1)+ " selected", toast.length_short).show(); */ } }); } public class imageadapter extends baseadapter { private context context; public imageadapter(context c) { context = c; } // returns number of images public int getcount(){ return imageids.length; } // returns id of item public object getitem(int position) { return position; } // returns id of item public long getitemid(int position){ return position; } // returns imageview view public view getview(int position, view convertview, viewgroup parent){ imageview imageview; if(convertview == null){ imageview = new imageview(context); imageview.setlayoutparams(new gridview.layoutparams(100,100)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(5, 5, 5, 5); } else{ imageview = (imageview) convertview; } imageview.setimageresource(imageids[position]); return imageview; } } } // end class
i'd appreciate should guide asap on how around lil glitch lil programmer eyes cannot see. :-)
thanks in advance.
Comments
Post a Comment