-1

In my App clickable imageview (Android Studio) and I used a PNG image with transparent background, but when I run the app it looks like when I click near the image it's getting clicked, can you help me out !

Here's a screenshot : https://i.stack.imgur.com/AhN1y.jpg

My XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@color/dim_foreground_material_light">


<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_weight="16"
    android:id="@+id/logo"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/logo"
    android:clickable="true"/>

Java Code

package com.example.bibiwars.skills;

import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

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




            ImageView img = (ImageView) findViewById(R.id.logo);
            img.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Clicked!!", Toast.LENGTH_LONG).show();
        }

    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



 }

I don't know why the app always crashes ? how cam i solve this issue?

:::This is the logcat:::

06-24 13:28:09.815 1922-1922/com.example.bibiwars.skills I/art﹕ Not late-enabling -Xcheck:jni (already on) 06-24 13:28:10.998 1922-1953/com.example.bibiwars.skills D/OpenGLRenderer﹕ Render dirty regions requested: true 06-24 13:28:10.999 1922-1922/com.example.bibiwars.skills D/﹕ HostConnection::get() New Host Connection established 0xa6c85570, tid 1922 06-24 13:28:11.042 1922-1922/com.example.bibiwars.skills D/Atlas﹕ Validating map... 06-24 13:28:14.228 1922-1953/com.example.bibiwars.skills D/﹕ HostConnection::get() New Host Connection established 0xa6c856b0, tid 1953 06-24 13:28:14.243 1922-1953/com.example.bibiwars.skills I/OpenGLRenderer﹕ Initialized EGL, version 1.4 06-24 13:28:14.567 1922-1953/com.example.bibiwars.skills D/OpenGLRenderer﹕ Enabling debug mode 0 06-24 13:28:14.728 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:14.728 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:14.887 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 39 frames! The application may be doing too much work on its main thread. 06-24 13:28:20.268 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 322 frames! The application may be doing too much work on its main thread. 06-24 13:28:36.548 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 41 frames! The application may be doing too much work on its main thread. 06-24 13:28:36.644 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:36.644 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:37.712 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 67 frames! The application may be doing too much work on its main thread. 06-24 13:28:41.599 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:41.599 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:52.650 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 644 frames! The application may be doing too much work on its main thread. 06-24 13:29:15.022 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:15.022 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:29:20.624 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:20.624 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:29:31.927 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:31.927 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:35:56.876 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:35:56.876 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 14:20:41.195 2422-2422/com.example.bibiwars.skills I/Choreographer﹕ Skipped 191 frames! The application may be doing too much work on its main thread. 06-24 14:20:53.827 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:20:53.827 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa685b980, error=EGL_SUCCESS 06-24 14:20:54.379 2422-2422/com.example.bibiwars.skills I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread. 06-24 14:21:08.637 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:08.637 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS 06-24 14:21:12.162 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:12.162 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS 06-24 14:21:16.752 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:16.752 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS

Baha Baghdadi
  • 29
  • 1
  • 1
  • 11

1 Answers1

0

If you mean that the area of the square enclosing the cirle, it is bound to happen, to prevent that you will have to specify that you don't want touch over there.

For that do this,

  1. Calculate the X and Y coordinates of the image.
  2. Calculate the width and height of the image.
  3. In onClick, get X' and Y' coordinates of the touch. Calculate

    touch = (X'- X)^2 + (Y'- Y)^2 - (Width/2)^2
    
    if(touch<=0)
        //It's a Click!!
    else
        //It's in the transparent area.
    

But, this will only work for circles. Even if you create a transparent image doesn't the transparent area doesn't receive touch events. Every image is a rectangle.

Mayank
  • 1,364
  • 1
  • 15
  • 29
  • What happened, I had solved it! You accepted the answer. – Mayank Jun 24 '15 at 15:01
  • yeah but after that I couldn't do it alone... I'm still a beginner so can you please show me how to get those X, Y, X', Y', width, height ? then I can surely accept! – Baha Baghdadi Jun 24 '15 at 17:59
  • being a beginner doesn't excuse you to copy codes, use onMeasure. and do some research – Mayank Jun 24 '15 at 18:56