0% found this document useful (0 votes)
8 views4 pages

4.implementation of Fragments Within Activities

The document outlines the implementation of fragments within an Android activity, including the XML layout for the main activity and two fragment layouts. It provides the Java code for the MainActivity class, which handles fragment transactions based on button clicks, and the fragment classes that inflate their respective layouts. The fragments display welcome messages and are designed to be switched dynamically in the main activity's FrameLayout.

Uploaded by

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

4.implementation of Fragments Within Activities

The document outlines the implementation of fragments within an Android activity, including the XML layout for the main activity and two fragment layouts. It provides the Java code for the MainActivity class, which handles fragment transactions based on button clicks, and the fragment classes that inflate their respective layouts. The fragments display welcome messages and are designed to be switched dynamically in the main activity's FrameLayout.

Uploaded by

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

4. Implementation of fragments within the activity.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frameLayout"/>

<Button
android:id="@+id/btn1"
android:layout_width="120dp"
android:layout_height="60dp"
android:text="Fragment1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
/>

<Button
android:id="@+id/btn2"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginStart="50dp"
android:layout_marginBottom="50dp"
android:text="Fragment2" />

</RelativeLayout>

CREATE 2 FRAGMENTS FOR LAYOUT -> app->res->layout->new-> Fragment (Blank)


➢ Name it as fragment1
➢ Name it as fragment2

fragment_fragment1.xml
<?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"
tools:context=".fragment1"
android:background="#FF5722">

<!-- TODO: Update blank fragment layout -->


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Welcome to Fragment 1"
android:textSize="50dp"
android:textColor="#FF03DAC5"
android:textAlignment="center"
/>

</FrameLayout>

fragment_fragment2.xml
<?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"
tools:context=".fragment2"
android:background="#FF5722">

<!-- TODO: Update blank fragment layout -->


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Welcome to Fragment 2"
android:textSize="50dp"
android:textColor="#FF03DAC5"
android:textAlignment="center"
/>

</FrameLayout>

MainActivity.java
package com.example.myapplication;

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 firstFragmentBtn , secondFragmentBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstFragmentBtn = findViewById(R.id.btn1);
secondFragmentBtn=findViewById(R.id.btn2);

firstFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new fragment1());
}
});

secondFragmentBtn.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();
}
}

fragment1.java

package com.example.myapplication;

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
package com.example.myapplication;

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 ;
}
}

Output:

You might also like