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
}