2

This is my Home.xml layout file

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rellay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/placesgradient">

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="click" 
        />

    **<I want to include another layout here on a click of a button dynamically on click of a button >**

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
       android:layout_below="@id/btn2"
        android:text="hello this is text..see above" />

</RelativeLayout>

This is my Home.java file

public class Home extends Fragment implements OnClickListener {
    Button b1;
    RelativeLayout r1;
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home, container, false);
        b1 = (Button) rootView.findViewById(R.id.btn2);
        b1.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        ViewGroup con = null;
        r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        r1.addView(layoutInflater.inflate(R.layout.test1, con , false));

    }
}

When I click on the button it places the test1.xml layout file at the top of the home.xml layout. I need it to be between the button and the text. I checked this code on stack before but it doesn't seem to work with the FragmentActivity. Here 1 indicates the index or the second child in this case.

rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );

How can I achieve it? Help!!

MAK
  • 1,250
  • 21
  • 50

2 Answers2

4

I would put some container view at the position you want your layout to be like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rellay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@drawable/placesgradient">

    <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="click"
            />

    <FrameLayout 
            android:id="@+id/flContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/btn2"
            android:text="hello this is text..see above" />

</RelativeLayout>

Then you can retrieve the container in your Fragment like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.home, container, false);

    // Retrieve your container
    flContainer = rootView.findViewById(R.id.flContainer);

    b1 = (Button) rootView.findViewById(R.id.btn2);
    b1.setOnClickListener(this);

    return rootView;
}

And later when you want to add your layout you can add a child to the FrameLayout like this:

flContainer.addView(subLayout);

If you later want to change the layout in your container you can do it like this:

flContainer.removeAllViews();
flContainer.addView(otherLayout);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • Is there a way to add animation on removeAllViews() ? – MAK Mar 24 '14 at 17:25
  • Yes there is, just set `android:animateLayoutChanges="true"` on the root view in your layout xml. – Xaver Kapeller Mar 24 '14 at 17:48
  • It is also possible to perform a custom animation, just comment here if you want me to tell you how, but it is a bit more code than just setting `android:animateLayoutChanges="true"`. – Xaver Kapeller Mar 24 '14 at 17:51
  • http://stackoverflow.com/questions/22615720/animation-on-removeallviews-android Have a look at this..I'm done with some part, where do I need to change – MAK Mar 24 '14 at 17:59
0

Use LayoutParams.

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, R.id.btn2);

r1.addView(layoutInflater.inflate(R.layout.test1, con , false),params);

Or put the view in your layout and toggle visibility with View.setVisibility(View.GONE/View.VISIBLE)

ElDuderino
  • 3,253
  • 2
  • 21
  • 38