mike20132013
mike20132013

Reputation: 5425

OnLongClickListener is not working as it's supposed to work

The code is very simple.. Nothing great I am just trying to toast a message and for some reason the behavior is different in two cases.

Case 1 : This is working:

Here's the code:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webopen);


            TextView sometext = (TextView)findViewById(R.id.click);
            sometext.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(getApplicationContext(), "Long click Working", Toast.LENGTH_LONG).show();
                    return false;
                }
            });


            }

Case 2: Not working! I want to know the reason why it's not working

public class OnClick extends Activity implements OnLongClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webopen);

        TextView sometext = (TextView) findViewById(R.id.click);

    }

    @Override
    public boolean onLongClick(View v) {

        switch (v.getId()) {
        case R.id.click:
            Toast.makeText(getApplicationContext(), "Long click enabled",
                    Toast.LENGTH_LONG).show();
            // OR
            // Something();
            break;

        default:
            break;
        }

        return false;
    }

    public boolean Something() {

        Toast.makeText(getApplicationContext(), "Long click enabled",
                Toast.LENGTH_LONG).show();
        return true;
    }
}

There's nothing great about the code but I just want to know the reason for onLongClickListener not working.

Any suggestions will help ..:)

Upvotes: 1

Views: 1228

Answers (3)

Raghunandan
Raghunandan

Reputation: 133580

Case 2 missing setOnLongClickListener for sometext.

TextView sometext = (TextView) findViewById(R.id.click);  
sometext.setOnLongClickListener(this); // missing

In the first case you used a annonymous inner class.

In the second your class implements the interface OnLongClickListener

Upvotes: 2

Phil
Phil

Reputation: 36299

You are not setting the onLongClickListener for someText. You need to add this:

someText.setOnLongClickListener(this);

This line will attach the listener object to the TextView Object. In the first case, you make the call to setOnLongClickListener, which is why it works. In this instance, you are creating a new onLongClickListener object in-line.

In the second example, your Activity is inheriting OnLongClickListener, which makes it an instance of that object. So to set the listener for someText, pass the listener this, which is a reference to the current class.

Upvotes: 2

Endzeit
Endzeit

Reputation: 5524

You don't register a onLongClick listener for the TextView. Add the following line in your codes onCreate and it should work.

sometext.setOnLongClickListener(this);

Upvotes: 2

Related Questions