0% found this document useful (0 votes)
3 views8 pages

Fragments

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)
3 views8 pages

Fragments

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/ 8

Here's an example of how to create three fragments named CIVE, COBE, and COED in Java for an

Android application:

1. Set up your project

Ensure your project uses AndroidX libraries.

Add the necessary dependencies for fragments in your build.gradle file.

---

2. Create the Fragment Classes

Each fragment class will extend Fragment and include a layout.

CIVEFragment.java

package com.example.myapp;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;
public class CIVEFragment extends Fragment {

public CIVEFragment() {

// Required empty public constructor

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_cive, container, false);

COBEFragment.java

package com.example.myapp;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;
public class COBEFragment extends Fragment {

public COBEFragment() {

// Required empty public constructor

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_cobe, container, false);

COEDFragment.java

package com.example.myapp;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class COEDFragment extends Fragment {


public COEDFragment() {

// Required empty public constructor

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_coed, container, false);

---

3. Create Layout Files

Create corresponding layout files for each fragment in the res/layout folder.

fragment_cive.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="CIVE Fragment"

android:layout_gravity="center"

android:textSize="20sp" />

</FrameLayout>

fragment_cobe.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="COBE Fragment"

android:layout_gravity="center"

android:textSize="20sp" />

</FrameLayout>
fragment_coed.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="COED Fragment"

android:layout_gravity="center"

android:textSize="20sp" />

</FrameLayout>

---

4. Add Fragments to an Activity

In your activity, you can add the fragments programmatically or using a FragmentTransaction.

MainActivity.java

package com.example.myapp;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Show the first fragment (CIVE) initially

replaceFragment(new CIVEFragment());

private void replaceFragment(Fragment fragment) {

FragmentManager fragmentManager = getSupportFragmentManager();

fragmentManager.beginTransaction()

.replace(R.id.fragment_container, fragment)

.commit();

}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="match_parent" />

---

5. Navigation Between Fragments (Optional)

You can use buttons or tabs to navigate between fragments using FragmentTransaction.

Let me know if you'd like additional navigation examples!

You might also like