Android Gallery display selected image -
Android Gallery display selected image -
i next code trying display image selected in gallery.but default android image , not image saved.what doing wrong how resolve this..
package hgallery.com; import hgallery.com.r; import hgallery.com.hgalleryactivity.imageadapter; import android.app.activity; import android.os.bundle; import android.content.context; import android.content.res.typedarray; import android.graphics.drawable.drawable; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.adapterview.onitemselectedlistener; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; import android.widget.toast; public class hgalleryactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final imageview iv=(imageview) findviewbyid(r.id.imageview1); iv.setvisibility(view.invisible); gallery g = (gallery) findviewbyid(r.id.gallery); g.setadapter(new imageadapter(this)); g.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview parent, view v, int position, long id) { //toast.maketext(hgalleryactivity.this, "here", toast.length_short).show(); //toast.maketext(hgalleryactivity.this, "" + position, toast.length_short).show(); iv.setvisibility(view.visible); drawable d=getresources().getdrawable(r.drawable.icon); //toast.maketext(hgalleryactivity.this, ""+d, toast.length_short).show(); iv.setimagedrawable(d); } }); } public class imageadapter extends baseadapter { int mgalleryitembackground; private context mcontext; private integer[] mimageids = { r.drawable.sample_1, r.drawable.sample_2, r.drawable.sample_3 }; public imageadapter(context c) { mcontext = c; typedarray = obtainstyledattributes(r.styleable.hellogallery); mgalleryitembackground = a.getresourceid( r.styleable.hellogallery_android_galleryitembackground, 0); a.recycle(); } public int getcount() { homecoming mimageids.length; } public object getitem(int position) { homecoming position; } public long getitemid(int position) { homecoming position; } public view getview(int position, view convertview, viewgroup parent) { imageview = new imageview(mcontext); i.setimageresource(mimageids[position]); i.setlayoutparams(new gallery.layoutparams(150, 100)); i.setscaletype(imageview.scaletype.fit_xy); i.setbackgroundresource(mgalleryitembackground); homecoming i; } /* public view getview(int arg0, view arg1, viewgroup arg2) { // todo auto-generated method stub homecoming null; }*/ } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <imageview android:id="@+id/imageview1" android:layout_width="320px" android:layout_height="250px"></imageview> <!-- <imageview android:layout_width="wrap_content" android:id="@+id/imageview2" android:layout_height="wrap_content" android:src="@drawable/icon"></imageview>-->
the method within imageadapter called public object getitem(int position)
should modified homecoming appropriate mimageids[]
using position
argument. then, within onitemclick
, can phone call parent.getitematposition(position)
resource id.
if want utilize method things other ids, can write own method (which i'd phone call getresourceid), , phone call ((imageadapter)parent.getadapter()).getresourceid(position))
.
android android-layout
Comments
Post a Comment