As in this section:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
imageButton01 = (ImageButton) findViewById(R.id.imageButton1);
imageButton01.setOnClickListener(this);
@Override
public void onClick(View view) {
if (view == imageButton01) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO) {
if (resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageButton01.setImageBitmap(bitmap);
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Left", Toast.LENGTH_SHORT);
}
}
}
}
Is there any way to use the same picture-taking scheme through the imageButton, using two layouts, with the same Activity?
Unfortunately, the syntax "setContentView ();" authorizes only one at a time.
PS: I'm using Eclipse's ADT Bundle for Android development.
EDIT: Below are the two .xml related screens:
Add contact:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/addFirstName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="103dip"
android:inputType="text"
android:hint="First name" >
</EditText>
<EditText
android:id="@+id/addLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addFirstName"
android:layout_marginLeft="103dip"
android:inputType="text"
android:hint="Last name"
android:width="190dip" >
</EditText>
<EditText
android:id="@+id/addPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addLastName"
android:layout_marginLeft="103dip"
android:layout_toRightOf="@+id/txtAux5"
android:inputType="text"
android:hint="Phone number"
android:width="190dip" >
</EditText>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addPhone"
android:gravity="center_vertical" >
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1.0"
android:onClick="btnSave_click"
android:text="Save">
</Button>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/addPhone"
android:layout_weight="1.0"
android:text="Cancel" >
</Button>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="103dip"
android:layout_height="103dip"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/addFirstName"
android:src="@drawable/avatar" />
</RelativeLayout>
Edit contact:
<EditText
android:id="@+id/edtFirstName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="103dip"
android:inputType="text"
android:hint="First name" >
</EditText>
<EditText
android:id="@+id/edtLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edtFirstName"
android:layout_marginLeft="103dip"
android:inputType="text"
android:hint="Last name"
android:width="190dip" >
</EditText>
<EditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edtLastName"
android:layout_marginLeft="103dip"
android:layout_toRightOf="@+id/txtAux5"
android:inputType="text"
android:hint="Phone number"
android:width="190dip" >
</EditText>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edtPhone"
android:gravity="center_vertical" >
<Button
android:id="@+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1.0"
android:onClick="btnConfirm_click"
android:text="Confirm">
</Button>
<Button
android:src="@drawable/delete"
android:id="@+id/btnDelete"
android:text=" Delete "
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/edtPhone"
android:layout_weight="1.0"
android:text="Cancel" >
</Button>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="103dip"
android:layout_height="103dip"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/edtFirstName"
android:src="@drawable/avatar" />