Multiple layouts in one Activity - how to use?

Asked

Viewed 1,988 times

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

  • 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.

  • 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?

  • @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

  • You edit your question and post the two Layout’s so I can show you a way to do?

  • Post edited with the two layouts!

Show 1 more comment

1 answer

4


The Difference Between Your Layout’s

From what I see, you have two Layout’s practically equal, with only a few minor changes (the ID’s), outside the ID’s would be the button btnSave and the btnConfirm which are equal but with different text, and also a ImageButton you added to "Edit contact".

Comments on the differences

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

Conclusion and resolution of the problem

Easily you can do the following, if you are adding contact, change the text of the btnSave to "Confirm" otherwise stay as is by default that would be "Save"

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

Since the Layout pattern will be "Add Contact" you should hide the Imagebutton by default too, as it should only appear 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 added the property android:visibility="gone" 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 need not have 2 Layout’s only one Layout you can use by changing text and hiding/showing elements to behave accordingly.

  • Thank you very much for the answer! I have just one more question. When creating the contact, the imageButton should come with the initial icon, so that the user can add it right from the beginning, if they wish. However, when you click on the contact in a list to edit it, it should also be able to see the current photo and change it. So I can’t just let imageButton turn off right away. Do you know any way to do this?

  • You may have a condition to enable imageButton, but I don’t understand in which situations you want to enable it

  • I want it to remain enabled, but in two different situations. To create contact and add a new image. And to edit, having the option to change the current one. I’m making a contact book.

  • 1

    add a boolean variable at the beginning of your activity where the variables of the Activity class are placed as private call isAdicionandoImagemNova = false for example, and seven to true when you click to add a new image, and then check if it is true you show the imageButton and arrow the boolean to false. And to edit it no longer appears?

  • I’ll try, it’s a good idea. For now, I need to figure out how to get the image from the database and transfer to imageButton yet.

  • anything post another question concerning this :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.