Wednesday, 15 January 2014

java - Using resolution independant fonts within an android layout -



java - Using resolution independant fonts within an android layout -

background

i'm creating layout android application , have until been using font sizes expressed in sp (or dp) in order scale correctly different devices. result has been different hoping. result has been text same physical size (in terms of inches) on devices whereas i'd occupy same proportion of screen.

at nowadays menu buttons little on tablets ludicrously big on phones (see screenshots; although doesn't justice how bad phone version looks).

using px gives me results want these particular devices because have similar resolutions (but different pixel densities). if used pixels based upon how want on these 2 devices (both approximately 1,200 x 700) , galaxy s10 came out same physical size galaxy s3 had resolution of 10,000 x 7,000 buttons ridiculously small.

question

how can set font size (or size) resolution independent (not density independent)?

galaxy s3 - big phone

tab 3 - medium sided tablet

current layout <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" android:background="@drawable/menubackground" > <linearlayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" android:layout_gravity="center" android:gravity="center" > <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/singleplayer" android:layout_margintop="50dp" android:layout_marginbottom="50dp" android:textsize="50sp" android:onclick="singleplayer" /> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/twoplayer" android:layout_margintop="50dp" android:layout_marginbottom="50dp" android:textsize="50sp" android:onclick="twoplayer" /> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/campaign" android:layout_margintop="50dp" android:layout_marginbottom="50dp" android:textsize="50sp" android:onclick="campaign" /> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/ai_vs_ai" android:layout_margintop="50dp" android:layout_marginbottom="50dp" android:textsize="50sp" android:onclick="aivsai" /> </linearlayout> </linearlayout>

first, state, dp (density independent pixels) supposed same "physical" size in devices, no matter display density, while sp (scaled pixels) supposed scale based on user preferences. not suitable you're attempting do.

if don't want different layouts, different font sizes, can utilize references resources these attributes. example, can create alternative xml files values , values-large:

in values:

<dimen name="mainmenuoption">50sp</dimen>

in values-large:

<dimen name="mainmenuoption">80sp</dimen>

and then, within layout, use:

<button android:layout_height="wrap_content" android:textsize="@dimen/mainmenuoption" ...

most (though not all) attributes can utilize references resources.

of course, not accomplish "resolution independence", allows customizing "small parts" instead of whole layouts depending on device characteristics.

java android resolution

No comments:

Post a Comment