0

This is a java code for an image

package com.example.ananduamenon.kindel3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class ToReader extends Activity {
    ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.book_covers );
        imageView = (ImageView) findViewById( R.id.book_cover51 );
        imageView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent( ToReader.this, com.example.ananduamenon.kindel3.Reader.class );
                startActivity( intent );
            }
        } );

    }

}

The XML code for this activity is

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/book_cover51"
    android:layout_width="100dp"
    android:layout_height="160dp"
    android:layout_margin="5dp"
    android:contentDescription="@string/todo"
    android:scaleType="fitXY"
    android:src="@drawable/book_cover"
    tools:ignore="RtlHardcoded,RtlSymmetry"
    tools:context=".ToReader"/>

This code is working when ToReader code is run as MAIN

But it's not working when I include this XML code is included in the main activity.

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
    android:id="@+id/horizontal_scrolls"
    xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>
    <include layout="@layout/book_covers"/>

</LinearLayout>
</HorizontalScrollView>

Click option is not working now. Is there any requirement when adding XML code while adding with option. The java code is not running in the background at all.

4 Answers4

0

When you use include, basically you're telling the layout you include is a template, and then you would have multiple instances of it. So, the template can't have an id, and you should instead use different ids for the instances of it, and refer them from runtime code. There are better ways to accomplish this, but this is why your actual code doesn't work:

<include android:id="@+id/myId1"
         layout="@layout/book_covers"/>
<include android:id="@+id/myId2"
         layout="@layout/book_covers"/>
Alessio
  • 3,063
  • 1
  • 15
  • 17
  • Nop not working. I have given the id to every include that I use. But the issue still exist – anandu Menon Aug 31 '18 at 06:14
  • that works. Of course you need to use such ids to retrieve your layouts. Try with including 2 images only, find those 2 view with 2 different ids, set 2 click listeners and output / debug the results. – Alessio Aug 31 '18 at 07:59
0

If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes). You can show Multiple Items in RecyclerView Horizontally or Vertically.

Follow these URLs:

RecyclerView Example

Show RecyclerView Horizontally

Official Documentation of RecyclerView

scienticious
  • 438
  • 1
  • 7
  • 22
  • The issue is with the click option not working. And I have also included the same image in a card view but suffer from the same problem. – anandu Menon Aug 31 '18 at 06:17
  • Correct me if i am wrong, You are using two XMLs the first One is Showing all imageView & the Second one has One ImageView, Could you please Show the name of Your Layout files – scienticious Aug 31 '18 at 06:21
  • nope. There is only one image view. I'm using to add it to everywhere I need. – anandu Menon Aug 31 '18 at 07:58
  • What is your layout file name in which you are including **book_covers.xml**? – scienticious Aug 31 '18 at 11:09
0

You have mentioned only one id ref. for multiple views in your layout does it make sence?

imageView = (ImageView) findViewById( R.id.book_cover51 );

android will be confused in which view setOnclickListner you want to do. I will recommend keeping separate id for all of this

<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/>
<include layout="@layout/book_covers"/> 

like in this way

<include layout="@layout/book_covers" 
  android:id="@+id/book_cover1"/> 

<include layout="@layout/book_covers" 
  android:id="@+id/book_cover2"/> .... 
Arnab Kundu
  • 1,287
  • 2
  • 12
  • 17
0

Got the answer. The include won't be including the java file but just the XML code. So I wrote the code in the main activity instead of a different class. Then it worked.

Everyone Thank You for the support and suggestions.