简体   繁体   中英

TextView - detect if String exceeds available space

Here is my layout

         RelativeLayout
             ImageView  iv1
             TextView  
                   toTheRightOf iv1
                   toTheEndOf   iv1
                   toTheLeftOf  iv2      
             ImageView  iv2
         RelativeLayout end

So TextView is sandwiched between two ImageViews. So everything is displaying exactly as it should. However sometimes the line in the TextView is too long and comes too close to second ImageView. Sometimes extending right into it. I am not looking to change the layout or try to squeeze around anything. What I want to do is detect when the length of the String is too long and change the content of the String to abbreviated version with fewer characters. How can I tell if and when the String is too close to the ImageView? Sometimes there is room and sometimes not. This layout is for item in a RecyclerView - not that that really matters much as far as the question is concerned. I know Android can detect when String is too long and add ... but what I am trying to do is rather than ... I want to simply change the String to one that has fewer characters.

Perhaps I just need to decide how many characters is too many? But will this change by device resolution etc? Or will it be the same whether I am on a tablet or Galaxy Note, or smaller device? I don't want a different layout per device just for this issue. I would rather just dynamically be able to check whether the string will take up too much room. In any event it's not really a layout change it's a string content change I am after.

UPDATE: TO CLARIFY.

Let's say the String is 'Thursday, December 19, 2017' If this is would take up too much room then I don't want to see 'Thursday, December 19 ...' Rather I want to see that the String is too long and instead do

    tv.setText('12/19/2017');

I want to conditionally set the text value based on whether the given string takes up too much room.

Try this solution:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image1"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsajkhfkjhfasa fbsa fas fahj afs fsa afs sf afs sda da das dsals"
        android:ellipsize="middle"
        android:padding="10dp"
        android:maxLines="1"
        android:layout_toLeftOf="@+id/image2"
        android:layout_toRightOf="@+id/image1" />

    <ImageView
        android:id="@+id/image2"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

In this layout, textview would ellipsize itself whenever the length would be greater than the available space.

It may be helpful. I did something like that, and in my solution I am scaling down textView, but you can set shorter text.

    if(textView.getWidth() > 0) {
        float widthOfTheText = textView.getPaint().measureText(textView.getText());
        if(widthOfTheText > textView.getWidth()) {
            //exceeds, let scale it
            textView.setTextScaleX((textView.getWidth()-0.1f)/widthOfTheText);
        } else {
            //not exceeds
        }
    } else {
        //not layouted yet
        textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              TextView tv = (TextView) v;
              float widthOfTheText = tv.getPaint().measureText(tv.getText());
              tv.setTextScaleX((tv.getWidth()-0.1f)/widthOfTheText);
              tv.removeOnLayoutChangeListener(this);

              if(widthOfTheText > tv.getWidth()) {
                 //exceeds, let scale it
                 tv.setTextScaleX((textView.getWidth()-0.1f)/widthOfTheText);
              } else {
                 //not exceeds
              }
           }
       });
    }

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM