4

I have a button wich takes you to a sample picture with a short description, but what i would like to do is to long click and then let it take the user to a website for more information.

here is my code for my button (normal)

    <Button
                    android:id="@+id/samplea"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/samplea_button" 
                    android:longClickable="true"/>

and my java is this

Button next = (Button) findViewById(R.id.samplea);
next.setOnClickListener(new View.OnClickListener() {


        public void onClick(View view) {
            final ImageView imageView = (ImageView) findViewById(R.id.iM2);
            imageView.setImageResource(R.drawable.samplea_draw);

How do I add the longclickable to this to take me to a website? Can anyone please help?

I have added it, but now it seems to take me to the this website (after longclicking), but not to the image (after normal onclick) heres my code:

  next1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            // Launch your web intent
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661"));
            startActivity(intent);
            return true;
        }

        public void onClick(View view) {
            final ImageView imageView = (ImageView) findViewById(R.id.iM2);
            imageView.setImageResource(R.drawable.samplea_draw);

get a yellow line under "public void onClick(View view) {"

Allrounder
  • 685
  • 2
  • 9
  • 20
  • You cannot combine an OnClickListener and OnLongClickListener like this, I updated my answer. – Sam Nov 08 '12 at 22:00

3 Answers3

3

Updated
Implement an OnLongClickListener much like your OnClickListener, but it needs to be separate. Try this:

Button next = (Button) findViewById(R.id.samplea);
next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        // You can turn this into a class variable
        final ImageView imageView = (ImageView) findViewById(R.id.iM2);
        imageView.setImageResource(R.drawable.samplea_draw);
    }
)};
next.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // Launch your web intent
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661"));
        startActivity(intent);
        return true;
    }
});
Sam
  • 86,580
  • 20
  • 181
  • 179
1

You add an on long click listener -

next.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        //your action on long click
        return true;
    }
});

Look here - Android: long click on a button -> perform actions

You will always get better answers with more effort if you Google your question first!

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54
0

See example code below.

next.setOnLongClickListener(new OnLongClickListener() {
   
   @Override
   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    
    Toast.makeText(MainActivity.this,"Button long click", Toast.LENGTH_SHORT).show();
    return true;
   }
  });
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23