Fragment implements OnClickListener

64,983

Solution 1

Just do one this

public class fragmentOne extends Fragment implements OnClickListener {
    Button myButton;

    @Override
    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
        View myView = inflater.inflate(R.layout.fragment_1, container, false);
        myButton = (Button) myView.findViewById(R.id.myButton);
        myButton.setOnClickListener(this);
        return myView;
    }

    @Override
    public void onClick(View v) {
        // implements your things
    }
}

very simple

Solution 2

I will Focus to use the OnClick action for global access, You have to do like this is your project, Must Implement the View.OnClickListener, then Override the Method OnClick(), In OnCreateView() have to do like this button_submit.setOnClickListener(this); for the Views you need, Please see the below code for Clear Answer,Thankyou.

public class New_Project extends Fragment implements View.OnClickListener{
private View mView;
private Button button_submit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_newproject, container,false);
    button_submit=(Button)mView.findViewById(R.id.button_submit);
    button_submit.setOnClickListener(this);
    return mView;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_submit:
            //do your stuff
            break;
    }
}

}

Solution 3

I want to comment on Abhijit Chakra answer but it seems that I need to have 50 reps for that. For those who are wondering if you can't use Abhijit's answer, it is because of:

public void OnClick(View v) {
    // implements your things
}

You need to make sure that it is onClick, NOT OnClick. Thankfully Android Studio internal error message come to rescue.

Solution 4

view.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    switch (v.getId()) {
      case R.id.imgView1:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      case R.id.imgView2:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      case R.id.imgView3:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      default:
        break;
    }
Share:
64,983
Nick
Author by

Nick

Hi, I'm Nick Ver Voort. I'm a software engineer at Black Duck Software, used to spend a lot of time developing Android apps for fun, and I write and play music in my free time under the name Perfect Inversion

Updated on July 05, 2022

Comments

  • Nick
    Nick almost 2 years

    I've got an application that I'm modernizing. One step of this process is changing to a Fragment based layout (using the Fragments from the support library). I converted my Activities into Fragments, and got the layout working nicely (using a ViewPager, cool stuff!)

    I was having my Activities implement OnClickListener for all of my button-pressing needs. I have the new Fragment incarnations doing the same thing of course, but it looks like "onClick" is never getting hit. Is there something special about Fragments that prevents them from working this way?

  • Krishnadas PC
    Krishnadas PC over 8 years
    Simple things are the most extraordinary. But only the wise are able to see it. Thanks a lot. Was trying to figure it for many hours.
  • nobjta_9x_tq
    nobjta_9x_tq almost 8 years
    I forgot: myButton.setOnClickListener(this); Oh men @@! Wasted my time after long time come back with android. Thanks.
  • Mousa Alfhaily
    Mousa Alfhaily almost 6 years
    That saved my day, Thanks.