2

On my code below would've been nice if the item has a picture. but if it is empty then the item picture looks empty box the size 70dp. I would like if the item does not contain an picture, then automatically the empty box filled with TextView, so if it is empty then the picture like no ImageView, only TextView. I tried to set the ImageView width size andoid:layout_width="wrap_content" it worked with what I want, but the size of the picture was disheveled. There are wide and small. therefore I set it so the 70 to make it look flat.

This is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/logo_article"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip" />

    <TextView
        android:id="@+id/title_article"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/logo_article"
        android:gravity="center_vertical"
        android:text="Title Article" />

</RelativeLayout>

I get items from database and I store to simple adapter, code in java:

String[] from = { "title", "logo" };
int[] to = { R.id.title_article, R.id.logo_article };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),listkatdb,R.layout.per_item_list, from, to);
kategoriListView.setAdapter(adapter);
adapter.notifyDataSetChanged();

Thanks.

Dave Jackson
  • 837
  • 5
  • 19
  • 28
  • 1
    SimpleAdapter cannot custom view. I suggest don't use SimpleAdapter. –  Feb 12 '13 at 07:44

5 Answers5

1

Don't fix your image view size. change image width as wrap_content. If image have means show test right of image. don't have image means image width 0dip text automatically comes left side. try this

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
    android:id="@+id/logo_article"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip" />

<TextView
    android:id="@+id/title_article"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/logo_article"
    android:gravity="center_vertical"
    android:text="Title Article" />

    </RelativeLayout>
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
  • 1
    If u have image means text will come right side of you imageview. If u don't have image means text come left side. This is you issue right – MuraliGanesan Feb 12 '13 at 08:02
0

set Visibility of ImageView to GONE if empty

Intathep
  • 3,378
  • 2
  • 21
  • 28
0

Use logo_article.setVisibility(View.GONE); if no content to show. thats it.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
0

If there is no image, you need to set ImageView's setVisibility to GONE, then TextView will fill whole width.

if(isImageAvailable)
{
    iv.setVisibility(View.VISIBLE);
}
else
{
    iv.setVisibility(View.GONE);
}
Santhosh
  • 1,962
  • 2
  • 16
  • 23
  • Hi thanks for reply. I store it to simple adapter. I have edit my post, please check. Thanks. – Dave Jackson Feb 12 '13 at 07:32
  • You need to implement custom adapter's getView() to check the image is available or not. Have a look at the following links to customize. http://www.ezzylearning.com/tutorial.aspx?tid=1763429 http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Santhosh Feb 12 '13 at 08:53
0

Programmatically when you find out there is no image set visibility of ImageView to GONE

 ImageView img = (ImageView)findViewById(R.id.logo_article);
 // logic here to find image
 //if no img
 img.setVisibility(View.GONE);
baboo
  • 1,983
  • 17
  • 23