1

I Have a Fragment, and i want to add elements (textview, button) dynamically when i click on Floating Action Button.

Code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.xyz, container, false);

    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // new elements on click



            // new elements on click

        }
    });
    return view;
}
chridam
  • 100,957
  • 23
  • 236
  • 235
Pranav Fulkari
  • 110
  • 2
  • 9

2 Answers2

5

try this xml code for Fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="10dp"
        app:srcCompat="@android:drawable/ic_dialog_email"/>
</FrameLayout>

And java code for Fragment

 package com.example.androiddeveloper.fragmentdynamic;


    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.LinearLayout;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class BlankFragment extends Fragment {

        private LinearLayout linearLayout = null;

        public BlankFragment() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_blank2, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);

            FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Create your Controls(UI widget, Button,TextView) and add into layout
                    Button btn = new Button(getActivity());
                    btn.setText("Manual Add");
                    btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                    linearLayout.addView(btn);
                }
            });


        }
    }
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
0

You could do it like this :

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.xyz, container, false);

FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

       View view = YouFragmentClass.this.getView(); // returns base view of the fragment

     if (view != null&&(view instanceof ViewGroup)){

   //set the properties for button
     Button btn = new Button(this);
     btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     btn.setText("Button");


    ViewGroup viewGroup = (ViewGroup) view;
    viewGroup.addView(btn);
}
    }
});
return view;

}

I havent tested it but it should do the job

Hansfritzi
  • 92
  • 6
  • Great !! It creates a button, but when i again click fab error occurs. "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first." – Pranav Fulkari Jul 15 '16 at 21:23
  • @ProtonzzBaba You need to create a new button each time you click the FAB. I changed to code to a example how you add a button each time – Hansfritzi Jul 16 '16 at 11:46