0

I am trying put a button in my android activity. I am creating it with xamarin.android and (not xamarin.forms) and I cant seem to position the button where I like.

My code for button looks like this which allows me to put the button where I want.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<AbsoluteLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frameLayout1">
    <ImageView
        android:id="@+id/myImage"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/my_image" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gotologin"
        android:layout_x="125px"
        android:layout_y="482px"  />
</AbsoluteLayout>

</RelativeLayout>

However, when I change the screen size to another emulator, the position changes. I am obviously doing something wrong but I cant seem to find a way to keep the size and position of the button same for all screen size.

Please, can anyone point me to right direction of the part of the documentation i need to read to do this? Thanks.

Screenshot

Screen size one

screen size two

Note the button position changed.

Mill3r
  • 544
  • 1
  • 10
  • 31
  • Have you tried using `dp` rather than `px` for your `layout_y` and `layout_x` attributes? – pushasha Oct 12 '18 at 20:54
  • Yes I have tried that. It still changes position for different screen size. – Mill3r Oct 12 '18 at 20:58
  • Can you describe what you mean by "changes position"? Perhaps provide screenshots? – pushasha Oct 12 '18 at 21:00
  • I have added the screenshot to the question. – Mill3r Oct 12 '18 at 21:07
  • You are using `px` for x/y, thus hardcoding the starting point, so yes, screens of different resolutions will cause your button to move : https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp – SushiHangover Oct 12 '18 at 21:16
  • same thing happening for all the units. I suppose the problem is with the layout and not the units. The lack of proper and complete documentation is so frustration for xamarin.android app development. The link you gave is most probably about the size of the button and not the positioning. – Mill3r Oct 12 '18 at 21:25
  • @Mill3r Google has thousands of pages of docs that are all relevent to Xamarin.Android, personally I would not use AbsoluteLayout as it has been deprecated since API 3, using a framelayout or a relativelayout can provide the same functionality – SushiHangover Oct 12 '18 at 21:59
  • Thanks for letting me know Google has documents. But I couldn't find any relevant once hence I posted here. I'm still working on it and trying to play around relative layout. Thanks . – Mill3r Oct 13 '18 at 05:28

0 Answers0