0% found this document useful (0 votes)
32 views12 pages

Riya Amp P3

Uploaded by

riyawala02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

Riya Amp P3

Uploaded by

riyawala02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Practical 3: Programming Activities and fragments

Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle


of fragments and multiple fragments.
In Android, the fragment is the part of Activity which represents a portion of User
Interface(UI) on the screen. It is the modular section of the android activity that is very
helpful in creating UI designs that are flexible in nature and auto-adjustable based on the
device screen size. The UI flexibility on all devices improves the user experience and
adaptability of the application. Fragments can exist only inside an activity as its lifecycle is
dependent on the lifecycle of host activity. For example, if the host activity is paused, then
all the methods and operations of the fragment related to that activity will stop functioning,
thus fragment is also termed as sub-activity. Fragments can be added, removed, or replaced
dynamically i.e., while activity is running.
<fragment> tag is used to insert the fragment in an android activity layout. By dividing the
activity’s layout multiple fragments can be added in it.
Types of Android Fragments
1. Single Fragment: Display only one single view on the device screen. This type of
fragment is mostly used for mobile phones.
2. List Fragment: This Fragment is used to display a list-view from which the user can
select the desired sub-activity. The menu drawer of apps like Gmail is the best
example of this kind of fragment.
3. Fragment Transaction: This kind of fragments supports the transition from one
fragment to another at run time. Users can switch between multiple fragments like
switching tabs.
Fragment Lifecycle
Step 1: Create a new project with empty activity.

Step 2: Name the project as per your wish.

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

and then in dependencies add the following code:


implementation ("androidx.fragment:fragment-ktx:1.5.5")

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) {}

Step 12: Now in main_activity.kt file update the following code:

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

class MainActivity : ComponentActivity() {


lateinit var binding: MainActivityBinding

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.fragment1.setOnClickListener {
replaceFragment(fragment1())
}
binding.fragment2.setOnClickListener {
replaceFragment(fragment2())
}}

}
private fun replaceFragment(fragment: Fragment){
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}

Put your outputs here

You might also like