5
As in this passage:
@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 scheme to take photos through imageButton, using two layouts, with the same Activity?
Unfortunately, the syntax "setContentView();" authorises only one at a time.
PS.: I’m using the ADT Bundle Eclipse for Android development.
EDIT.: Below are the two . xml related to the 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" />
What do you mean by "two layouts"?
– ramaral
Two canvases. I created two xml, one to create and one to add the contact and would like to use the same method both to add a photo and to change it.
– Leonardo Saldanha
My dear friend @Leos, you can only have a layout for your Activity, and also would have no reason to want two layout’s in one activity, what would be your goal?
– Paulo Roberto Rosa
@Leos, you can use the same layout to do this, but you can hide/show elements according to the state whether it’s editing or creating
– Paulo Roberto Rosa
You edit your question and post the two Layout’s so I can show you a way to do?
– Paulo Roberto Rosa
Post edited with the two layouts!
– Leonardo Saldanha