How to adjust text font size to fit textview

The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc’s, but it is a bit more readable. It also include’s gregm’s bug fixes and a bug-fix of my own. import android.content.Context; import android.graphics.Paint; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.TextView; public … Read more

Android TextView : “Do not concatenate text displayed with setText”

Resource has the get overloaded version of getString which takes a varargs of type Object: getString(int, java.lang.Object…). If you setup correctly your string in strings.xml, with the correct place holders, you can use this version to retrieve the formatted version of your final String. E.g. <string name=”welcome_messages”>Hello, %1$s! You have %2$d new messages.</string> using getString(R.string.welcome_message, … Read more

Change the Right Margin of a View Programmatically?

EDIT: A more generic way of doing this that doesn’t rely on the layout type (other than that it is a layout type which supports margins): public static void setMargins (View v, int l, int t, int r, int b) { if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); p.setMargins(l, t, r, b); … Read more

Android Paint: .measureText() vs .getTextBounds()

You can do what I did to inspect such problem: Study Android source code, Paint.java source, see both measureText and getTextBounds methods. You’d learn that measureText calls native_measureText, and getTextBounds calls nativeGetStringBounds, which are native methods implemented in C++. So you’d continue to study Paint.cpp, which implements both. native_measureText -> SkPaintGlue::measureText_CII nativeGetStringBounds -> SkPaintGlue::getStringBounds Now … Read more

TextView bold via XML file?

I have a project in which I have the following TextView : <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:textStyle=”bold” android:text=”@string/app_name” android:layout_gravity=”center” /> So, I’m guessing you need to use android:textStyle

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 … Read more