How to add fragments programmatically in relativelayout

12,762

Solution 1

We can do it as follows :

Activity class in which relative layout is present. Let that relative layout (mParentLayout) be root element of activity's view hierarchy. And the fragment to be added to the relative layout will be mGridFragment at a position below the Button (mGridBtn).

public class DroidOpsActivity extends FragmentActivity implements
    OnClickListener {

    private RelativeLayout mParentLayout = null;
    private Button mGridBtn = null;
    private FragmentManager mFragmentMgr = null;
    private FragmentTransaction mFragmentTransc = null;
    private Fragment mGridFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
    }

    private void initialize() {
        setContentView(R.layout.activity_droid_ops);
        mParentLayout = (RelativeLayout) findViewById(R.id.parent_layout);
        mGridBtn = (Button) findViewById(R.id.grid_button);
        mFragmentMgr = getSupportFragmentManager();
        registerListener();
    }

    private void registerListener() {
        mGridBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.grid_button:
                    loadFragment();
                    mGridBtn.setEnabled(false);
        break;
        }
    }

    private void loadFragment() {
        mGridFragment = new DroidOpsFragments();
        mFragmentTransc = mFragmentMgr.beginTransaction();
        mFragmentTransc.add(mParentLayout.getId(), mGridFragment);
        mFragmentTransc.addToBackStack(null);
        mFragmentTransc.commit();
    }

    public RelativeLayout.LayoutParams fetchLayoutParams() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        // We can add any rule available for RelativeLayout and hence can position accordingly
        params.addRule(RelativeLayout.BELOW, mGridBtn.getId());
        return params;
    }
}

Corresponding Fragment class which is to be positioned with in the activity's layout.

public class DroidOpsFragments extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_word_grid, null);

        // Here we are fetching the layoutParams from parent activity and setting it to the fragment's view.

        view.setLayoutParams(((DroidOpsActivity) activity).fetchLayoutParams());
        return view;
    }

}

Solution 2

You have to set layout prarams in your Fragment's views.

Are they XML or hardCode?

If they are hardcode, you will have to get the FrameLayout Wrapper that Google puts on all fragment's views using the backwards compatible support library and set its relativelayout params either in your view's onlayout or onMeasure.

Otherwise, if they are XML, just set the relative rules.

Share:
12,762
Satish
Author by

Satish

Updated on June 12, 2022

Comments

  • Satish
    Satish almost 2 years

    I have designed a RelativeLayout. I am trying to add fragment to it. It overlaps. What do I need to do so that it comes one below other.

        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    
        FragmentTest myFragment = new FragmentTest();
        t.add(layout.getId(), myFragment, "myFirstFragment");
    
        FragmentTest myFragment2 = new FragmentTest();
        t.add(layout.getId(), myFragment2, "mySecondFragment");
    
        t.commit();
    

    //Views of myFragment and myFragment2 overlaps even though I have set the orientation to vertical.

    <RelativeLayout
        android:id="@+id/idll1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="0sp"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp" 
        android:orientation="vertical"/>
    
  • Satish
    Satish over 11 years
    What happens is when I use LinearLayout after the line t.commit(); I try to add button. layout.addView(button). Then the button is positioned at the top then the 2 fragments come down. I expect that the 2 fragment views appear top as in order or addition and then the button. But that way it does not happen. Any thoughts?
  • Satish
    Satish over 11 years
    Thanks. I have not had time to check this as I am temporarily moved away from this project. But definitely I will check this and if it works I will accept your answer.