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.

13
  • Instead of Adding same layout inside HorizontalScrollView why don't use a RecyclerView? Commented Aug 31, 2018 at 5:46
  • 2
    that is probably because now you have multiple ImageViews with id book_cover51 in your layout (9 to be accurate) Commented Aug 31, 2018 at 5:49
  • I'm new to android and I didn't think about it. And this horizontal scroll is not the main activity, but I used include to add the horizontal view multiple times in the main activity Commented Aug 31, 2018 at 5:50
  • Kindly write your XML files names Commented Aug 31, 2018 at 5:51
  • Can you paste the entire xml of book_covers.xml layout file pls Commented Aug 31, 2018 at 5:52

4 Answers 4

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"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Nop not working. I have given the id to every include that I use. But the issue still exist
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.
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

4 Comments

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.
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
nope. There is only one image view. I'm using <include> to add it to everywhere I need.
What is your layout file name in which you are including book_covers.xml?
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"/> .... 

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.