4.implementation of Fragments Within Activities
4.implementation of Fragments Within Activities
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>
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">
</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">
</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;
@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());
}
});
}
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;
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;
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: