Reputation: 127
I am simply using a PNG image as source in ImageView
& want to add a onClickListener()
. Problem is that button's onClick()
doesn't trigger smoothly, means when I click on image sometimes it works fine & sometimes it doesn't work. Even I have added some padding around ImageView to increase the clickable area. I also tried replacing ImageView
with ImageButton
but no luck. That is also behaving same way.
Here is my code:
ImageView back = (ImageView) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
finish()
}
});
XML for button:
<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:padding="10dp"
android:src="@drawable/btn_back"/>
Please note that I am using this ImageView
in a custom action bar.
Upvotes: 0
Views: 283
Reputation: 124
XML layout
<ImageView android:id="@+id/back"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_close_black_24dp" />
here is class
public class About_us extends AppCompatActivity {
ImageView back;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
back = (ImageView) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
finish();
}
}); }
}
Upvotes: 0
Reputation: 124
without code it's will impossible to find your problem , because it's not happens in any case, you can check by remove padding otherwise,I'm not seeing any problem in this xml
Upvotes: 0
Reputation: 10244
Already your image size is too low, 24dp
along with that you're giving extra padding to it 10dp
which results in a smaller image hence the click is not obtaining correctly.
Try removing the padding and use margin
if possible
<ImageView
android:id="@+id/back"
android:layout_width="24dp"
android:layout_height="24dp"
android:scaleType="fitCenter"
android:src="@drawable/btn_back"/>
Or you can add a container layout which can wrap around the `ImageView`` and give click listener to that.
Upvotes: 0
Reputation: 224
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Upvotes: 2