Listview Header Wrap_content causing weird layout - android

I have a listview with header attach using method addHeaderView(header);
same listview layout loading different data for same fragment depdens upon navigation drawer category.
header is having its own layout
{
**R.layout.header_layout**
<?xml version="1.0" encoding="utf-8"?>
<com.loopj.android.image.SmartImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/desc"
android:scaleType="centerCrop"
android:baselineAlignBottom="true"
android:src="#drawable/blankk" />
}
Note: SmartImageView is library that loads image from url .
my problem is the, images that are loading into smartimageview (listview header) are not having fixed height so the layout for different fragment are different i.e not consistent .if i assign height as say 300dp images are getting crop.
plz help me to get get header of same height with images not getting crop.

Please remove android:baselineAlignBottom="true" from your smart imageview and use fix width. I hope it will works

Related

Android Studio - change background image size

So, i wanted to put an image in background of my screen. My problem is that if i just add the image to "background" it will fill all the screen, and my image gets defaced. I could put as an ImageView, but the problem is that i have an expandableListView in the same screen, and i wanted the image to stay behind, as a background.
Any idea how to do this?
Can have 2 solutions: Put as an imagemView but behind the list. Or add as a background but resize it. How can i do it?
Here is what happened:
He is what happens if i put the image as an imageView:
Yes this is very easy to achieve with relative layouts
<RelativeLayout ... >
<ImageView ...
android:layout_alignParentTop="true"
android:scaleType="centerCrop“ />
<ListView ...
android:layout_alignParentTop="true“ />
</RelativeLayout>
With a relative layout z-index is decided by the order you declare the tags, so stuff towards the bottom of the xml is in front of stuff from closer to the top, when they are in the same position.
Use a FrameLayout (Inside FrameLayout view written on top will appear on top and so on)
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
<ImageView
android:layout_width="your_width_in_dp"
android:layout_height="your_height_in_dp"
/>
</FrameLayout>
You can also place FrameLayout inside another layout.

Loading the correct Image in List View

I'm making an app that displays images from urls in a listview. I use a asynctask to get the image and place it into the imageviews. The way I currently have it is that an asynctask is called in the getview method. The do in background part gets the image from the url. The problem I'm having is when I scroll to an element in the listview, initially the wrong image is there but the correct image is quickly loaded. Why is this happening?
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Make sure that you are setting your ImageView to null as soon as you declare it. It's hard to tell without knowing what the image is, but it's possible that the ListView is reusing old images. Reference below:
wrong images are displaying first and then correct images are displaying during scrolling of listview

ViewPager on Android 4.3 missing content

I have a ListView each item of which is ViewPager with some content (mostly images). Everything works fine bot not on Android 4.3 and above ViewPager's content is missing and ViewPager not scrollable. I don't know which part of source code to provide here - ask me and I'll try to provide you maximum info.
EDIT: Found out that ViewPager in my case has zero dimensions. But only on Android 4.3+.
Here is part of my layout:
<...AdaptableHeightImageView
android:id="#android:id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/slide"
android:visibility="invisible"
tools:ignore="ContentDescription" />
<...UninterceptableViewPager
android:id="#+id/list_item_showcase_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#android:id/icon"
android:overScrollMode="never" />
When I set ViewPager's width and height 100dp for example it becomes visible.
The UninterceptableViewPager is ViewPager subclass with locked X-axes. The AdaptableHeightImageView is ImageView subclass which fits the setted width but stretches it's height to save aspect ratio. The goal is to have ViewPager with the same dimensions that ImageView has.
If I write something like this:
<...UninterceptableViewPager
android:id="#+id/list_item_showcase_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#android:id/icon"
android:layout_alignLeft="#android:id/icon"
android:layout_alignRight="#android:id/icon"
android:layout_alignTop="#android:id/icon"
android:overScrollMode="never" />
Then once my Views inside ViewPager or parent ListView are recycled they never appear again. Also in this case any ViewPager content which wasn't shown when data array is set will never be shown. But ViewPager will have dimensions and I even able to scroll it. Seems like Google changed something in Android 4.3.
In conclusion: everything works fine only if I set concrete width and height

android gridview cell size

In my gridview, all the cell initially render as rectangles, but the second time it is viewed they are square.
I have to logic to make them square, but I want them square the first time they are loaded
my problem is that I don't understand why the views are initially rectangles, or any particular proportion at all!
my XML files are all set to fill parent, and I even put in a square placeholder image in an attempt to force the views to be rendered square
is there something about gridview I should know about?
create a custom array adapter.(java)
see ( Showing graphical instead of strings in an imagelist with title )
in custom array adpater xml file (cell) write something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="50dp" >
<ImageView
android:id="#+id/iv_caarow"
android:src="#drawable/icon"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
android:layout_width="50dp", you can set cell size like this.
The answer was to extend my ImageView within the gridview cell, and #Override the onMeasure method and dynamically generate square proportions there

Android crop background

I have a LinearLayout with width set to fill_parent and height to wrap_content. Now I want to give it a background from a png file, but doing it in a traditional way causes the LinearLayout to resize in order to show the whole background instead of cropping the additional part.
How can I set the background of LinearLayout so it won't resize?
Thanks
It appears that such thing is achievable only using the ImageView and setting the scaleType parameter accordingly. A quick workaround is to use FrameLayout and put the ImageView under another layout with transparent background.
Code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background" />
</FrameLayout>
The default behaviour of background bitmaps is to fill the container completely and grow the horizontal and vertical size of the container if needed. The solution is to create a drawable bitmap resource and changing the gravity.
Check the following link and look for the possible gravity values. Your view should then just reference the drawable resource and you should be fine.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Bitmap
I solved this one by subclassing LinearLayout and overriding the onMeasure(int,int) callback, then updating the layout XML to use my new layout type. I'm not at liberty to post that exact code, but what I did was invoke the super.onMeasure(int,int) implementation and check whether the measured height came back as the exact height of my background image. If so, I grab the measured height of all child views, then calculate a new height value and pass it to setMeasuredDimension(int,int).
put this code in the xml activity, for example activity_main.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/image" />

Categories

Resources