1

I have a layout called thumb_unit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_height="240dp"
    android:orientation="horizontal"
     >

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_marginLeft="10dp"
        android:layout_width="160dp"
        android:layout_height="220dp"
        android:scaleType="fitXY"
        android:src="@drawable/app_icon" 
            android:layout_marginTop="20dp"/>

    <LinearLayout
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/issueName"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:text="Summer 2012"
            android:textSize="18dp"
            android:layout_marginLeft="6dp"
             />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="151dp"
            android:layout_marginLeft="6dp"
            android:text="Description"
            android:textSize="14dp"
            android:textStyle="italic" />

        <Button
            android:id="@+id/view_download"
            android:layout_marginTop="5dp"
            android:layout_width="80dp"
            android:layout_marginLeft="5dp"
            android:layout_height="30dp"
            android:text="View"
            android:textSize="14dp" 
            android:textColor="@drawable/push_button_text_color"
            android:background="@drawable/push_button"
            android:layout_marginBottom="40dp"
            />

    </LinearLayout>

</LinearLayout>

and another layout called grid_unit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="170dp"
    android:layout_height="130dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="77dp"
        android:layout_height="105dp"
        android:src="@android:drawable/ic_menu_gallery" 
        android:layout_marginTop="10dp"
        />

    <LinearLayout
        android:layout_width="73dp"
        android:layout_height="120dp"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
         >

        <TextView
            android:id="@+id/issueName"
            android:layout_width="77dp"
            android:layout_marginTop="5dp"
            android:layout_height="30dp"
            android:text="South Louisiana PnK"
            android:layout_marginLeft="3dp"
            android:textSize="11dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/productName"
            android:layout_marginTop="5dp"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_marginLeft="3dp"
            android:text="South Louisiana PnK"
            android:textSize="9dp" />

        <Button
            android:id="@+id/viewButton"
            android:layout_width="70dp"
            android:layout_height="20dp"
            android:text="View"
            android:textSize="9dp"
            android:textColor="@drawable/push_button_text_color"
            android:background="@drawable/push_button"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="3dp"
             />

    </LinearLayout>

</LinearLayout>

and now finally, I have main activity_main.xml, I want to use the combination of these as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="440dp"
    android:orientation="vertical"
    tools:context="com.mirabel.activities.MainActivity" >

    <include layout="@layout/thumb_unit" android:id="@+id/main_thumb"/>
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <include layout="@layout/grid_unit" android:id="@+id/left_grid"/>
        <include layout="@layout/grid_unit" android:id="@+id/right_grid"/>

    </LinearLayout>


</LinearLayout>

sublayout in the activity_main.xml and programatically I want to set the image, text for all these things. How to do that?

Bharath
  • 3,001
  • 6
  • 32
  • 65

1 Answers1

0

You can do it in the Activity file for activity_layout.xml in the following way:

View view = findViewById(R.id.left_grid);
ImageView image = view.findViewById(R.id.thumbImage);
image.setBackgroundResource(R.id.image));    //or whatever you wish to set

TextView text = view.findViewById(R.id.issueName);
text.setText("Whatever text you want to set);

In this way, you can access all the Views in the included layout.

STT
  • 274
  • 5
  • 11
  • @Tahira.. I know how to do it in normal cases. But, here in this case I want to use. This one is bit different. Observe carefully. I am using layout as a sublayout in my main layout. In that, layout there are sub components and I want to access those components. – Bharath Oct 12 '12 at 12:10
  • 1
    Yes Tarunima. I think you didn't see my answer carefully. View view = findViewById(R.id.left_grid); ImageView image = view.findViewById(R.id.thumbImage); When you access ImageView, you need to refer to the previosly found View(view) object and not the Activity object. view.findViewById(R.id.thumbImage); – STT Oct 16 '12 at 06:09