user1400538
user1400538

Reputation: 865

android onClick for imageview not fired

I have a common layout which I use in all my activities. In the title layout, I have an imageview included and there is an onclick method specified in the xml layout for this imageview.

In one of my activities, the onclick for this imageview is not fired in the java code. Following is the layout of the activity I refer to:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/bottom_layout_main"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:padding="5dp" >

    <RelativeLayout
        android:id="@+id/btn_layout_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/exit_createprofile"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/custom_button"
            android:onClick="onCreateExitClick"
            android:text="Exit"
            android:textSize="14dp" />

        <Button
            android:id="@+id/save"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/exit_createprofile"
            android:background="@drawable/custom_button"
            android:onClick="onCreateSaveClick"
            android:text="Save"
            android:textSize="14dp" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/top_layout"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_weight="1"
    android:background="@color/light_grey"
    android:gravity="top"
    android:orientation="horizontal" >

    <include
        android:id="@+id/includeTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/titlebar_layout" />
</LinearLayout>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="40dp" >

    <br.com.dina.ui.widget.UITableView
        android:id="@+id/profileList_create"
        style="@style/UITableView"
        android:layout_height="wrap_content" />
</ScrollView>

and the layout specified in include is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/list_item_top"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/background_grey"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgBack"
    android:layout_width="28dp"
    android:layout_height="28dp"
    android:onClick="onBackClick"
    android:src="@drawable/arrow_back" />

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:textColor="@android:color/black"
    android:textStyle="bold" 
    android:textSize="18sp" />



</LinearLayout>

I have even tried, using the following code in the activity, but still, the onclick is not fired:

backImage = (ImageView)layout.findViewById(R.id.imgBack);
    backImage.setClickable(true); 
    backImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Title back clicked", Toast.LENGTH_SHORT).show();
        }
      });

I am left with no clues as to why this onclick is not getting fired. Any help is much appreciated.

Here is my screen:

enter image description here

Upvotes: 0

Views: 2138

Answers (3)

Paresh Mayani
Paresh Mayani

Reputation: 128458

Add android:clickable="true" in your ImageView inside XML layout.

OR

backImage.setClickable(true); 

Upvotes: 2

Pragnani
Pragnani

Reputation: 20155

If your imageview is in included layout than try this

LinearLayout layout=(LinearLayout)findViewById(R.id.includeTitle);

And then

backImage = (ImageView)layout.findViewById(R.id.imgBack);

Upvotes: 1

Jithu
Jithu

Reputation: 1478

Please try this

backImage = (ImageView)findViewById(R.id.imgBack);
backImage.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Title back clicked", Toast.LENGTH_SHORT).show();
    }
  });

Upvotes: 1

Related Questions