3

I want to setOnClickListener to my ImageView which is in a ViewPagerItemXML.

In my activityXML I have ViewPager and several other buttons. In my ViewPagerItemXML I have clickable ImageView. I want to make some action after clicking to ImageView in activity. Unfortunately OnClickListener does not trigger at all.

LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.viewPager_item, null);

onThemePhotoClick= (ImageView)itemView.findViewById(R.id.themePhoto);
onThemePhotoClick.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          Log.i(TAG,"clickOnPhoto!");
     });

EDIT

My Activity XML

LinearLayout
        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:orientation="vertical"
        android:weightSum="10">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/iconTools">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_navigate_before_black_36dp"
            android:background="@android:color/transparent"
            android:id="@+id/backButton"
            />
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_weight="8"
         android:id="@+id/viewPagerLayout">

        <android.support.v4.view.ViewPager
        android:id="@+id/visitMuseumPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/art_work_item" />
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/bottomButtonToolbar">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/qrScannerButton"
        android:text="QrCode" />
</LinearLayout>

    </LinearLayout>

And the viewPagerItem

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fillViewport="true">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/WelcomeScreenDarker"
            android:id="@+id/imageBackground"
            />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:id="@+id/themePhoto"
                android:clickable="true"
                />
        </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/artWorkCounter"
        android:textColor="@color/searchHint"
        android:textSize="12sp"
        android:layout_marginRight="80dp"
        android:layout_gravity="end" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/artWorkDescriptionTitle"
           android:paddingTop="15dp"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_margin="10dp"/>

        <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/artWorkDescription"
            android:layout_margin="10dp"
           />
    </LinearLayout>
</ScrollView>
    </LinearLayout>

EDIT 2 I solve my problem by adding OnClickListener into the viewpagerAdapterClass. It probably trigger on another view. If somebody know how to trigger button for the actual activity class please give me a hint

Expiredmind
  • 788
  • 1
  • 8
  • 29

3 Answers3

2

It's normal that the view is triggered in the adapter and not in the acvitivity.

The hierarchy that gets inflated in your Activity contains only the ViewPager, which contains the different ViewPagerItems with the help of the Adapter.

The idea is that you need to pass that click event back to the activity. To do that, create an Interface callback, have your Activity implement it and call that callback from your Adapter.

Many questions on SO will help you. Call Activity method from adapter

Community
  • 1
  • 1
JDenais
  • 2,956
  • 2
  • 21
  • 30
0

you can try to this changes in your activity ..

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:id="@+id/themePhoto" />

and in class..

onThemePhotoClick=(ImageView)itemView.findViewById(R.id.themePhoto);

onThemePhotoClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "You click " + position, Toast.LENGTH_LONG).show();
        }
    });
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
  • You must define your click listener before adding it. It won't compile like this. – JDenais Oct 06 '16 at 12:35
  • I dont understand, the imageView is setting in another layout. I should implement the same image View in my activity layout in viewPager part? – Expiredmind Oct 06 '16 at 12:39
0

make ImageView as final

final ImageView onThemePhotoClick= (ImageView)itemView.findViewById(R.id.themePhoto);

using final you can click view which you want to click

Mahesh Gawhane
  • 348
  • 3
  • 20