Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

Adding multiple fragments to LinearLayout

I want to add multiple fragments to my LinearLayout by clicking a button.

This is my MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button addFragmentButton = (Button) findViewById(R.id.addFragmentButton);
        addFragmentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                HeaderButtonFragment fragment = new HeaderButtonFragment();
                Integer x = (int) (Math.random() * 1000);
                String name = String.valueOf(x);
                fragmentTransaction.add(R.id.fragmentsHolder, fragment, name);
                fragmentTransaction.commit();
            }
        });
    }
}

my main layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="majewski.ninja.mylibrary.MainActivity">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/fragmentsHolder">
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add new fragment"
        android:id="@+id/addFragmentButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

My Fragment:

public class HeaderButtonFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Integer x = (int) (Math.random() * 1000);
        String name = String.valueOf(x);
        View view =  inflater.inflate(R.layout.fragment_header_button, container, false);
        ((Button) view.findViewById(R.id.fragmentButton)).setText(name);
        return view;
    }
}

and fragment layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="majewski.ninja.mylibrary.HeaderButtonFragment">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/fragmentButton"
        android:layout_gravity="left|top" />
</FrameLayout>

This is what I get when I click the button:

enter image description here

When I click it more times nothing changes but I want it to add new fragments so it can look like this:

enter image description here

Is there a way I can do that?

Upvotes: 0

Views: 1271

Answers (1)

Danail Alexiev
Danail Alexiev

Reputation: 7772

When you are adding multiple fragments to the same placeholder, they are getting stacked one on top of the other. This is why you are only seeing one fragment when you press the button.

For your use case, I would consider using some kind of Adapter View - a Grid View maybe - to achieve the dynamic creation of view and the their addition to the view hierarchy. It would be much simpler and easy to implement.

Upvotes: 1

Related Questions