Multiple layouts in one activity - how to use it?

5

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" />

    
asked by anonymous 14.03.2014 / 12:29

1 answer

4

Difference between your Layout's

As I see it, you have two Layout's practically the same, with only a few minor changes (the ID's), outside the ID's would be the btnSave button and btnConfirm which are the same but with different text, ImageButton you added in the "Edit contact".

Comments on differences

You do not need to make ID's changes, you can actually use the "Edit Contact" Layout for both, since they are equal elements, with the exception of btnConfirm and imageButton , however this is easy to solve.

Conclusion and resolution of the problem

You can easily do the following if you are adding a contact, change the text from btnSave to "Confirm" otherwise stay as is "Save" by default

if (isEdicao){
  Button btnSave = (Button) findViewById(R.id.btnSave);
  btnSave.setText("Confirm");
}

Given that the Layout pattern will be "Add Contact" you should hide the ImageButton by default as well, as it should only appear to be for Editing:

<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"
    android:visibility="gone" />

Note that I have added the android:visibility="gone" property to become invisible and non-existent (does not represent space).

The code would look like this:

if (isEdicao){
  Button btnSave = (Button) findViewById(R.id.btnSave);
  btnSave.setText("Confirm");
  ImageButton imgButton = (ImageButton) findViewById(R.id.imageButton1);
  imgButton.setVisibility(View.VISIBLE);
}

So you should not and do not need to have 2 Layout's just a Layout you can use by changing texts and hiding / showing elements to behave accordingly.

    
14.03.2014 / 13:44