-1

I'm developing an Android app with two fragments. In one of the fragments, I have four ImageButtons and I want to add some ation to them.

So, in the Java file of that fragment I already have the code for OnClickListener but it's not working.

Java file with errors

halfer
  • 19,824
  • 17
  • 99
  • 186
Guilherme R
  • 525
  • 1
  • 4
  • 11

2 Answers2

1

It should be

public class GeneresFragment extends Fragment implements View.OnClickListener

The OnClickListener is a an interface of the View class (ImageView extends View).

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
0

You have to use View.OnClickListener like this

  public class felan extends Fragment implements View.OnClickListener {

    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
    }
}
  • I wrote view.onclicklistener and it's working but when I click a button. it does nothing – Guilherme R Jan 03 '17 at 16:15
  • This is my onClickListener code: [Imgur](http://i.imgur.com/cvxKbzb.jpg) – Guilherme R Jan 04 '17 at 12:06
  • Well you wrote nothing in your case blocks. It just check the conditions, nothing more. You can see if a condition works by using 'Toast' or 'Log' in them. Write "Toast.makeText...". For toast guide you cab use this post: http://stackoverflow.com/a/20542766/5780236 – Ahmad Reza Enshaee Jan 04 '17 at 12:21
  • But Toast.makeText is for showing text in a small popup and I don't want that. When clicking a button, I want that button to open a fragment – Guilherme R Jan 07 '17 at 17:55
  • I meant if you want to test click events you can use toasts. Forr opening fragments unfortunatly i can not help too much becase i haven't worked with them. – Ahmad Reza Enshaee Jan 07 '17 at 18:18