FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frameLayout_fragmentTransaction_fragmentContainer, new FragmentA());
fragmentTransaction2.replace(R.id.frameLayout_fragmentTransaction_fragmentContainer, new FragmentB());
Fragment fragment=getSupportFragmentManager().findFragmentById(
R.id.frameLayout_fragmentTransaction_fragmentContainer);
if (fragment!=null){
fragmentTransaction3.remove(fragment);
fragmentTransaction2.addToBackStack(null);
fragmentTransaction.commit();
<FrameLayout
android:id="@+id/frameLayout_fragmentTransaction_fragmentContainer"
fragment_a.xml
FragmentA.java
class FragmentA extends Fragment
public onCreate
public onCreateView return inflater.inflate(R.layout.fragment_a, container, false);
public onViewCreated
fragment_a.xml
fragmentA.java
fragment_b.xml
fragmentB.java
FragmentPagerAdapterSample extends FragmentStatePagerAdapter
public FragmentPagerAdapterSample(FragmentManager fm)
getItem(int position) case 0: return new FragmentA(); return null;
getCount() return 3;
getPageTitle(int position) case 0: return "Fragment A";
getPageWidth return 0.8f;
MainActivity.java
ViewPager viewPager = findViewById(R.id.viewPager_viewPagerSample);
viewPager.setAdapter(new FragmentPagerAdapterSample(getSupportFragmentManager()));
TabLayout tabLayout = findViewById(R.id.tabLayout_viewPagerSample);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
dialog_sample.xml
class SampleDialog extends DialogFragment
public Dialog onCreateDialog
MainActivity.java
SampleDialog sampleDialog = SampleDialog.newInstance("ali");
sampleDialog.show(getSupportFragmentManager(), null);
SampleDialog.java
class SampleDialog extends DialogFragment
public onCreateDialog
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sample, null);
builder.setView(view);
yesBtn = view.findViewById .setOnClickListener
dialogCallback.onNoClicked();
dismiss();
noBtn = view.findViewById .setOnClickListener
dialogCallback.onNoClicked();
dismiss();
return builder.create();
BottomSheetDialogSample.java
class BottomSheetDialogSample extends android.support.design.widget.BottomSheetDialogFragment
public View onCreateView
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sample, container, false);
return view;
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="24dp"
android:paddingTop="32dp"
android:paddingEnd="24dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_main_fragmentTransactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Fragment Transactions" />
<Button
android:id="@+id/btn_main_viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="ViewPager" />
<Button
android:id="@+id/btn_main_dialogFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Dialog Fragment" />
<Button
android:id="@+id/btn_main_bottomSheetFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="BottomSheet Fragment" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SampleDialog.DialogCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_main_fragmentTransactions:
startActivity(new Intent(this, FragmentTransactionsActivity.class));
break;
case R.id.btn_main_viewPager:
startActivity(new Intent(this, ViewPagerSampleActivity.class));
break;
case R.id.btn_main_dialogFragment:
SampleDialog sampleDialog = SampleDialog.newInstance("ali");
sampleDialog.show(getSupportFragmentManager(), null);
break;
case R.id.btn_main_bottomSheetFragment:
BottomSheetDialogSample bottomSheetDialogSample=new BottomSheetDialogSample();
bottomSheetDialogSample.show(getSupportFragmentManager(),null);
break;
}
}
@Override
public void onYesClicked() {
Toast.makeText(this,"On Yes Button Clicked",Toast.LENGTH_LONG).show();
}
@Override
public void onNoClicked() {
Toast.makeText(this,"On No Button Clicked",Toast.LENGTH_LONG).show();
}
}
public class FragmentTransactionsActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_transactions);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_fragmentTransactions_add:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frameLayout_fragmentTransaction_fragmentContainer, new FragmentA());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.btn_fragmentTransactions_replace:
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frameLayout_fragmentTransaction_fragmentContainer, new FragmentB());
fragmentTransaction2.addToBackStack(null);
fragmentTransaction2.commit();
break;
case R.id.btn_fragmentTransactions_remove:
FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.frameLayout_fragmentTransaction_fragmentContainer);
if (fragment!=null){
fragmentTransaction3.remove(fragment);
fragmentTransaction3.commit();
}
break;
}
}
}
<?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=".FragmentTransactionsActivity">
<FrameLayout
android:id="@+id/frameLayout_fragmentTransaction_fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fragmentTransactions_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Add" />
<Button
android:id="@+id/btn_fragmentTransactions_replace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Replace" />
<Button
android:id="@+id/btn_fragmentTransactions_remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Remove" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="A"
android:textSize="50sp" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="B"
android:textSize="50sp" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="C"
android:textSize="50sp" />
</FrameLayout>
public class FragmentA extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
package com.sevenlearn.fragmentstutorial;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentB extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
public class FragmentC extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_c, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
<?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=".ViewPagerSampleActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout_viewPagerSample"
android:layout_width="match_parent"
app:tabMode="fixed"
android:layout_height="56dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_viewPagerSample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout_viewPagerSample">
</android.support.v4.view.ViewPager>
</RelativeLayout>
public class ViewPagerSampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_sample);
ViewPager viewPager = findViewById(R.id.viewPager_viewPagerSample);
viewPager.setAdapter(new FragmentPagerAdapterSample(getSupportFragmentManager()));
TabLayout tabLayout = findViewById(R.id.tabLayout_viewPagerSample);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
// viewPager.setOffscreenPageLimit(3);
// viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// @Override
// public void onPageScrolled(int i, float v, int i1) {
//
// }
//
// @Override
// public void onPageSelected(int i) {
//
// }
//
// @Override
// public void onPageScrollStateChanged(int i) {
//
// }
// });
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_dialog_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
<Button
android:id="@+id/btn_dialog_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</LinearLayout>
</LinearLayout>
public class SampleDialog extends DialogFragment {
private DialogCallback dialogCallback;
private String name;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MainActivity) {
dialogCallback = (DialogCallback) context;
}
name = getArguments().getString("name");
Toast.makeText(getContext(), name, Toast.LENGTH_SHORT).show();
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sample, null);
builder.setView(view);
Button yesBtn = view.findViewById(R.id.btn_dialog_yes);
yesBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogCallback.onYesClicked();
dismiss();
}
});
Button noBtn = view.findViewById(R.id.btn_dialog_no);
noBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogCallback.onNoClicked();
dismiss();
}
});
return builder.create();
}
public static SampleDialog newInstance(String name) {
Bundle args = new Bundle();
args.putString("name", name);
SampleDialog fragment = new SampleDialog();
fragment.setArguments(args);
return fragment;
}
interface DialogCallback {
void onYesClicked();
void onNoClicked();
}
}
public class FragmentPagerAdapterSample extends FragmentStatePagerAdapter {
public FragmentPagerAdapterSample(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fragment A";
case 1:
return "Fragment B";
case 2:
return "Fragment C";
}
return null;
}
@Override
public float getPageWidth(int position) {
return 0.8f;
}
}
public class BottomSheetDialogSample extends android.support.design.widget.BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sample, container, false);
return view;
}
}