-2

I have created a layout with an ImageView, I want to include it two times with two different ImageView like a ListView with different positions.

this is the ImageView layout

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

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/main_guilds_item"
        android:layout_gravity="center_horizontal" />

    <include
        layout="@layout/main_guilds_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

here i include the layout two times but i want to access those ImageViews with different IDs to set different Images.

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

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/main_guilds_item"
        android:layout_gravity="center_horizontal" />

    <include
        layout="@layout/main_guilds_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

How can I do it ?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
amir
  • 145
  • 1
  • 9

1 Answers1

0

You can create custom view for this purpose. Steps are

  1. Create a xml file of the view my_custom_view.xml

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        />
    </RelativeLayout>
    
  2. Then you need to create a class ItemList that extends RelativeLayout(the parent layout in your my_custom_view.xml)

    public class ItemList extends RelativeLayout {
    
    ImageView mImageView;
    
    public ItemList(Context context) {
    this(context, null);
    }
    
    public ItemList(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    }
    
    public ItemList(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.item_list_content, this);
         mImageView=(ImageView) findViewById(R.id.image_view);
    }
    
    public void setImage(Drawable imageId){
    mImageView.setImageDrawable(imageId);
    }
    }
    
  3. Then you can use this customview in your other layouts as many times as you want like below

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <yourpackage.ItemList
            android:id="@+id/first_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </yourpackage.ItemList>
    
        <yourpackage.ItemList
            android:id="@+id/second_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </yourpackage.ItemList>
    
    </LinearLayout>
    
  4. Now from your class you do

    ItemList firstItem=(ItemList) findViewById(R.id.first_view);
    ItemList secondItem=(ItemList) findViewById(R.id.second_view);
    firstItem.setImage(R.drawable.your_image);
    secondItem.setImage(R.drawable.your_image_2);
    

NOTE: You can also take the image from xml. In that case you will have to use custom attribute. Follow the link for that purpose. https://stackoverflow.com/a/7608739/3975838

Community
  • 1
  • 1
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32