2

Hey I know there's a lot of questions pertaining to this topic, I just need some clarification.

I'm trying to get my app to look as closely the same on all device sizes. I use 'sp' as the type of value for "textSize", but it doesn't seem to do anything. It looks too big on ldpi, and hdpi screen sizes, and too small on mdpi, and xxhdpi. It looks like how I'd like it to look on hdpi, and xhdpi screens.

I've enabled "Screen Support" on my AndroidManifest file. The part I'm having trouble with is the values folders.

These are currently what I have there: (I haven't made any modifications of my own to the folder structure, that I know of)

res
-Values
--dimens.xml
--styles.xml
--strings.xml
-Values-v11
--styles.xml
-Values-v14
--styles.xml
-Values-w820dp
--dimens.xml

Do I just make a new values folder for ldpi, hdpi, xhdpi, xxhdpi, etc.? Then make a new dimens.xml for each? What about after that?

Here's my AndroidManifest.xml file (Application tag minimized):

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.suckatprogramming.coach"
        android:versionCode="1"
        android:versionName="1.0" >

        <supports-screens android:resizeable="true"
                          android:smallScreens="true" 
                          android:normalScreens="true" 
                          android:largeScreens="true"
                          android:anyDensity="true" />

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="19" />

        <application... />

    </manifest>

I'm using a LinearLayout if that means anything. Let me know if you need anything else from me. Thanks for any assistance you can provide.

1 Answers1

0

I'm not sure whether I get the question , but you can try this.Starting with quite big size for the text in the textvew and to decrease it , until it fits..

`

public static void setTextSizeToFitTextView(TextView tv) 



    /*
     * Resize the text size with specified width and height
     * @param width
     * @param height
     */

    int height = tv.getHeight();
    int width = tv.getWidth();
    float mTextSize = tv.getTextSize();
    CharSequence text = tv.getText();
    // Do not resize if the view does not have dimensions or there is no
    // text
    if (text == null || text.length() == 0 || height <= 0 || width <= 0
            || mTextSize == 0) {
        return;
    }

    // Get the text view's paint object
    TextPaint textPaint = tv.getPaint();

    float targetTextSize = 50.0f;

    // Get the required text height
    int textHeight = getTextHeight(text, textPaint, width, targetTextSize);

    // Until we either fit within our text view or we had reached our min
    // text size, incrementally try smaller sizes

    while (textHeight >= height) {
        targetTextSize = targetTextSize - 2;
        textHeight = getTextHeight(text, textPaint, width, targetTextSize);
    }
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
    // textPaint.setTextSize(targetTextSize);
}

// Set the text size of the text paint object and use a static layout to
// render text off screen before measuring
private static int getTextHeight(CharSequence source,
        TextPaint originalPaint, int width, float textSize) {
    // Update the text paint object
    TextPaint paint = new TextPaint(originalPaint);
    paint.setTextSize(textSize);
    // Measure using a static layout
    StaticLayout layout = new StaticLayout(source, paint, width,
            Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    return layout.getHeight();
}`