2200

Android - ImageView from URL won't fill_parent in ListView

I have a JSON, I am parsing the JSON. It results with text and an image in a listview. Everything works fine except one thing. I am trying to get the ImageView to fill_parent as width and wrap_content as height. This doesn't work..

This is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/id" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/created_at" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/title" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/loves" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/created_at" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/comments" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/loves" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/hypes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/comments" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/hypes" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/byline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/name" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:id="@+id/iv_flag" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/byline" />

And if you need to, this is my Java to show the Image in Listview: http://pastebin.com/N0j5gjPE

Can anyone help me?

Answer1:

Try with this:

<ImageView android:id="@+id/iv_flag" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:adjustViewBounds="true" android:scaleType="fitCenter"/>

From the doc:

android:adjustViewBounds Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Try changing scaleType="fitCenter" for "centerInside"

if that doesn't work, try this custom ImageView.

public class AspectRatioImageView extends ImageView { public AspectRatioImageView(Context context) { super(context); } public AspectRatioImageView(Context context, AttributeSet attrs) { super(context, attrs); } public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); setMeasuredDimension(width, height); }

}

and replace ImageView for

<com.package.AspectRatioImageView android:id="@+id/iv_flag" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:adjustViewBounds="true" android:scaleType="fitCenter"/>

Answer2:

Can You try add this attribute to imageView

android:scaleType="fitXY"

It will look like

<ImageView android:id="@+id/iv_flag" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="@drawable/abs__ab_bottom_solid_light_holo" android:scaleType="fitXY" android:layout_below="@+id/byline" />

Recommend

  • How to link text in a TextView to open web URL
  • SeekBar - Showing progress with TextView above
  • android listpopupwindow list item textview not being multiline
  • How to change the font of tabLayout with a custom font / calligraphy
  • Android EditText - Stop cursor blinking, want a solid cursor
  • Edittext requestFocus() dynamically not working
  • Overlapping ImageView in RelativeView
  • Android custom dialog linearlayout size same as dialogs bg image
  • Background image crashes android
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Android Custom dialog inflating from layout - alignment issue
  • EditText ignores the ImeActionLabel
  • C++ Armadillo Access Triangular Matrix Elements
  • Toolbar shifts up, when soft keyboard appears
  • Binary Tree Traversal Sum Of Each Depth
  • Small video playback
  • changes in jquery 1.4.2 breaking the code?
  • Gray line inside cardview :)?
  • Selecting a subset of data in ServiceStack.OrmLite
  • jquery code not working without breakpoint
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • saving file generated by TCPDF
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • d3 v4 drag and drop with TypeScript
  • What is Eclipse's Declaration View used for?
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • trying to dynamically update Highchart column chart but series undefined
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal