Pranav Fulkari
Pranav Fulkari

Reputation: 120

How to add elements dynamically in fragment in android

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;
}

Upvotes: 1

Views: 5447

Answers (2)

timonmw
timonmw

Reputation: 92

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

Upvotes: 0

Muhammad Waleed
Muhammad Waleed

Reputation: 2601

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);
                }
            });


        }
    }

Upvotes: 6

Related Questions