Normal and long click only work after a first click on Imageview

Asked

Viewed 80 times

0

I am with a problem that when I click on the image, the first time nothing happens, only after the first click it works simple click function and long click.

XML code:

<ImageView 
android:id="@+id/idimagem" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:onClick="clickimagem" 
android:src="@drawable/imagem" />

JAVA code:

public class MainActivity extends Activity { 
ImageView idimagem; 
[.....] 
public void clickimagem(View v) { 
idimagem= (ImageView) findViewById(R.id.idimagem); 
idimagem.setOnClickListener(new View.OnClickListener() { 
@Override public void onClick(View v) { 
Toast.makeText(getApplicationContext(), "Click! ", Toast.LENGTH_SHORT).show(); 
idimagem.setOnLongClickListener(new View.OnLongClickListener() { 
@Override public boolean onLongClick(View v) { 
Toast.makeText(getApplicationContext(), "LongClick! ", Toast.LENGTH_SHORT).show(); 
return true; 
} 
}); 
}

I need to make it work right away, not after I have to click.

1 answer

0

Coming of that question, It seems clear to me what’s going on. Let me try to reproduce the flow of your application:

You have set a android:onClick="clickimagem" in your Imageview, which when clicked calls the clickimagem(View v)

public void clickimagem(View v) { 
    idimagem= (ImageView) findViewById(R.id.idimagem); 
    idimagem.setOnClickListener(new View.OnClickListener() { 
        @Override public void onClick(View v) { 
            Toast.makeText(getApplicationContext(), "Click! ", Toast.LENGTH_SHORT).show(); 
        } 
    }); 
    idimagem.setOnLongClickListener(new View.OnLongClickListener() { 
        @Override public boolean onLongClick(View v) { 
            Toast.makeText(getApplicationContext(), "LongClick! ", Toast.LENGTH_SHORT).show(); 
            return true; 
        } 
    }); 
}

And was consumed and click event normally, but in it there is no action other than setOnClickListener(...) and setOnLongClickListener(...) so, from now on, your Imageview has another Onclicklistener. That’s why you need 2 clicks to show the Toast.

1 - Remove android:onClick

<ImageView 
    android:id="@+id/idimagem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/imagem" />

2 - In the onCreate method, set the click listeners

@Override
protected void onCreate(Bundle savedInstanceState) {
    // [...]
    idimagem = (ImageView) findViewById(R.id.idimagem);
    idimagem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Click! ", Toast.LENGTH_SHORT).show(); 
        }
    });

    idimagem.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(getApplicationContext(), "LongClick! ", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

3 - Remove the public void clickimagem(View v) from your Activity

  • It didn’t work, the application crash when starting. Probably because I added this on onCreate

Browser other questions tagged

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