Click and hold the Button / Listview

Asked

Viewed 4,496 times

1

How to program to click and hold down call a method other than just a normal click?

I would like tips, tutorials something that can help me.

I’m having a new problem using the Onitemclick method and the onItemLongClick method have already implemented onItemClick and I’m in doubt how to implement also the

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity().getBaseContext(), inalcancaveis_tela.class);
        startActivity(intent);}

I call this method on onCreateView

list.setOnItemClickListener(this);

NOTE: to using extends Fragment

3 answers

10


If you used the interface View.OnClickListener to set an action for the button (using the method setOnClickListener), you can use the OnLongClickListener also.

View.OnLongClickListener() may help you implement this system.

View view = ...;

view.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        //....
    }
});

It is important to return true if you treated the event by returning false will allow the event of Click be called.

  • @Walkin you unintentionally answered a question of mine (I did not get to ask here on the site) that I left as a pending of my project. I handle a click event and a long click event, only when I used the long click the click event was also called. I didn’t know it was because of the return false or true. Thank you very much. + 1 for your reply.

  • 1

    It is... These return parameters of events are very complicated, I’ve had several problems with it. I’m glad you helped.

3

Button myButton = (Button) rootView.findViewById(R.id.button);
myButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        System.out.println("Clique simples");
     }
     });

myButton.setOnLongClickListener(new View.OnLongClickListener() {
     @Override
     public boolean onLongClick(View v) {
        System.out.println("Clique longo");
        return false;
     }
     });

1

All subclasses of View (TextViews, ImageViews, etc.) have a method setOnLongClickListener() which receives as a parameter an implementation of View.OnLongClickListener. Now just search for examples on Google.

In the particular case of item lists (ListViews), if you want to apply long click to a list item, you should call setOnItemClickListener(), which receives an implementation of AdapterView.OnItemClickListener.

  • But , and for me to put the sharing event on this button with longclicklistener?

  • Dude, I haven’t touched Android in a while so I can’t help you. But throw a new question in the stack that will surely be answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.