Making a gallery with automatically generated ImageView s in Android -
i'm writing android application taking photos camera , generating imageview
s previewing them.
this how take pictures , put them in imageview
:
public class takephoto extends activity { private static final int camera_request = 1888; button btncaputre; imageview image; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_take_photo); btncaputre = (button)findviewbyid(r.id.btncapture); image = (imageview)findviewbyid(r.id.imageview1); btncaputre.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent cameraintent = new intent(android.provider.mediastore.action_image_capture); startactivityforresult(cameraintent, camera_request); } }); } protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == camera_request && resultcode == result_ok) { bitmap photo = (bitmap) data.getextras().get("data"); image.setimagebitmap(photo); } }
this layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="100dp" > <button android:id="@+id/btncapture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="camera" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </linearlayout>
how dynamically add more imageview
(for previewing new photos made) in same activity
?
edit: new edited code implementation of setonclicklistener
dynamically created imageview
:
public class takephoto extends activity { private static final int camera_request = 1888; button btncaputre; imageview image; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_take_photo); btncaputre = (button)findviewbyid(r.id.btncapture); image = (imageview)findviewbyid(r.id.imageview1); btncaputre.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent cameraintent = new intent(android.provider.mediastore.action_image_capture); startactivityforresult(cameraintent, camera_request); } }); image.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.e("image id","-->"+ image.getid()); } }); } protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == camera_request && resultcode == result_ok) { bitmap photo = (bitmap) data.getextras().get("data"); image = new imageview(this); layoutparams param=new layoutparams(layoutparams.match_parent,layoutparams.match_parent); image.setlayoutparams(param); image.setimagebitmap(photo); image.settag(java.util.uuid.randomuuid().tostring()); mlayout.addview(image); } }
for adding imageview dynamically need following changes in code
in layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="100dp" > <button android:id="@+id/btncapture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="camera" /> <linearlayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"/> </linearlayout>
in java code:
in oncreate(): linearlayout view viewgroup xml
linearlayout mlayout=(linearlayout)findviewbyid(r.id.layout); protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == camera_request && resultcode == result_ok) { bitmap photo = (bitmap) data.getextras().get("data"); imageview image=new imageview(this); layoutparam param=new layoutparam(layoutparam.wrap_content,layoutparam.wrap_content); image.setlayoutparam(param); image.setimagebitmap(photo); mlayout.addview(image); } }
Comments
Post a Comment