Reputation: 113
I am making a game and while my UI is very well organised in my Preview Device it gets messed up in the Emulator device and thats because of the different screen size ! I tried to achieve to have an organised UI programmatically , meaning that i firstly measure the screen size and depending on how big or small it is i create buttons,images,textviews that are for this size...
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn_params=new RelativeLayout.LayoutParams(40,40);
btn_params.rightMargin=0;
btn_params.bottomMargin=0;
btn1.setLayoutParams(btn_params);
waterlily_params=new RelativeLayout.LayoutParams(layout.getHeight()/3,layout.getHeight()/3);
waterlily_params.leftMargin=layout.getWidth()/2;
waterlily_params.bottomMargin=0;
iv2.setLayoutParams(waterlily_params);
frog_params=new RelativeLayout.LayoutParams(iv2.getHeight()/2,iv2.getHeight()/2);
frog_params.leftMargin=layout.getWidth()/2;
frog_params.bottomMargin=0;
iv1.setLayoutParams(frog_params);
iv2.setVisibility(view.VISIBLE);
iv1.setVisibility(view.VISIBLE);
btn2.setVisibility(view.GONE);
btn1.setVisibility(view.VISIBLE);
}
});
but seriously the above doesnt seem right and it doesnt work So my question is how to make a UI that will match every screen ?
here is the xml of the specific activity :
im using xml files ! Here is the xml file of the specific activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.catchthefly.MainGame"
android:clickable="true"
android:id="@+id/MyLayout"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:id="@+id/waterlily"
android:background="@drawable/water_lily"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="invisible" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/frog"
android:background="@drawable/f132"
android:layout_gravity="bottom"
android:layout_alignBottom="@+id/waterlily"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tap Here To Start Your Game"
android:id="@+id/StartingButton"
android:enabled="true"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#00000000" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/MovingObj"
android:background="#ffff00e1"
android:layout_above="@+id/StartingButton"
android:layout_toLeftOf="@+id/frog"
android:layout_toStartOf="@+id/frog"
android:visibility="invisible" />
</RelativeLayout>
Upvotes: 0
Views: 122
Reputation: 24991
Did you read http://developer.android.com/guide/practices/screens_support.html?
Did you consider to use a game engine https://github.com/nicolasgramlich/AndEngine ? It take care about properly rendering on various devices and a lot of other stuff.
Upvotes: 0
Reputation: 1268
You need to use xml layout folder. Do you need to create your layout programmatically or can you use xml files? You should create xml resources and inflate them.
Upvotes: 1