0

I want to change from layout 1 to layout 2, after rotate still keep content. Can somebody show me how to do that?

from this: https://www.dropbox.com/sc/y2nyrzard859hf2/AAD0qVjWoLzKcnQV9a4FTQi_a

to this: https://www.dropbox.com/sc/29hhlbfm31cfs0j/AADCWsFNzD7DKHx4q9i2FlbDa

this is my code but seem like it didn't work

if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.activity_create_bill3);

    }
    else {
        setContentView(R.layout.activity_create_bill2);
    }

    if(fragmentManager.findFragmentByTag("fragment_product")==null) {
        fragment_product = new Fragment_Product();
        fragmentTransaction.replace(R.id.fragment_product,fragment_product,"fragment_product");

    }
    else
        fragmentTransaction.replace(R.id.fragment_product,fragmentManager.findFragmentByTag("fragment_product"));


    if(fragmentManager.findFragmentByTag("fragment_product_chosen")==null) {
        fragment_product_chosen = new Fragment_Product_Chosen();
        fragmentTransaction.replace(R.id.fragment_product_chosen,fragment_product_chosen,"fragment_product_chosen");

    }
    else
        fragmentTransaction.replace(R.id.fragment_product_chosen,fragmentManager.findFragmentByTag("fragment_product_chosen"),"fragment_product_chosen");


    fragmentTransaction.commit();

I using 2 diffent layout, it has a same view but one in horizontal and another in vertical, when rotate, fragment_product still keep content, but fragment_product_chosen are disappear.

Kul Ken
  • 143
  • 1
  • 2
  • 8

2 Answers2

1

You should have 3 clases:

  1. FragmentMain
  2. FragmentSide
  3. MainActivity

click here to see your layout folder

Code in your MainActivity Class:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

Code in your FragmentMain Class:

public class FragmentMain extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

}

Code in your FragmentSide Class:

public class FragmentSide extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_side, container, false);
}

}

Then in your activity_main.xml:

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_main"
    android:layout_marginTop="230dp"
    class="au.com.example.multi_fragments.FragmentMain" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:id="@+id/fragment_side"
    class="au.com.example.multi_fragments.FragmentSide" />
    />

same way in your activity_main.xml(land):

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="250dp"
    android:id="@+id/fragment_main"
    class="au.com.example.multi_fragments.FragmentMain" />

<fragment
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:id="@+id/fragment_side"
    class="au.com.example.multi_fragments.FragmentSide" />

in your fragment_main.xml:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="@+id/textViewMain" />

In your fragment_side.xml:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="View 1"
    android:textSize="20sp"
    android:padding="20dp"
    android:textStyle="bold"
    android:id="@+id/textViewMain" />

Click here to see the output

I hope this solution is the one you want. Good luck :)

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
Priyanka
  • 26
  • 3
0

To have different layouts for landscape and portrait modes, create two folders under the res folder: layout and layout-land. All XML files should have the same names in both folders. For more details, read Designing for Multiple Screens. Even though this article is for different screen sizes, the techniques apply to different device orientations as well.

As for saving and restoring data, this is the same as destroying the activity without an orientation change.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
  • I create 2 layout with different name in same folder so this is not working. Thank you so much, now im understand. – Kul Ken Oct 13 '16 at 04:56