Dynamic positioning of a Button [Android] -
i have relativelayout holds imageview , imagebutton. imageview serves container background image. i'm trying set button @ fixed position appears on same position on background image.
here layout file i'm using:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_height="match_parent" android:layout_width="match_parent" android:gravity="center_horizontal"> <imageview android:src="@drawable/bg_1" android:id="@+id/imgview" android:adjustviewbounds="true" android:layout_height="match_parent" android:layout_width="match_parent" /> <imagebutton android:layout_width="120dp" android:background="@drawable/button_1" android:layout_height="30dp" android:id="@+id/imgbutton" android:layout_marginleft="100dp" android:layout_margintop="170dp" /> </relativelayout> as can see i've tried positioning button it's left-/top-margin using dp unit, doesn't work. since background image beeing scaled down/up, position have dynamic in kind of way.
i understand absolute positioning, pixel-values x-/y-position, won't work on android, explained here. still need solve , not sure how.
would have calculate values left-/top-margin (not sure how be) , set them this?
final float density = getresources().getdisplaymetrics().density; int width = (int)((float)120 * density); int height = (int)((float)120 * density); relativelayout.layoutparams rlp = new relativelayout.layoutparams(width, height); rlp.leftmargin = newmargin; rlp.topmargin = newtopmargin; imagebutton imgbutton = (imagebutton) findviewbyid(r.id.imgbutton); imgbutton.setlayoutparams(rlp); hope didn't forget ...
//edit: thinking, reason issue might be, scaled image has different "borders", depending on screen size. image @ 1:1.6 ratio on hvga screen have black bars on left , right, whereas on wvga screen bars on left. considering i'm using default scaling. , post again, if necessaray...
why scaling happens? because of different dpi on different devices? have different drawabled different dpi settings? if isn't dpi issue , want scale background image freely can't job using standard layouts. should implement custom one.
Comments
Post a Comment