Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add fragments programmatically in relativelayout

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"/>
like image 759
Satish Avatar asked Nov 02 '12 13:11

Satish


2 Answers

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

}
like image 170
Napolean Avatar answered Nov 09 '22 21:11

Napolean


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.

like image 25
Shawn Maybush Avatar answered Nov 09 '22 22:11

Shawn Maybush