Skip to content Skip to sidebar Skip to footer

How To Fit My Layout To Any Screen In Android

I'm building my own application game for Android, it's a Tic Tac Toe game. my activities contains mostly ImageViews , all the images placed in the dir res\drawable-mdpi ONLY. the r

Solution 1:

Are you using RelativeLayout?

You should 1. use RelativeLayout as the base container 2. put the "Player" and "System" score inside the same horizontal LinearLayout so that they won't overlap 3. Put the "End game" button as an element which align parent bottom

The screenshots you attached shows that it's a problem caused by the soft status bar. RelativeLayout would help and you should try to prevent hard-coding the dimensions in numbers but making the auto-adjust in RelativeLayout using margins, "layout_toLeftOf", "layout_toRightOf" and etc.

Solution 2:

Create a drawable folder, just the word "drawable", in res and put your game images in there. That will make the game images the same across every device density.

Without looking at your XML, I would agree that you're probably using hardcoded dp values instead of sizing the layout with math, ie, weights, match_parent, alignParentRight, etc.

I have adjusted your XML to be easier. I did not plug it into a previewer or anything.

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/board_page_bg"android:gravity="center" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:layout_marginLeft="ADJUST ME"android:layout_marginRight="ADJUST ME"android:background="@drawable/board"android:orientation="vertical" ><!-- Scoreboard --><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:layout_marginRight="5dp"android:layout_marginTop="5dp" ><TextViewandroid:id="@+id/players_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginTop="2dp"android:layout_alignParentLeft="true"android:text="@string/playersName"android:textSize="30sp" /><TextViewandroid:id="@+id/players_score"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="150dp"android:layout_marginTop="5dp"android:layout_toLeftOf="@id/players_name"android:text="@string/playersPoints"android:textSize="25sp" /><TextViewandroid:id="@+id/machines_score"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="172dp"android:layout_marginTop="5dp"android:layout_toRightOf="@id/machines_name"android:text="@string/machinePoints"android:textSize="25sp" /><TextViewandroid:id="@+id/machines_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="50dp"android:onClick="openChat"android:layout_alignParentRight="true"android:clickable="true"android:text="@string/machineName"android:textSize="30sp" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/block1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_1_empty" /><ImageViewandroid:id="@+id/block2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_2_empty" /><ImageViewandroid:id="@+id/block3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_3_empty" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/block4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_4_empty" /><ImageViewandroid:id="@+id/block5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_5_empty" /><ImageViewandroid:id="@+id/block6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_6_empty" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/block7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_7_empty" /><ImageViewandroid:id="@+id/block8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_8_empty" /><ImageViewandroid:id="@+id/block9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="touch"android:src="@drawable/block_9_empty" /></LinearLayout><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_marginTop="ADJUST ME" ><TextViewandroid:id="@+id/game_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="@string/gameNumber"android:textSize="25sp" /><TextViewandroid:id="@+id/count_down"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="@string/countDown"android:textSize="25sp" /></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="20dp"android:orientation="horizontal" ><ImageViewandroid:id="@+id/end_game"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/EndGame"android:onClick="endGame"android:src="@drawable/endgame_btn_1" /></LinearLayout></LinearLayout>

Post a Comment for "How To Fit My Layout To Any Screen In Android"