1st & 2nd Fragmeant

 Main Activity XML



<?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"
android:layout_margin="10dp"
>

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Third Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>


<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


</LinearLayout>


 Main Activity JAVA

package com.rakibshah.fragment;

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

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

import com.FirstFragment;
import com.SecondFragment;

public class MainActivity extends AppCompatActivity {

Button button1, button2, button3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout , new FirstFragment());
fragmentTransaction.commit();

}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout, new SecondFragment());
fragmentTransaction.commit();

}
});

}
}

Create Fragment File

Com > NEW > Fragment > Fragment(Blank)


First Fragment XML

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


First Fragment JAVA

package 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 {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_first, container, false);

return myView;
}
}


 Second Fragment XML

<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"
android:orientation="vertical"
android:background="#DD4D4D"
tools:context="com.SecondFragment">



<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Fragmeant"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="center"
/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>


Second Fragment JAVA

package com;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.rakibshah.fragment.R;


public class SecondFragment extends Fragment {

Button button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_second, container, false);

button = myView.findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(getContext(), "Hello", Toast.LENGTH_LONG).show();

}
});
        return myView;
        }
   }




No comments: