-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 ?

3
  • 1
    Please add your code Commented Aug 31, 2016 at 7:35
  • Please share the code you already have, and if possible a (simple and small) image of what you want to achieve. That way it's easier for the rest of us to help you. Commented Aug 31, 2016 at 7:35
  • The two codeblocks you've added are the same. Could you include the 'main_guilds_item' xml layout? Commented Aug 31, 2016 at 8:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.