android - Making a clickable image to open another activity -
here practice tutorial of making gridview auto resize image. i'm adopted tutorial activity. i'm gonna next how create image clickable. when users clicked on each image goes class/activity.
here several code programme experimented :
activity_main.xml throw gridview layout,stretch mode , column width :
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <gridview android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:verticalspacing="5dp" android:horizontalspacing="5dp" android:stretchmode="columnwidth" android:numcolumns="2" /> </framelayout>
mainactivity.java custom imageview maintains aspect ratio:
public class mainactivity extends imageview { public mainactivity(context context) { super(context); } public mainactivity(context context, attributeset attrs) { super(context, attrs); } public mainactivity(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); setmeasureddimension(getmeasuredwidth(), getmeasuredwidth()); //snap width } }
this layout grid item :
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.conversa.mainactivity android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centercrop" /> <textview android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="10dp" android:paddingright="10dp" android:paddingtop="15dp" android:paddingbottom="15dp" android:layout_gravity="bottom" android:textcolor="@android:color/white" android:background="#55000000" /> </framelayout>
this adapter gridview :
public class myadapter extends baseadapter { private list<item> items = new arraylist<item>(); private layoutinflater inflater; public myadapter(context context) { inflater = layoutinflater.from(context); items.add(new item("conversation 1", r.drawable.pic1)); items.add(new item("conversation 2", r.drawable.pic2)); items.add(new item("conversation 3", r.drawable.pic3)); items.add(new item("conversation 4", r.drawable.pic4)); items.add(new item("conversation 5", r.drawable.pic5)); items.add(new item("conversation 6", r.drawable.pic6)); items.add(new item("conversation 7", r.drawable.pic7)); items.add(new item("conversation 8", r.drawable.pic8)); items.add(new item("conversation 9", r.drawable.pic9)); items.add(new item("conversation 10", r.drawable.pic10)); } @override public int getcount() { homecoming items.size(); } @override public object getitem(int i) { homecoming items.get(i); } @override public long getitemid(int i) { homecoming items.get(i).drawableid; } @override public view getview(int i, view view, viewgroup viewgroup) { view v = view; imageview picture; textview name; if(v == null) { v = inflater.inflate(r.layout.gridimage, viewgroup, false); v.settag(r.id.picture, v.findviewbyid(r.id.picture)); v.settag(r.id.text, v.findviewbyid(r.id.text)); } image = (imageview)v.gettag(r.id.picture); name = (textview)v.gettag(r.id.text); item item = (item)getitem(i); picture.setimageresource(item.drawableid); name.settext(item.name); homecoming v; } private class item { final string name; final int drawableid; item(string name, int drawableid) { this.name = name; this.drawableid = drawableid; } } }
and adapter set gridview :
public class gridpiw extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gridview = (gridview)findviewbyid(r.id.gridview); gridview.setadapter(new myadapter(this)); } }
what should add together or maybe modify image can clicked show new activity?
you should this:
firstly, create activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <gridview android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:verticalspacing="5dp" android:horizontalspacing="5dp" android:stretchmode="columnwidth" android:numcolumns="2"/> </framelayout>
secondly, alter gridpiw mainactivity classname:
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(new myadapter(this)); } }
thirdly, alter mainactivity extends imageview measuredimageview extends imageview, this:
public class measuredimageview extends imageview { public measuredimageview(context context) { super(context); } public measuredimageview(context context, attributeset attrs) { super(context, attrs); } public measuredimageview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); setmeasureddimension(getmeasuredwidth(), getmeasuredwidth()); //snap width } }
then, include grid item layout:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.conversa.measuredimageview android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:scaletype="centercrop" /> <textview android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="10dp" android:paddingright="10dp" android:paddingtop="15dp" android:paddingbottom="15dp" android:layout_gravity="bottom" android:textcolor="@android:color/white" android:background="#55000000" /> </framelayout>
note: imageview id "picture" should have attribute android:clickable="true".
then in adapter should store context, passed parameter.
public class myadapter extends baseadapter { ... private context context ... public myadapter(context context) { this.context = context; //your actions... } }
and finally, in getview() function of adapter class, should setonclicklistener imageview:
... @override public view getview(int i, view view, viewgroup viewgroup) { view v = view; imageview picture; textview name; if(v == null) { v = inflater.inflate(r.layout.gridimage, viewgroup, false); v.settag(r.id.picture, v.findviewbyid(r.id.picture)); v.settag(r.id.text, v.findviewbyid(r.id.text)); } image = (imageview)v.gettag(r.id.picture); name = (textview)v.gettag(r.id.text); item item = (item)getitem(i); picture.setimageresource(item.drawableid); name.settext(item.name); picture.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(context, secondactivity.class); context.startactivity(intent); } }); homecoming v; } ...
hope helps.
android onclicklistener baseadapter
No comments:
Post a Comment