Reputation: 4780
I have a layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/logo_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logos_background">
</ImageView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_alignBottom="@+id/logo_background"
>
<TextView
android:id="@+id/logoDetails"
android:text="Test Logo details"
android:textSize="15sp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/stockQuantity"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:layout_below="@+id/logoDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x10"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I need to include this layout multiple times in another layout which I have done as follows:
<HorizontalScrollView
android:layout_below = "@+id/selectLayout"
android:id="@+id/pager"
android:scrollbars="none"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
<include layout="@layout/viewpager_item_layout"/>
</LinearLayout>
</HorizontalScrollView>
This gives the the following result:
The problem is I need to access the text views and image views in each <include>
. So the logo image will be different the test logo details will be different and the 'x10' will be different for each one as I hope to populate them from a database. Could anyone shine a light on this?
It had been my intention to use a view pager for this but that doesn't work as it takes up the whole width and height of the screen.
Upvotes: 4
Views: 2737
Reputation: 18509
<include android:id="@+id/news_title"
layout="@layout/viewpager_item_layout"/>
Try if it helps.
You can access like this :
View includedLayout = findViewById(R.id.news_title);
TextView insideTheIncludedLayout = (TextView )includedLayout.findViewById(R.id.logoDetails);
Upvotes: 5
Reputation: 5220
after setting layout to include
tags , in your code use this :
View v = findViewById(R.id.your_layout_id);
TextView tv = (TextView) v.findViewById(R.id.text_view_id);
Upvotes: 1