How to implement onClickListener method in Fragments?

  • Replies:2
Guilherme Rodrigues
  • 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.

Sandra Hdes

Reply
RoryOlden
  • Forum posts: 1

Aug 30, 2022, 3:48:11 PM via Website

In regards to your project, music is a popular topic that's sure to interest a lot of people. By developing an app about music, you'll be able to provide valuable information for students and other interested parties. Not only that, but your app will also be unique and original - something that's hard to come by these days. So keep up the good work - we're excited to see how it develops!

Reply
Sandra Hdes
  • Forum posts: 5

Aug 31, 2022, 9:21:10 AM via Website

Replace the existing Fragment with the new Fragment and push the transaction to the back of the stack to solve your issue. This keeps the functionality of the button. Making a new Activity is incredibly counterproductive and actually destroys the purpose of using fragments.
1. Expand app > java in the Project: Android window and choose com.
2. Select File > New > Fragment > Fragmentation (Blank).
3. Give the fragment the name SimpleFragment in the Configure Component dialogue.
4. De-check the boxes next to Include interface callbacks and Include fragment factory methods.

Reply