1. Are you ready for the Galaxy S20? Here is everything we know so far!

Margin Property of an ImageView in code

Discussion in 'Android Development' started by totally_insane, Feb 11, 2011.

  1. totally_insane

    Thread Starter

    How can i set the layout_margin property of an ImageButton in code?

    Heres what i've got:
    Code (Text):
    1.     ImageButton ib = (ImageButton) findViewById(R.id.btn_learn);
    2.     MarginLayoutParams mlp = new MarginLayoutParams(ib.getLayoutParams());
    4.     if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    5.         mlp.setMargins(0, 20, 0, 0);
    6.     else
    7.         mlp.setMargins(0, 100, 0, 0);
    9.     ib.setLayoutParams(mlp); // ERROR HERE
    Well, theres an error - classcastException - sure..
    The setLayoutParams wants an "LayoutParams" and im giving a "MarginLayoutParams".
    Well that was finally what I tried.

    Any ideas how i can make that working?

    1. Download the Forums for Android™ app!



Share This Page