Subscribe us on Youtube
- MainActivity.java
- activity_main.xml
- FirstFragment.java
- fragment_first.xml
- SecondFragment.java
- fragment_second.xml
- FragmentInsideFragment.java
- fragment_inside.xml
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; public class MainActivity extends AppCompatActivity { FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Switch sw = findViewById(R.id.switch1); sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(b){ fragmentTransaction = getSupportFragmentManager().beginTransaction(); FirstFragment firstFragment = new FirstFragment(); fragmentTransaction.replace(R.id.fragment_container, firstFragment); fragmentTransaction.commit(); } else{ fragmentTransaction = getSupportFragmentManager().beginTransaction(); SecondFragment secondFragment = new SecondFragment(); fragmentTransaction.replace(R.id.fragment_container,secondFragment); fragmentTransaction.commit(); } } }); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:background="#6200EA" tools:context=".MainActivity"> <Switch android:id="@+id/switch1" android:layout_width="186dp" android:layout_height="36dp" android:layout_marginTop="26dp" android:layout_marginBottom="32dp" android:text="Switch" android:textColor="#FFFFFF" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/fragment_container" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <fragment android:id="@+id/fragment_container" android:name="com.taimoorsikander.fragments.FirstFragment" android:layout_width="409dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/switch1" /> </androidx.constraintlayout.widget.ConstraintLayout>
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first,container,false); //Your Code here return view; } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#3949AB" tools:context=".FirstFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="1" android:textColor="#fff" android:textSize="200sp" /> </FrameLayout>
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_second, container, false); //Your Code here return view; } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom" android:background="#1A237E" tools:context=".SecondFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="2" android:textColor="#FFFFFF" android:textSize="200sp" /> <fragment android:id="@+id/fragment" android:name="com.taimoorsikander.fragments.FragmentInsideFragment" android:layout_width="match_parent" android:layout_height="336dp" android:layout_gravity="bottom" android:layout_margin="20dp"/> </FrameLayout>
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentInsideFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_inside, container, false); //Your Code here return view; } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#1976D2" tools:context=".FragmentInsideFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="264dp" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="Fragment Inside Fragment" android:textColor="#FFFFFF" android:textSize="30sp" /> </FrameLayout>
To learn in detail just follow our Youtube tutorial..
Android Related Videos
- Design a Modern Dashboard
- Material Design Login Screen with Shared Animations
- Create a Navigation Drawer Material Design in 2020
- Design Responsive Android App to fit all screen sizes
Related Articles that you might want to check out.
- Create a new project in android studio for beginners?
- How to create an Account on Fiverr and start earning?
- Create a Gig on Fiverr and start selling specific services.
- How To Start Blogging and Make Money?
Our Projects Make Us Proud