How to implement onClickListener method in Fragments?

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Jan 28, 2017 7:45:38 PM via Website

I'm new to Android development and I'm developing my first Android app about music that contains two fragments: Home Fragment and Genres Fragment. This app is a school project and it's kinda urgent.

In Genres Fragment, I have four ImageButtons and I want to add some action to them, like when clicking a button, it goes to another fragment

So, in the Java file of that fragment, I already have the code for OnClickListener but I don't know what to put in the case condition of each button.

public class GenresFragment extends Fragment implements View.OnClickListener{


public GenresFragment() {

    // Required empty public constructor
}


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

    ImageButton rapBtn = (ImageButton)v.findViewById(R.id.RapButton);
    ImageButton popBtn = (ImageButton)v.findViewById(R.id.PopButton);
    ImageButton edmBtn = (ImageButton)v.findViewById(R.id.EDMButton);
    ImageButton rockBtn = (ImageButton)v.findViewById(R.id.RockButton);
    rapBtn.setOnClickListener(this);
    popBtn.setOnClickListener(this);
    edmBtn.setOnClickListener(this);
    rockBtn.setOnClickListener(this);
    return v;
}

@Override
public void onClick(View v) {
        switch (v.getId()) {
            case R.id.RapButton:

            break;
            case R.id.PopButton:

            break;
            case R.id.EDMButton:

            break;
            case R.id.RockButton:

            break;
        }
}

}

Can you help me with this, please? I have Googled it for hours and found nothing.
Like I said, this is urgent.