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"/>
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With