Fragment
use just portrait for phone and w600 for tablet (bigger screen)
for portrait (phone)
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<!-- Top TextView -->
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/top_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="18sp"
android:textStyle="bold"
android:padding="12dp"
android:gravity="start|center_vertical"
android:background="#DDDDDD"
android:layout_weight="1"
/>
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/button_help"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="?android:attr/selectableItemBackground"
android:src="@android:drawable/ic_menu_help" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_fragment_container_main"
android:name="[Your java main fragment with full package name]"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
for w600 tablet
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Top TextView -->
<TextView
android:id="@+id/top_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Title Here"
android:textSize="18sp"
android:textStyle="bold"
android:padding="12dp"
android:gravity="center"
android:background="#DDDDDD" />
<!-- Horizontal Content -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_fragment_container_left"
android:name="[Your java left fragment with full package name]"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:id="@+id/divider_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_fragment_container_main"
android:name="[Your java main fragment with full package name]"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
Guideline for layout LinearLayout
- orientation android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" - orientation android:orientation="horizontal"
android:weightSum="3"
android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="1"
in main activity on onCreate(Bundle savedInstanceState)
public boolean isDualPane = false; // default is portrait
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
isDualPane = findViewById(R.id.my_fragment_container_left) != null;
// R.id.main id for root portrait & w600
AppCompatImageButton buttonHelp = findViewById(R.id.button_help);
buttonHelp.setOnClickListener(buttonHelpListener);
// Load Fragments only if this is a fresh start (savedInstanceState == null)
// This prevents overlapping fragments during screen rotation
if (savedInstanceState == null) {
// If system loaded the w600 layout, load both Fragment
if (isDualPane) {
replaceFragment(
R.id.my_fragment_container_left,
new FragmentLeft(),
null);
replaceFragment(
R.id.my_fragment_container_main,
new FragmentMain(),
null);
} else {
// default load main container
replaceFragment(
R.id.my_fragment_container_main,
new FragmentMain(),
null); // when navigating add "Fragment Main"
}
} View.OnClickListener buttonHelpListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment currentFragment =
getSupportFragmentManager()
.findFragmentById(R.id.my_fragment_container_main); if (currentFragment instanceof FragmentMain) { // NOTE: we still use R.id.my_fragment_container_main in portrait
replaceFragment( R.id.my_fragment_container_main, new FragmentLeft(), "Main Menu"); } else { getSupportFragmentManager().popBackStack(); } }
};
/* * fragment helper */ private void replaceFragment( int containerId, Fragment fragment, String addToBackStackString) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(containerId, fragment); if (addToBackStackString!=null) { transaction.addToBackStack(addToBackStackString); } transaction.commit(); } ....To implement onbackpresseddispatcher pattern code, see this https://dedetoknotes.blogspot.com/2026/01/android-java-onbackpresseddispatcher.html