Skip to content Skip to sidebar Skip to footer

Set Imagebutton Height And Width In Code

this is my code ImageButton btnPlayVideo = (ImageButton) findViewById(R.id.btnPlayVideo); btnPlayVideo.setBackgroundResource(R.drawable.play_icon_grey); btnPlayVideo.setAdjustViewB

Solution 1:

You will need to use android:scaleType to have it resize to fit.

<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ic_launcher"android:scaleType="fitXY" />

Which produces following output.

enter image description here

Solution 2:

You can do it from code via

android.view.ViewGroup.LayoutParams params = btnPlayVideo.getLayoutParams();
params.height = myHeight;
params.width = myWidth;
btnPlayVideo.setLayoutParams(params);

, but probably your problem lays not in the code, but in layout - I see you are using android:layout_span="2" - does that mean btnPlayVideo is in TableLayout? If so, you must regulate this TableLayout's dimensions, because its children will always have fill_parent width and heights.

Solution 3:

I used metrics:

DisplayMetricsmetrics=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

btnPlayVideo.setMinimumHeight(metrics.heightPixels / (amount));
btnPlayVideo.setMinimumWidth(metrics.widthPixels/ (amount));

Post a Comment for "Set Imagebutton Height And Width In Code"