Riya Amp P3
Riya Amp P3
Step 3: Since we have created a project with empty activity it won’t be having any xml
file(this file is used to design the interface for the application) to create that we need to
create layout directory first and then add the layout file.
Step 4: Once the file has been created which is named as main_activity here, since it is a
blank file , we are adding three components here which are two buttons and a fragment.
Change the layout to RelativeLayout and add the following code inside it.
<fragment
android:layout_width="match_parent"
android:layout_height="600dp"
android:id="@+id/fragmentContainer"/>
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:id="@+id/fragment1"
android:backgroundTint="@color/teal_200"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="30dp"
android:text="Fragment 1"
android:textStyle="bold"/>
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:id="@+id/fragment2"
android:backgroundTint="@color/teal_200"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="30dp"
android:text="Fragment 2"
android:textStyle="bold"/>
Step 5: Now the first fragment has to be created, to do so right click on layout folder and
select New -> Layout Resource Fie and name it has Fragment1 and change its layout to
RelativeLayout.
Add the following code below to the layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragment1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="This is Fragment 1"
android:textSize="40dp"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:textColor="@color/black"
</RelativeLayout>
Step 6: To add background color to the fragments, add the color to color.xml file
<color name="fragment1">#E91E63</color>
<color name="fragment2">#9C27B0</color>
Step 7: Now the second fragment would also be created in similar manner.
Add the following code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragment2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="This is Fragment 2"
android:textSize="40dp"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:textColor="@color/white"/>
</RelativeLayout>
Step 8: Now goto build.gradle and enable view binding feature by add the code mentioned
below in buildFeatures function
viewBinding = true
dataBinding = true
Step 9: Now Kotlin class file has to be created for both the fragment files.
Step 10: Now add the following code to fragment1.kt file
package com.example.fragments
import androidx.fragment.app.Fragment
class fragment1 : Fragment(R.layout.fragment1) {}
Step 11: Now add the following code to fragment2.kt file
package com.example.fragments
import androidx.fragment.app.Fragment
class fragment2 : Fragment(R.layout.fragment2) {}
package com.example.fragments
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.fragments.ui.theme.FragmentsTheme
import android.view.View
import androidx.fragment.app.Fragment
import com.example.fragments.databinding.MainActivityBinding
}
private fun replaceFragment(fragment: Fragment){
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}