1

I want to count the total number of characters a TextView can hold with 14dp text size and 100dp of width and with 100dp of height. then I have to add those characters' limits to an EditText.

 <TextView
                android:id="@+id/OFSO_text1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:layout_gravity="top|center"
                android:fontFamily="@font/lato_bold"
                android:gravity="center|bottom"
                android:text="SEASONAL DISCOUNT ARE HERE"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="@color/lightcream"
                android:textSize="14dp"
                android:textStyle="normal"
                android:autoSizeTextType="none"
               />

I have tried this code but this is not working properly. this provides me more char limit then this Textview can holds.

            TextPaint paint = textView.getPaint();
            int wordwidth=(int)paint.measureText("a",0,1);
            int screenwidth = getResources().getDisplayMetrics().widthPixels;
            int  maxLength = screenwidth/wordwidth;

please help me out!! thanks in advance. happy coding!!!

ADM
  • 20,406
  • 11
  • 52
  • 83
sam
  • 216
  • 4
  • 17
  • try OFSO_text1.getText().toString().length(); and store it into variable – niceumang Dec 02 '19 at 06:57
  • 1
    TextSize Should be in SP .. – ADM Dec 02 '19 at 06:59
  • Does this answer your question? [Limit the Length of TextView](https://stackoverflow.com/questions/13120397/limit-the-length-of-textview) – Divyesh patel Dec 02 '19 at 07:00
  • @Niceumang I don't want to count string char, I want to add more text to this TextView but before that, I need to know how many more char this text view can hold? got my point? – sam Dec 02 '19 at 07:03

4 Answers4

0

You are missing font size and typeface in your measurements.

        // In case you are storing your font in res/font
        Typeface typeface = ResourcesCompat.getFont(context, R.font.noto_sans_regular);
        paint.setTypeface = typeface
        paint.setTextSize = sizeOfYourFontInPixels
Dmitrii Leonov
  • 1,331
  • 1
  • 15
  • 25
0

First of all, your textholder size varies from device to device, And for the count, you can do something like,

    int textLenght = yourtText.getText().toString().lenght();
    if(textLenght>n) {
    yourEditText.setText(yourText);}
Md. Rafsan Biswas
  • 128
  • 1
  • 2
  • 8
0

Measuring Text: You can do something like this :

float textWidth = textView.getPaint().measureText(textView.getText().toString());

if(textView.getMeasureWidth() == n) {

Log.i("Text_count",textView.getText().toString().lenght());

}

or

if(textWidth == n) {
Log.i("Text_count",textView.getText().toString().lenght());
}
Md. Rafsan Biswas
  • 128
  • 1
  • 2
  • 8
0

Try this

  1. Add addTextChangedListener to the edittext for reading characters.
  2. In override methods beforeTextChanged, onTextChanged and afterTextChanged, read characters like below

    int length = etText.length();
    int leftCharacters = 500 - length; //let default length of edittext is 500
    

Hope, it works for you!!

Android Geek
  • 8,956
  • 2
  • 21
  • 35