Normal and long click only work after first clicking ImageView

0

I have a problem that when I click on the image, the first time nothing happens, only after the first click that the simple click function works 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 first time, not after clicking.

    
asked by anonymous 16.09.2018 / 17:54

1 answer

0

Coming this question , it seems clear to me what is happening. Let me try to reproduce the flow of your application:

You have set a android:onClick="clickimagem" in your ImageView, which when it is clicked calls 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 it was consumed and click event normally, but in it there is no action other than setOnClickListener(...) and setOnLongClickListener(...) therefore, from now on, your ImageView has another OnClickListener. That's why you need 2 clicks to show 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 click image (View v) of your activity

    
17.09.2018 / 00:10