siwarak
siwarak

Reputation: 197

How to setOnclick Listener to button in fragment

I have button in fragment1. I want to click this buttton and replace fragment1 by fragment2 but my app still close when I try to run it

This is my code in fragment1

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

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

     // gallery = (Gallery) rootView.findViewById(android.R.id.list);
      //gallery.setAdapter(new ItemAdapter(generateData(), getActivity()));
      Button button = (Button) rootView.findViewById(R.id.imageButtonAll);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
        public void onClick(View v) {
              HomeFragment homeFragment = new HomeFragment();
              FragmentTransaction transaction = getFragmentManager().beginTransaction();
              transaction.replace(R.id.frame_container, homeFragment);
              transaction.commit();

        }       

        });     

      return rootView;       

        }  

  } 

Upvotes: 1

Views: 191

Answers (2)

justin shores
justin shores

Reputation: 695

The problem is that you are trying to replace a view that does not exist within the view that you are inflating. You have to switch these fragments from your FragmentActivity, which has the contentview containing the layout you are trying to replace.

MainActivity:

class MainActivity extends FragmentActivity {
    public Fragment frag1;
    public Fragment frag2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        frag1 = new Frag1();
        frag2 = new Frag2();
        //assumption to switch to frag 1 immediately
        switchToFragment(R.id.frame_container, homeFragment);

        ...
    }

    /** Switch UI to the given fragment (example) */
    void switchToFragment(Fragment newFrag) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newFrag)
            .commitAllowingStateLoss();
        currentFragment = newFrag;
    }
}

Fragment:

....

final Activity activity = getActivity();
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            about.setTextColor(Color.WHITE);

            if(activity instanceof MainActivity) {
                ((MainActivity) activity).switchToFragment(((MainActivity) activity).frag1));
            }
        }
    });

...

Upvotes: 1

W3hri
W3hri

Reputation: 1591

You cant open a fragment from inside another fragment. So you have to move your code from the onClick to your activity and run it there.

In your Activity (lets assume its MainActivity):

public void openMyFragment(){
    HomeFragment homeFragment = new HomeFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, homeFragment);
    transaction.commit();
}

And then, add this in your fragments onClick:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ((MainActivity) getActivity()).openMyFragement();
    }

});

Upvotes: 2

Related Questions