How to Replace a Fragment on Button Click of That Fragment

How to replace a Fragment on button click of that fragment?

you can replace fragment by FragmentTransaction.

Here you go.

Make an interface.

public interface FragmentChangeListener 
{
public void replaceFragment(Fragment fragment);
}

implements your Fragment holding activity with this interface.

public class HomeScreen extends FragmentActivity implements
FragmentChangeListener {

@Override
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();;
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainerId, fragment, fragment.toString());
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.commit();
}

}

Call this method from Fragments like this.

//In your fragment.

public void showOtherFragment()
{
Fragment fr=new NewDisplayingFragment();
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.replaceFragment(fr);
}

Hope this will work!

NOTE: mContainerId is id of the view who is holding the fragments inside.
You should override Fragment's onString() method as well.

Change fragments when button is clicked

You can use OnfragmentInteractionListener for this. It is a interface that your activity should implement. You can call the methods from your fragments and it will be like it is calling a method from the activity.

In the following example, first fragment has a button which when clicked calls the method changeFragment(2) and similarly the second fragment calls method changeFragment(1). This method is implemented in the main activity and through if .. else it figures out which fragment to replace.

OnFragmentInteractionListener.java

public interface OnFragmentInteractionListener {
public void changeFragment(int id);
}

FragmentA:

public class first extends Fragment {

private OnFragmentInteractionListener mListener;

public first() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.changeFragment(2);
}
});
return view;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;

}

}

FragmentB:

public class first extends Fragment {

private OnFragmentInteractionListener mListener;

public first() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.changeFragment(1);
}
});
return view;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;

}

}

MainActivity:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnFragmentInteractionListener {

//Rest of the code

@Override
public void changeFragment(int id){
if (id == 1) {
fragment = new first();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == 2) {
fragment = new second();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
}
}

Hope it helps!!

New fragment on button click from another fragment?

I think you are not initialising the FragmentManager. Without FragmentManager it is not possible to replace,update,create any fragment. Beacuse it is responsible replace or deleting any fragment. Hope this will help.

public class FragmentName extends Fragment {

public FragmentName() {
// Required empty public constructor

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_name, container, false);

Button ID = (Button) rootView.findViewById(R.id.buttonID);
ID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentName NAME = new FragmentName();
fragmentTransaction.replace(R.id.main_container, NAME);
fragmentTransaction.commit();

}
});
return rootView;
}
}

How to replace one fragment with another on button click in Xamarin.Android?

You can create two Fragment and its xml first.

FragmentOne :

public class Fragment1 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your fragment here
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
return inflater.Inflate(Resource.Layout.layoutFragment1, container, false);

//return base.OnCreateView(inflater, container, savedInstanceState);
}
}

and its xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/th2"/>

</LinearLayout>

FragementTwo :

public class Fragment2 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your fragment here
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
return inflater.Inflate(Resource.Layout.layoutFragment2, container, false);

//return base.OnCreateView(inflater, container, savedInstanceState);
}
}

and its xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment2">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/th2"/>

</LinearLayout>

Then in MainActivity , you can set its xml as follow :

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

<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="80dip"
android:orientation="horizontal">

<Button
android:id="@+id/buttonone"
android:layout_width="150dip"
android:layout_height="80dip"
android:text="FragmentOne"/>

<Button
android:id="@+id/buttontwo"
android:layout_width="150dip"
android:layout_height="80dip"
android:text="FragmentTwo"/>

</LinearLayout>

<RelativeLayout
android:id="@+id/containerView"
android:layout_width="match_parent"
android:layout_height="430dip">

</RelativeLayout>

</LinearLayout>

Finally , in MainActivity.cs implement this function :

public class MainActivity : AppCompatActivity
{
Android.Support.V4.App.Fragment fragmentOne;
Android.Support.V4.App.Fragment fragmentTwo;
Android.Support.V4.App.FragmentTransaction fragmentManager;
[Obsolete]
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

Button buttonone = FindViewById<Button>(Resource.Id.buttonone);
buttonone.Click += Buttonone_Click;

Button buttontwo = FindViewById<Button>(Resource.Id.buttontwo);
buttontwo.Click += Buttontwo_Click;
fragmentOne = new Fragment1();
fragmentTwo = new Fragment2();

fragmentManager = SupportFragmentManager.BeginTransaction();
fragmentManager.Add(Resource.Id.containerView, fragmentOne);
//adding first fragment when entering activity
fragmentManager.Commit();
}

private void Buttonone_Click(object sender, System.EventArgs e)
{
//throw new System.NotImplementedException();
Console.WriteLine("Buttonone_Click");
fragmentManager = SupportFragmentManager.BeginTransaction();
// replace to be the first fragment
fragmentManager.Replace(Resource.Id.containerView, fragmentOne);
fragmentManager.Commit();
}

private void Buttontwo_Click(object sender, System.EventArgs e)
{
//throw new System.NotImplementedException();
Console.WriteLine("Buttontwo_Click");

fragmentManager = SupportFragmentManager.BeginTransaction();
// replace ro be the second fragment
fragmentManager.Replace(Resource.Id.containerView, fragmentTwo);
fragmentManager.Commit();
}
}

The effect as follow :

Sample Image

How do I change/replace fragment on button press?

If I am not mistaken you simply forgot to commit your fragment transaction.

Try adding a

ft.commit();

right after your

ft.replace(R.id.fragment_container,new fragment2());

How to move from one fragment to another fragment on button's click in android?

Use Fragment transaction to replace your second fragment with third fragment on button click,
For example,

Fragment thirdFragment = new ThirdFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, thirdFragment);
transaction.addToBackStack(null);
transaction.commit();

Hope it helps!

How to open a Fragment on button click from a fragment in Android

You can replace the fragment using FragmentTransaction on button click. Something like this:

  Fragment someFragment = new SomeFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();

Learn about performing fragment transactions here.

code:

  package com.rupomkhondaker.sonalibank;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment implements View.OnClickListener {

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);

aboutBtn.setOnClickListener(this);
phonebookBtn.setOnClickListener(this);

return rootView;
}

@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.aboutusButton:
fragment = new AboutFragment();
replaceFragment(fragment);
break;

case R.id.phbookButton:
fragment = new PhoneBookFragment();
replaceFragment(fragment);
break;
}
}

public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}

}

Add and replace one fragment with another on button click

Setting background color to White in parent layout fixed the issue.



Related Topics



Leave a reply



Submit