Fragment 1st
XML
Create a Fragment Activity
Com > New > Fragment > Fragment (Blank)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#2196F3"
>
</FrameLayout>
</LinearLayout>
JAVA
package com.rakibshah.fragment;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.FirstFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout , new FirstFragment());
fragmentTransaction.commit();
}
}Fragment<LinearLayout 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="com.FirstFragment"
android:background="#88B6BC"
android:orientation="vertical"
android:padding="15dp"
>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1st Fragment"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>
</LinearLayout>Fragment JAVApackage com;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rakibshah.fragment.R;
public class FirstFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_first, container, false);
return myView;
}
}