0% found this document useful (0 votes)
5 views7 pages

Ex 7

This document outlines the steps to create an Android application using Fragments, including setting up the project in Android Studio and writing the necessary Java code for MainActivity and two fragment classes. It provides detailed instructions for creating the layout files for the main activity and the fragments, as well as the functionality to switch between them using buttons. The final output demonstrates the application behavior when the buttons are clicked to display the respective fragments.

Uploaded by

tmsubhashvaleti
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)
5 views7 pages

Ex 7

This document outlines the steps to create an Android application using Fragments, including setting up the project in Android Studio and writing the necessary Java code for MainActivity and two fragment classes. It provides detailed instructions for creating the layout files for the main activity and the fragments, as well as the functionality to switch between them using buttons. The final output demonstrates the application behavior when the buttons are clicked to display the respective fragments.

Uploaded by

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

EXPERIMENT NO-7

AIM: Create an android application using Fragments


Steps to Create the Application Using Fragments
Create a New Android Project
1. Open Android Studio.
2. Choose File > New > New Project.
3. Select Empty Views Activity, name your project (e.g., FragmentApp), and
set the language to Java

MainActivity.java
package com.example.fragmentapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button btnFragment1,btnFragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFragment1=findViewById(R.id.btnFragment1);
btnFragment2=findViewById(R.id.btnFragment2);
btnFragment1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new fragment1());
}
});
btnFragment2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new fragment2());
}
});
}

private void replaceFragment(Fragment fragment) {


FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,fragment);
fragmentTransaction.commit();
}
}
activity_main.xml
<?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"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="0dp"
android:layout_height="680dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</FrameLayout>
<Button
android:id="@+id/btnFragment1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:background="#2196F3"
app:layout_constraintEnd_toStartOf="@+id/btnFragment2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frameLayout"/>

<Button
android:id="@+id/btnFragment2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:background="#FF9800"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnFragment1"
app:layout_constraintTop_toBottomOf="@+id/frameLayout"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Step 2: Create Fragment Classes

We need to create two fragment classes: Fragment1 and Fragment2.


This fragment will display a button. When clicked, it will replace Fragment 1 with Fragment 2.
Fragment1.java
package com.example.fragmentapp;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment1 extends Fragment {


View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fragment1, container, false);
return view;
}
}
Fragment2.java
This fragment will just display a message when it’s selected.

package com.example.fragmentapp;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment2 extends Fragment {


View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fragment2, container, false);
return view;
}
}
Step3. Define Layout for Fragments
fragment1.xml

<?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="@color/fragment1"
tools:context=".fragment1">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Fragment2.xml

<?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="@color/fragment2"
tools:context=".fragment2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run the Application

Output: when button 1 is clicked fragment 1 is displayed ,button 2 clicked fragment2 is


displayed

You might also like