-2

so I'm trying to learn to do stuff with Android apps, and I have an ImageView named (id) "img_BarHealth" that i initialize to be "160dp".

I have made a button and hooked it up to this function:

public void onBack(View view) {
    //Get link to the object.
    ImageView bar_Health = (ImageView) findViewById(R.id.img_BarHealth);

    //This is what I want done. (However this value is not accessable it seems)
    bar_Health.width = "80dp";
}

So, any suggestions? =)

Aleksander Fimreite
  • 1,409
  • 1
  • 16
  • 28
  • I think it's already here what you need: http://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically – Balazs Varhegyi Sep 05 '13 at 20:22

2 Answers2

0

Use getLayoutParams().width

ImageView bar_Health = (ImageView) findViewById(R.id.img_BarHealth);
    bar_Health.getLayoutParams().width = 80;
Joe Rakhimov
  • 4,203
  • 8
  • 44
  • 101
0

You have to call getLayoutParams().width and then you need to convert dip to pixel size:

public int convertDipToPixels(float dips)
{
    return (int) (dips * getResources().getDisplayMetrics().density + 0.5f);
}

Something working would be close to:

bar_Health.getLayoutParams().width = convertDipToPixels(80);
Community
  • 1
  • 1
gunar
  • 14,580
  • 7
  • 55
  • 86