4

Is there a way to find out whether the text content to be placed in a TextView will fit in a single row or not?

What i want to achieve is displayed in the attached image (its a section of a listView). The problem relates to textView#3. If it is too big, i want to place it below textView#2, but if content is short enough i want to place it to right of textView#2 (scenario seen in row#3 is what i want to escape from)

So, anyone?.. How can i solve this particular problem? I'm imagining this can't be achieved with a single layout for my listview's rows..

enter image description here

Community
  • 1
  • 1
pulancheck1988
  • 2,024
  • 3
  • 28
  • 46

4 Answers4

2

It is possible. You should measure text size and compare it against TextView size. If text width > textView width than it is too long.

You can learn about text measuring from this post.

also you can use TextView's built-in features and make it single line and set text ellipsize method.

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • i need full text to be display.. it will more than probably not be a big paragraph.. maybe ~10 words.. – pulancheck1988 Jan 10 '13 at 09:13
  • if you want to display full text in one line, use AutoResizeTextView. It will decrease font size to fit all text in textView in one line. – Leonidos Jan 10 '13 at 09:45
0

A solution (& not a pretty one) is to do some math on the space occupied in the textView by your strings. In my particular case i've added 2 textViews (3 & 3B) & depending on what my math calculations say - i would use just one & hide the other.

Details:

  1. in the getView method of your adapter find out the width of the parent listView

    public View getView(int position, View convertView, ViewGroup parent) { int listViewWidth = parent.getWidth();

  2. calculate occupied space by other textViews placed in same row (just textView2 in my case);

    Paint paint = textViewX.getPaint(); occupiedWidth = (int) (occupiedWidth +paint.measureText("text to be placed in textview"));

  3. compare space occupied by text to be placed in the last textview ( in my screenShot textView3, same formula to calculate width) & compare with space left (listViewWidth - occupiedWidth)

  4. set the text on the correct textView & hide the other
  5. .. improve the code as needed

new layout result

pulancheck1988
  • 2,024
  • 3
  • 28
  • 46
0

There is no in-build function to compare this.

After doing to many research and code i have build my own function to do this...

1) copy this code to your project



import android.app.Activity;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;

public class TextMeasure {


    public static boolean isTextCanFitInTextView(TextView textView, String txt , float allowWidthInDp , Activity activity){

        String backupText = textView.getText().toString();

        textView.setText(txt);
        textView.measure(0, 0);       //must call measure!
        float textViewSize_px = textView.getMeasuredWidth(); //get width in px

        // Converts dip into its equivalent px
        float dip = allowWidthInDp;
        Resources r = activity.getResources();
        float allowView_px = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dip,
                r.getDisplayMetrics()
        );

        //restore textView
        textView.setText(backupText);

        return textViewSize_px <= allowView_px;
    }


    public static boolean isTextCanFitInTextView_matchParent(TextView textView, String txt , float totalMarginInDp, Activity activity){

        String backupText = textView.getText().toString();

        textView.setText(txt);
        textView.measure(0, 0);       //must call measure!
        float textViewSize_px = textView.getMeasuredWidth(); //get width in px

        // Converts dip into its equivalent px
        float dip = totalMarginInDp;
        Resources r = activity.getResources();
        float totalMarginInDp_px = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dip,
                r.getDisplayMetrics()
        );

        float allowView_px;
        float window_length;

        window_length = getDisplayWidthInPixel(activity);

        allowView_px = window_length - totalMarginInDp_px;

        //re store textView
        textView.setText(backupText);

        return textViewSize_px <= allowView_px;
    }

    private static float getDisplayWidthInPixel(Activity activity){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return  metrics.widthPixels;
    }




}

2) Use above class method to do your text and textView comparison , for eg :

1) Know exact width , android:layout_width="300dp"


 TextView yourTextView = findViewById(R.id.txt);
        float widthOfTextView_inDp = 300f;      // width of textView Size in dp (densityPixel) , what you set in xml view

        String yourTxt = "your text need to display in single line";

        if (TextMeasure.isTextCanFitInTextView(yourTextView,yourTxt,widthOfTextView_inDp,this)){
            // text can fit in to text View
            yourTextView.setText(yourTxt);

        }else {
            // text can't fit in to text View

            // add your logic
        }

2) match_parent used to set width.

android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:layout_width="match_parent"


 TextView yourTextView = findViewById(R.id.txt);
        float totalMargin_inDp = 20f;      // width of text view is match_parent and (marginStart = 10dp), (marginEnd = 10dp) = 20dp, note: it is total margin from both end of mobile screen combine.

        String yourTxt = "your text need to display in single line";

        if (TextMeasure.isTextCanFitInTextView_matchParent(yourTextView,yourTxt,totalMargin_inDp,this)){
            // text can fit in to text View
            yourTextView.setText(yourTxt);

        }else {
            // text can't fit in to text View

            // add your logic
        }


-2

try this property of textview

android:maxLength="number of characters you want to display in textview"

now in Actvity check if string length is > maxLength than change the postion of your view.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
  • 1
    thanks ..but i dont know number of characters.. textView uses wrap content.. & obviously parent viewGroup/layout width will dictate how much content can this textview hold (on tablets i will definetly have space..) – pulancheck1988 Jan 10 '13 at 10:39