28

I included second layout to first layout like this:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="clip_horizontal"
    android:layout_alignParentLeft="true" 
    android:id="@+id/rlMenu"
    >

    <Button
        android:id="@+id/bMusteriler"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Musteriler"
        android:textSize="45dp"
        android:layout_alignParentLeft="true"
         />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/rlEkranlar"
     >

    <include
        android:id="@+id/include1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/ikinci" />

</RelativeLayout>
</RelativeLayout>

Problem is how can I change included layout when clicked a button(on java code)?

3
  • create a new xml file and activity and call second through intent. Commented Dec 21, 2011 at 12:35
  • please modify your question, its not much clear. Commented Dec 21, 2011 at 12:37
  • @YugandharBabu I want to change layout="@layout/ikinci" to layout="@layout/third_layout" when clicked the button. Commented Dec 22, 2011 at 15:03

3 Answers 3

60

I suggest ViewFlipper inside RelativeLayout of your include statements. Try like this:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vf"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <include android:id="@+id/include1" layout="@layout/ikinci" />
    <include android:id="@+id/map" layout="@layout/third_layout" />

</ViewFlipper>

Access ViewFlipper as below. Initially first layout is output:

ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);

For Button onClickListener:

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                vf.setDisplayedChild(1);
            }
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. You fixed the problem =))
For experience, if use ViewFlipper maybe you can see a little slow in your app....
24

There is two ways to change layouts in code:

A. Change visibility. Include tow different layouts in your xml:

<include 
    android:id="@+id/id1"
    android:visibility="visible"/>
<include 
    android:id="@+id/id2"
    android:visibility="gone"/>

and in code use this:

findViewById(R.id.id1).setVisibility(View.GONE);
findViewById(R.id.id2).setVisibility(View.VISIBLE);

B. Manually add children. Use the same xml as you use now and add in code you can add or delete children:

yourRelativeLayout.removeAllViews();
yourRelativeLayout.addView(viewToInclude);

Offtopic:

You don't need to write xmlns:android param in RelativeLayout. Just for the most top tag in layout file.

1 Comment

I was confused because Intellij 14 does not offer "visibility" in autocomplete options.
0

Have you tried to get the instance of the current view of the xml you have included the other xml and use findViewById(). View v is the first layout and against this view you use .findViewById()

3 Comments

i tried this: Layout L1 = (Layout) findViewById (R.id.layout1); There is convert error. Can't convert View to Layout. Am i doing wrong somethings?
Layout? There is no such View in SDK as long as you created your own. Make sure you referenced the class names properly. Like: LinearLayout, RelativeLayout, FrameLayout....
i tried you suggestion. But i cant solve my problem. I want change included layout when a button clicked. I can't found a method for this. How can i do that? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.