Yehonatan
Yehonatan

Reputation: 3225

Resize single ImageView inside ListView

I need to change an ImageView based on a condition, the problem is that ImageView sizes can be change only via ImageView.getLayoutParameters().width/height, doing so will change the width of each and every item inside the ListView.

Is there a way of changing the size of a single ImageVIew? As for now I can't find a way of doing it.

Upvotes: 2

Views: 446

Answers (2)

ArbenMaloku
ArbenMaloku

Reputation: 558

Here is my custom adapter. I can re-size images by setting params to imageView. Hope this will help you!

p.s The worst thing is that in this way you have to create your view each time! It's more slowly than using viewHolder.

public class Adapter extends BaseAdapter {

ArrayList<String> list = new ArrayList<String>();
Activity activity;

public Adapter(Activity activity) {
    this.activity = activity;
    list.add("Item 1");
    list.add("Item 2");
    list.add("Item 3");
    list.add("Item 4");
    list.add("Item 5");
    list.add("Item 6");
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {


    LayoutInflater inflater = activity.getLayoutInflater();
    convertView = inflater.inflate(R.layout.adapter,null);

    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);


    textView.setText(list.get(i));
    //Condition
    if (i == 5)
    {
        setLayoutParams(imageView,40,40);
    }
    else {
        setLayoutParams(imageView,70,70);
    }

    imageView.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_launcher));
    return convertView;
}



private View setLayoutParams(View view, int width, int height)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
    view.setLayoutParams(params);
    return  view;
}
}

Upvotes: 1

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

You have to write if else condition

if(yourcondtion)
{
setlayoutparams();
}

else
{
setoriginallayoutparams();
}

Upvotes: 0

Related Questions