Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support
It is exposed in the settings menu on some Android devices (manufacturer dependent). It may also be altered by some accessibility options (device-dependent).
In general, you should always used scale-independent pixels, especially for a large body of text.
However if your text has to fit into a bounding-box of known size then you should use density independent pixels in order to ensure that the text always fits properly and that all characters are visible regardless of the users' setting.
In a nutshell: would increasing the text-size by around 5sp result in the text being unreadable or mangle your UI? If so use density-independent pixels. If not, use scale-independent pixels. However you should generally aim to use scale-independent pixels wherever possible, which means designing a UI that can accommodate different text sizes.
What is the difference between px, dip, dp, and sp?
From the Android Developer Documentation:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> Millimeters - based on the physical size of the screen.pt
> Points - 1/72 of an inch based on the physical size of the screen.dp or dip
> Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".sp
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
Why is 60dp text clipped in a 60dp TextView?
It should help to disable the implicit font padding
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:drawablePadding="0dp"
android:padding="0dp"
android:textSize="60dp"
android:includeFontPadding="false"
android:text="12:22" />
Dp unit does not scale layouts in different screens
Using dp for dimensions is not a truly one size fits all solution for this problem.
Note that you should use layout weight when possible, and in general one dp value should work for all screen sizes. However, sometimes you will run in to a edge case that causes problems, and you just need to do something to make it work (For example I had to use this technique for positioning a badge on a tab in a TabLayout correctly for all screen sizes).
What I do to get around it is to put a dimens.xml file for each supported screen size:
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
You can use other qualifiers as well to target tablets if that is needed, see here for a guide to configuration qualifier names.
Then, specify each dimension for each screen size qualifier in each file (note that this only needs to be done for the dimension values that are causing problems on very large or very small screens).
For example in res/values-large/dimens.xml you might have this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_view_height">140dp</dimen>
</resources>
Then in res/values-small/dimens.xml you might have this to make it fit on the smaller screens:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_view_height">96dp</dimen>
</resources>
Then, in your layout, reference it with @dimen/your_dimens_id
, and the framework will choose the correct one to take for the screen size of the device:
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/image_view_height"
android:src="@drawable/logo3"
android:scaleType="centerCrop"
/>
Font size of TextView in Android application changes on changing font size from native settings
Actually, Settings font size affects only sizes in sp
. So all You need to do - define textSize
in dp
instead of sp
, then settings won't change text size in Your app.
Here's a link to the documentation: Dimensions
However please note that the expected behavior is that the fonts in all apps respect the user's preferences. There are many reasons a user might want to adjust the font sizes and some of them might even be medical - visually impaired users. Using dp
instead of sp
for text might lead to unwillingly discriminating against some of your app's users.
i.e:
android:textSize="32dp"
Android app UI messed up when I change from appearance from settings
You have to use dp for setting the size of Text, it will sort your problem.
You should also refer this SO for when to using SP and DP.
Stackoverflow link
Resize text size of a TextView in a Fragment
First, please read the following Q&A about the textsize "sp" unit in android. You should understand why it used sp as unit for textView and your question should be solved.
Should use "sp" instead of "dp" for text sizes
What is the difference between "px", "dp", "dip" and "sp" on Android?
Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support
http://www.singhajit.com/tutorial-1-android-ui-desgin-and-styling/
Now, you should know the usage of "sp" unit in TextView since it can be adjusted due to the user accessibility setting. That's mean if your boss cannot see it clearly, he/she can set the font size setting in the devices instead of code change.
If you still need workaround for the programming. Here is the solution
I have added the seekbar in your fragment layout.
Update xml.
<RelativeLayout android:id="@+id/oneFragmentId"
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"
>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"/>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBar"
android:fadingEdge="none"
android:fillViewport="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/tvFragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:text="Hello world"
android:textIsSelectable="true"
android:textSize="27sp"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I have change your textView text as "Hello world" for testing, please change back to your own string resource.
Here is the fragment code.
public class OneFragment extends Fragment {
//static WebView mWebview;
public OneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
final TextView tvFragmentOne = (TextView) rootView.findViewById(R.id.tvFragmentOne);
SeekBar lSeekBar = (SeekBar) rootView.findViewById(R.id.seekBar);
lSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvFragmentOne.setTextSize(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return rootView;
}
}
If you like to use other control pinch to zoom, up/down button or other control component to change the text size of the textview, you can do it with the following procedures:
Change other widget instead of seekbar
Change the listener for listening the component control change( like OnSeekBarChangeListener )
Reset the textView size
Related Topics
How to Programmatically Scroll Android Webview
Start New Activity and Finish Current One in Android
Convert Existing Project to Library Project in Android Studio
Asynctask, Must It Take Such a Performance Penalty Hit...
How Do Cursorloader Automatically Updates the View Even If the App Is Inactive
How to Return String or JSONobject from Asynchronous Callback Using Retrofit
Changing the Screen Brightness System Setting Android
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
What Dom Events Are Available to Webkit on Android
Webview Rendering Issue in Android Kitkat
How to Reduce the Spacing Between the Action Item Icons on Action Bar
Moving from One Activity to Another Activity in Android
How to Copy Selected Files from Android with Adb Pull
How to Import a .Aar File into Android Studio 1.1.0 and Use It in My Code
Android: Custom Separator (Or Even Item) in Listview Depening on Content of Item