Android listview item display text over image using array adapter - android

I have overloaded the getView method of listview to populate custom style of list item.
This is what currently it looks like:!!
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).
<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.

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.

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.

Related

Why my TextView under an ImageView is not showing

I am writing an Android game. In the level selection activity's layout file, I want to layout the levels' buttons (They are actually ImageViews) like this:
x x x
x x x
And each level button has a TextView, with that level's name as the text, below it (Let's call these two views together as a "level choice"). I used a lot of LinearLayouts to do this. Here is the code for a level choice:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/angles"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/angles_level"
android:textSize="#dimen/level_text_size"/>
</LinearLayout>
As you can see, the two views' height and width are all wrap_content. But when I look at the designer, the text view doesn't show up.When I select the text view in the component tree, it shows where the text view is:
P.S. The picture isn't showing all six levels because I haven't made them yet.
As you can see, the text view is right at the bottom! When I select the ImageView, it shows that it is occupying all the space of its parent!
I don't know why this is happening, my image is certainly a square! Can you explain why this is happening and how do I fix it?
If you need my whole layout code, feel free to tell me in the comments.
For me, the best solution is to position and size it properly by code (where you have total control) instead of xml.
Anyway, i think your problem can be solved by setting ImageViews ScaleType
imageView1.setScaleType(ScaleType.FIT_START);
By XML:
android:scaleType="fit_start"
Hope this helps.
I use background color for textview when I'm studying the layout.
If you use wrap content in both dimension for TextView, that is invisible since you did not write any text inside it. wrap content means that the view take the minimum space. And no text means 0px; try to set ImageView and TextView with layout_weight 1 and layout_height 0dp. In this way both view take half of space of parent layout
Because right now, your LinearLayout doesn't know how to distribute the ratio of its children. And in fact, your imageview's wrap content already
consumes the whole space.
So, LinearLayout says "Sorry TextView, you have no space left".
Use layout_weight to both of the children.
I guess you want to have your picture twice the size of your text.
2:1
That is,
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=2
android:src="#drawable/angles"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=1
android:text="#string/angles_level"
android:textSize="#dimen/level_text_size"/>
</LinearLayout>
I just realized that I posted a question about ImageViews leaving out too much whitespace:
LinearLayout leaving out too much white space. Why?
I think this is the same as that problem. So I tried setting adjustViewBounds to true in the xml. And it works! Now the image view look like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/parallel_lines"/>
You can use relative layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/angles"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/angles_level"
android:textSize="#dimen/level_text_size"/>
</RelativeLayout>
or simple you can set background of textview to that image by putting this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/angles_level"
android:background="#drawable/angles"
android:textSize="#dimen/level_text_size"/>

Row layout for a listView with image view and text view

I'm using a listView with a custom row layout to show an image and some text on a single line. At the moment I fix the height of the image and the text and I'm using a relative layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/rowimage"
android:layout_width="55dp"
android:layout_height="55dp"
android:src="#drawable/foo" />
<TextView
android:id="#+id/rowlabel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignTop="#+id/rowimage"
android:layout_toRightOf="#+id/rowimage"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="25sp" />
In some devices using 25sp can happen that I've got a two line text and it could be ok for me but the text is not readable because the 55dp for the height is not enough and it's fixed. So I think there are different solutions:
Use a dynamic height for the image for auto resize depending on the height of the textView. In this way the height for the row is ok for the text and for the image, but how can I do it?
use different layout for different layout screen size, using a different text size
Use a smart approach to set a dynamic size of the textview to avoid multi line
I'd like to use the first one, any tips?

Adjust parent layout in a gallery to remote image's width after it has been loaded

I have a gallery item which consists of a RelativeLayout containing an ImageView and a TextView. (see my masterpiece of drawing below)
Upon start of the activity one gallery item takes up all the space of the screen making the image look lost on the left side and the textview (blue banner) way too long. I want to have several items next to each other depending on the image width.
The image is being replaced after loaded from a server so I can't know how wide the image will be exactly when I start/layout the activity.
I now want to adjust the RelativeLayout's width to the width of the image after it has been loaded and placed into the view.
I already played around with the getLayoutParams() method inside the ListAdapter for the Gallery but this leads to ANRs and no result at all.
Here's the layout for my gallery item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_wrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/item_margin"
>
<ImageView
android:id="#+id/item_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
/>
<TextView
android:id="#+id/item_name"
android:layout_width="fill_parent"
android:layout_height="#dimen/item_title_height"
android:layout_alignParentBottom="true"
android:text="Some longer title so you can see how the text behaves"
android:singleLine="true"
android:gravity="left|center_vertical"
android:paddingLeft="10dip"
android:background="#drawable/title_gradient_grey"
/>
</RelativeLayout>
Any help how to relayout after the image has been inserted?
you should resize image after downloading from server.This is necessary coz the image you are downloading may be of much bigger size and set property for ImageView as
<ImageView ...
android:scaleType="fitXY" this would lead you to scaling.
other option is
<ImageView ...
android:scaleType="center" if you don't want scaling.

Why is my Android list item background image displaying with a dark overlay?

This is my Android list_item layout XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="?android:attr/scrollbarSize"
android:background="#drawable/list_bg">
<TextView
android:id="#+id/tv_displayname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
This is the list_bg image:
and yet here is what it looks like when shown in Android emulator:
I can see that the background is being applied because there is a slight gradient effect to it, but I have no idea where this black overlay is coming from!
have you tried a different image to see if that one does the same thing? to me it looks like it is creating the image behind the list in your RelativeLayout and your textview background is on top. since you are setting it to fill the width of the row, i bet if you just set to wrap_content you would see it behind there
try putting the image as your TextView background instead

Gallery space at beginning and end

I got the following problem , i made a form with a gallery, the gallery instead of containing images contains items from one of my classes, everything inside each item of the gallery displays perfectly. I removed the space between images using:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="#+id/galleryid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spacing="0dip"
android:padding="0dip"
android:layout_weight="1" />
the items of the gallery:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="75dip" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:background="#ffffff">
<TextView android:id="#+id/frame_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="-" android:textSize="12dip" android:textColor="#ffffff" android:background="#000000" android:gravity="center" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal">
<TextView android:id="#+id/frame_shot1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="18dip" android:textColor="#000000" />
<TextView android:id="#+id/frame_shot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="18dip" android:textColor="#000000" />
</LinearLayout>
<TextView android:id="#+id/frame_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="---" android:textSize="38dip" android:textColor="#000000"/>
</LinearLayout>
buuuuuuuuuuuuuuuuut , i got a problem, there is some blank space at the beginning and end of the gallery with no items. the thing is that my gallery has many items on it that u can actually scroll horizontally, but i wanna get rid of those spaces so the very first thing on the very left of the gallery is the first item, and the very last when u scroll to the very right is the right item.
Edit 08/16 Still with the same problem back in the project, here i leave an image of exactly what is what I'm trying to get rid of that is the black space at the beginning (also is at the end of the gallery at the other side)
The reason you get the blank space on the left is simply because there are no more items to display to the left of the first item (centred in the view). I would assume that the same problem arises to the right of the last item as well.
To properly solve this one will have to decide on what to display when there are no more items, but I will give some ideas:
(1) Increasing the width of the gallery items to the same width as the gallery will only show one item at a time, thus the user will never be able to scroll to the empty space.
(2) To display a solid color or an image in the blank space I would assume setting the android:background attribute of the gallery.
(3) Create a buffer in the beginning and end of your adapter, consisting of the last/first items respectively, and when the user scrolls far enough in the buffer, jump to the matching item at the other end of the gallery for a infinite "carousel" gallery.
Try:
int position=1;
void android.widget.AbsSpinner.setSelection(int position)
One thing you can try is to check if there's more than one item. If there is, set the selection on the gallery to be the second item.
Your probably talking about the fading edge length.
http://developer.android.com/reference/android/view/View.html#setFadingEdgeLength(int)
Try
setFadingEdgeLength(0);
Or
setHorizontalFadingEdgeEnabled(false);
Try changing your layout for the gallery width to fill parent as well instead of wrap content:
<Gallery android:id="#+id/galleryid"
android:layout_width="fill_parent"
set the margin of the gallery to -(metrics.widthPixels)+itemWidth, the blank space is hide.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(-(metrics.widthPixels)+itemWidth,0,0,0);
gallery.setLayoutParams(params);
I've just spent with this issue 2 hours and finally found a solution for myself. I have a Gallery, with custom LinearLayout elements. I want to display just 1 element a time without any padding on the start nor on the end.
After many tries I found that, if I set the minWidth parameter of every Item to a big number (android:minWidth="1000dp") my problem is solved. Don't know if it is suitable for your issue. Good Luck
Peter B.

Categories

Resources