elL
elL

Reputation: 777

Android ImageView dynamic width and height

I have a horizontal list of images loaded from a url. So i have an ImageLoader, then an Adapter then my Activity. My xml layouts looks like this:

mainlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

  <com.devsmart.android.ui.HorizontalListView
      android:id="@+id/hlist_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"
      android:overScrollMode="always" > 
  </com.devsmart.android.ui.HorizontalListView>

</LinearLayout>

Then my list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:orientation="vertical"
android:translationY="200dp" > <!-- make this dynamic -->

    <FrameLayout 
        android:id="@+id/mainlayout" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" >

       <ImageView
        android:id="@+id/prof_pic"    
        android:clickable="true"
        android:adjustViewBounds="true"
        android:layout_width="360dp" 
        android:layout_height="360dp" />

    <ProgressBar
        android:id="@+id/img_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        </FrameLayout>      

    <TextView
        android:id="@+id/sname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/sdesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

With this layout, the result is Figure A. The gray broken line is the imageView, we can't actually see it but only those images(green box). What i want to achieve is the one in Figure B.

enter image description here

So, to somewhat solve this i tried adding in my imageView

                  <ImageView
                android:id="@+id/prof_pic"    
                android:clickable="true"
                android:adjustViewBounds="true"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:maxWidth="360dp"
                android:maxHeight="360dp"
                android:minHeight="240dp"
                android:minWidth="240dp"
                 />

But what happened is that, by the time i open the application, my image list looks like Figure A. But when a scroll along the images, it adjusted to somewhat look like Figure B. But when i scroll back to the first image, only the first have of the image will only be show or sometimes, my image list starts at my second image. This is really crazy.

Is there any way where i can achieve Figure B without destroying my list of images or at the start of the app, it already displays like Figure B?

EDIT:

This is how my Adapter looks like:

public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);



    if(convertView == null){
        convertView = inflater.inflate(R.layout.list_layout, null);
        holder = new ViewHolder();
        holder.txtShopName = (TextView) convertView.findViewById(R.id.shop_name);
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
        holder.progBar = (ProgressBar) convertView.findViewById(R.id.img_progress);


        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }



    imageLoader=new ImageLoader(context.getApplicationContext(), holder.progBar);

    SRowItem sItem = (SRowItem) getItem(position);

    holder.txtName.setText(sItem.getShopName());
    holder.txtDesc.setText(sItem.getShopDesc());
    imageLoader.DisplayImage(sItem.getImageUrl(), holder.imageView);

    return convertView;
}

Upvotes: 1

Views: 7747

Answers (1)

lokoko
lokoko

Reputation: 5803

Try something like this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);

ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image, null);
imageView.setLayoutParams(params);

Upvotes: 2

Related Questions