Behy
Behy

Reputation: 483

put buttons on an image which looks the same in different displays

I have a map and wanna put some Buttons on it as pointers. Here is my

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/karte"
    android:background="@drawable/my_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/btnSP1"
        android:layout_width="@dimen/pinpoint_size"
        android:layout_height="@dimen/pinpoint_size"
        android:scaleType="fitXY"
        android:background="@drawable/pinpoint"
        android:tag="001"
        android:layout_marginLeft="50px"
        android:layout_marginTop="90px"
        android:onClick="spClick"/>
    <ImageButton
        android:id="@+id/btnSP2"
        android:layout_width="@dimen/pinpoint_size"
        android:layout_height="@dimen/pinpoint_size"
        android:scaleType="fitXY"
        android:background="@drawable/pinpoint"
        android:tag="002"
        android:layout_marginLeft="100px"
        android:layout_marginTop="250px"
        android:onClick="spClick"/>
  </RelativeLayout>

I have set layout_margin in px or dp or progremmatically in percentage of the display_size:

        RelativeLayout.LayoutParams param =(RelativeLayout.LayoutParams)(getActivity().findViewById(id[i])).getLayoutParams();
        param.setMargins((int)(xFactor[i]*displySize.x),(int)(yFactor[i]*displySize.y),0,0);

but in all of these methods Buttons change their position when I use different display size.
Is there any idea how to fix Buttons to make their position independent of the display size.

Upvotes: 0

Views: 40

Answers (1)

baderkhane
baderkhane

Reputation: 307

Normally DP or DIP means in android Density-independent Pixels so if you attribute a position to an object obviously it will be fixed.
From your code i see that you should get the equal of your values in DP with this way.

RelativeLayout.LayoutParams param =(RelativeLayout.LayoutParams(getActivity().findViewById(id[i])).getLayoutParams();
param.setMargins(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(int)(xFactor[i]*displySize.x), context.getResources().getDisplayMetrics()),TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(int)(yFactor[i]*displySize.y), context.getResources().getDisplayMetrics()),0,0);


I hope that will help you

Upvotes: 1

Related Questions