• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Margin Property of an ImageView in code

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

Heres what i've got:
Code:
	ImageButton ib = (ImageButton) findViewById(R.id.btn_learn);
	MarginLayoutParams mlp = new MarginLayoutParams(ib.getLayoutParams());
	
	if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
	    mlp.setMargins(0, 20, 0, 0);
	else
	    mlp.setMargins(0, 100, 0, 0);
	
	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?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones