How to change the class & ldquo; & quot; attribute of a java code fragment

advertisements

In my android app, I have defined a fragment in my xml layout. The code is below:

<fragment
    android:id="@+id/list_fragment"
    android:layout_width="220dp"
    android:layout_height="fill_parent"
    android:layout_marginLeft="62dp"
    android:layout_marginTop="20dp"
    class="com.example.shantaportfolio.ListFragment" />

But I need to change the class attribute of the fragment from my java code. How can I do it? which method use?


You need to change <fragment/> by <FrameLayout/> in your .xml file

<fragment
    android:id="@+id/list_fragment"
    android:layout_width="220dp"
    android:layout_height="fill_parent"
    android:layout_marginLeft="62dp"
    android:layout_marginTop="20dp"
/>

and then use java code to add fragment programatically

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListFragment fragment = new ListFragment();
fragmentTransaction.replace(R.id.list_fragment, fragment);
fragmentTransaction.commit();

you can get more details from http://developer.android.com/guide/topics/fundamentals/fragments.html