I have a list view which displays news items.
Each news item may / may not contain an image.
If it does contain an image, cool!
If it doesn't contain an image I set the image placeholder to invisible, and resize the frame layout it is contained inside.
I will show an image of my list view first:
As you can see the news item with the image is working as it should. But the news item without the image is not !!.
In my XML Layout file I have the Blog/News Text View height set to 30dp.
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
So, when no image exists I run the following code:
if(no image is loaded)
nimage.setVisibility(View.GONE); // set image placeholder to hidden
// change frame layout height to 30dp (size of News/Blog)
flayout1.getLayoutParams().height = (int)convertPixelsToDp(30, getContext());
flayout1.requestLayout();
where my convertPixelsToDp()
function is like this:
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
This code should work,... I think. But clearly, as you can see above, the height is not set right. i.e - some of the TextView is cut off.
The thing is, when I run on a smaller screen, it works pretty well, but shouldn't it be screen size independent (using dp)... Maybe my conversion function is faulty?
Any help is hugely appreciated !
Use wrap_content as layout height of your TextView. This should fix your issue.