Question

i want to change layout of fragment when configuration change by using of fragment ..but fragment is overlapping in activity layout so help me for this how to solve this... and this class file .........

public class Self_funded_1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Self_fragment ls_fragment = new Self_fragment();
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.self_funded_category);
    Configuration config = getResources().getConfiguration();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        /**
         * Landscape mode of the device
         */

        Log.i("LM", "LM");
         ls_fragment = new Self_fragment();
        fragmentTransaction.replace(android.R.id.content, ls_fragment);

    } else {
        /**
         * Portrait mode of the device
         */
        Log.i("LM", "PM");
        PM_self_funded pm_fragment = new PM_self_funded();

        fragmentTransaction.replace(android.R.id.content, pm_fragment);
        fragmentTransaction.remove(ls_fragment);
        fragmentTransaction.hide(ls_fragment);
    }
    fragmentTransaction.commit();
}

this is xml file for activity..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/lm_fragment"
    android:name="info.com.forms.Self_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment
    android:id="@+id/pm_fragment"
    android:name="info.com.forms.PM_self_funded"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

this is snap shot ........

enter image description here

Was it helpful?

Solution

You're declaring the fragments in the xml and programmactically in the Activity as well. You need to just choose one option.

For example, create two different layout files one for portrait and one for landscape and declare the fragments there then you won't need to do anything special in the Activity.

layout/my-layout
layout-land/my-layout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top