Yoav
Yoav

Reputation: 645

listviewitem issue with imageview on android

I want to create a listviewitem in a listactivity. The listviewitem will be a textview and an image.

But I have a case that there is no image to put in the imageview I will want the textview to take the whole listviewitem.

I do not want to use different XML layouts for these cases because I have same issue but with 3 images (n of the 3 can be shown).

I need some sample code for dynamic adding image views....

Also - I am using an adapter for this list...

Upvotes: 0

Views: 77

Answers (2)

user1034443
user1034443

Reputation: 9

if(v==null)
{

    v= generateView(Activity.this);

}

LinearLayout generateView(Context context){

    LinearLayout lay = new LinearLayout(context);       

    TextView tv = new TextView(context);

    lay.AddView(tv);

}

Upvotes: 0

Chiara
Chiara

Reputation: 1887

I have no time to post code, but you could do something like this:

  1. Create a Item List Layout as a Relative o LinearLayout with your TextView and your ImageView
  2. in your Adapter.getView you should inflate your Item List Layout and then set the ImageView's visibility to GONE whenever you don't have an image.

EDITED:

Here is one layout to use as your itemLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Upvotes: 1

Related Questions