Kul Ken Kul Ken - 1 month ago 7
Android Question

How to change fragment position when rotate device

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.

Answer

If you are using fragment, then you can do this way: 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 :)