1

I have overloaded the getView method of listview to populate custom style of list item.

This is what currently it looks like:!!

enter image description here

Red color is to just show the background of listview item

Here I have used 3 controls: Imageview for image, 2 text views (one top, one bottom).

Problem here is the height of listview item is getting increased. I want two textviews over the image not above or below. To someextent I know the root cause of this problem but don't know how to fix it.

Here is the layout of listview item:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"   >

  <ImageView android:id="@+id/imgIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp" 
    android:scaleType="fitCenter"/>

   <TextView android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:textStyle="bold"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="2dp"
    android:layout_alignLeft="@+id/imgIcon"
    android:layout_alignTop="@+id/imgIcon"
    android:layout_alignBottom="@+id/imgIcon"/>

   <TextView android:id="@+id/txtTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:textStyle="bold"
    android:textSize="18sp"
    android:textColor="#FFFFFF"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="5dp"
    android:layout_alignLeft="@+id/imgIcon"
    android:layout_alignTop="@+id/imgIcon"
    android:layout_alignBottom="@+id/imgIcon"/>

The problem is i'm using png image for Imageview and their size is getting increased in some aspect ratio. In case I hard code the height of listview item layout then, specifically on my phone screen size, things start appearing good but on some different screen size things get totally worse (which makes sense too).

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="70dp"
android:padding="5dp"   >

Would be great if someone can explain about how to do it. Main problem is to display text over an image (and image is not same). Thanks.

Update: My major problem is I don't have the bit vector of image instead a PNG and it tries to scale up the image. (setting scale type to center-fit could be an option but would it work on background resourcE?. Here is the output if I set image as a background for listview item and two texts over it. enter image description here

Rohit
  • 6,365
  • 14
  • 59
  • 90
  • 1
    Have you thought about creating a layout file for your view, setting your image as background, and just placing your textviews where you want them? – confused_at_times Apr 12 '13 at 16:32

2 Answers2

1

If you're using solid colors as the backgrounds, and you intend to support a variety of screen sizes, I would think it might be easier to use a background color fill on the text views, with the text views stacked and aligned such that there's no borders. Then it would not have any stacking issues (a simple Linear Layout would work) and it would be scalable with no processing of images.

Tim C
  • 635
  • 5
  • 18
1

Ok, when calling getView() in your CustomArrayAdaptor, rather than set the ImageView bitmap to the image you have (what you are currently doing, I believe), instead call View.setBackgroundResource(R.drawable.yourimage.png) on your view after you have infalted it. The equivalent in xml is below:

android:background="@drawable/yourimage".

If you need more information, let me know.