How to add fragments programmatically in relativelayout

I developed RelativeLayout. I am trying to add a fragment to it. It is overlapping. What I need to do to get it below the others.

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

// Overlay views myFragment and myFragment2, although I 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"/> 
+8
android android-layout android-fragments relativelayout
source share
4 answers

We can do this as follows:

The activity class in which the relative layout is present. Let this relative layout (mParentLayout) be the root element of the activity hierarchy. And the fragment to be added to the relative layout will be mGridFragment in the position below 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; } } 

The corresponding fragment class to be placed in the action 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 view. view.setLayoutParams(((DroidOpsActivity) activity).fetchLayoutParams()); return view; } } 
+4
source share

You must set the profile layouts in the fragment views.

Are they XML or hardCode?

If they are hard code, you will need to get the FrameLayout framework, which Google will place in all views of the fragment using the backward compatible support library, and set its relativelayout parameters in either your onlayout or onMeasure view.

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

+2
source share

I think it should be LinearLayout

0
source share

Quick and dirty seems a little better. If you already know your layout, you can save a few steps, and I know a giant one, if the (X instanceof Class) set of blocks frowned, it is quite universal.

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ViewGroup.LayoutParams p = view.getLayoutParams(); if (p instanceof CoordinatorLayout.LayoutParams) { CoordinatorLayout.LayoutParams params = ((CoordinatorLayout.LayoutParams)p); params.gravity = Gravity.END | Gravity.BOTTOM; view.setLayoutParams(params); } else if (p instanceof RelativeLayout.LayoutParams) { RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)p); params.addRule(RelativeLayout.ALIGN_BOTTOM); view.setLayoutParams(params); } } 
0
source share

All Articles