Monday, 14 July 2014

Android app supporting screen sizes/densities



I recently wrote a small android app and am having a hard time getting to the layout to scale on other devices. I wrote it for and tested it with my Galaxy S3.


To test it on other devices I had to use AVD's. I use a layout file and provide dimens.xml for each screen density (values-mdpi, values-hdpi etc.)


This only works for the screen size of the AVD I was testing on. If I use an AVD with the same screen density but a different screen size , the layout gets whacky. Am I going to provide a full set of dimensions for every device I want to support?


Such as values-(screen size)-(screen density)-(orientation) with every combination I want to support?


Here is my activity_main.xml layout:



<FrameLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:click="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<click.CustomFontTextView
android:id="@+id/click_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/textView_title_margintop"
android:gravity="center_horizontal"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="@dimen/textView_title_textsize"
click:fontName="freescript.ttf" />

<Button
android:id="@+id/button_newgame"
style="@style/clickMenuButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_newgame_margintop"
android:onClick="newGame"
android:text="@string/button_newgame" />

<Button
android:id="@+id/button_highscore"
style="@style/clickMenuButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_highscore_margintop"
android:onClick="highScore"
android:text="@string/button_highscore" />

<Button
android:id="@+id/button_quit"
style="@style/clickMenuButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_quit_marginTop"
android:onClick="showInsertitial"
android:text="@string/button_quit" />

</FrameLayout>


and dimens.xml from values-xhdpi/



<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

<!-- Main Activity Layout -->
<dimen name="textView_title_textsize">145sp</dimen>
<dimen name="textView_title_margintop">125dp</dimen>
<dimen name="button_newgame_margintop">390dp</dimen>
<dimen name="button_highscore_margintop">455dp</dimen>
<dimen name="button_quit_marginTop">520dp</dimen>

<!-- Game Activity Layout -->
<dimen name="textView_timer_textsize">80sp</dimen>
<dimen name="textView_timer_margintop">80dp</dimen>
<dimen name="textView_clickcount_textsize">50sp</dimen>
<dimen name="textView_clickcount_margintop">510dp</dimen>

<!-- Dialog Layout -->
<dimen name="textView_dialogTitle_textsize">60sp</dimen>
<dimen name="textView_dialogTitle_padding">6dp</dimen>
<dimen name="textView_dialogGOInfo_textsize">40sp</dimen>
<dimen name="textView_dialogGOInfo_padding">10dp</dimen>
<dimen name="textView_dialogHSInfo_textsize">70sp</dimen>
<dimen name="textView_dialogHSInfo_padding">5dp</dimen>

<!-- Menu Button -->
<dimen name="button_menubutton_textsize">15sp</dimen>
</resources>


Any easy fix or am I going to have to write a million copies of dimens.xml?


No comments:

Post a Comment