Cabezas
Cabezas

Reputation: 10717

How to know if there is available space in TextView?

Because I would like to make the same, like Whatsapp, it puts message and hour in the same line

enter image description here

or in another line.

Sometimes there is space and Whatsapp puts the hour in same line. However sometimes there is not space and Whatsapp puts the hour in other line.

Inside or outside...

Any idea?

Upvotes: 3

Views: 335

Answers (2)

Cabezas
Cabezas

Reputation: 10717

public static final String TAG = "MainActivity";
private TextView mText;
private RelativeLayout relativeLayout;
private Boolean mFirstTime = true;
private static final int WIDH_HOUR = 382;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final int width = getScreensWidh();

    mText = (TextView) findViewById(R.id.activity_main_text);
    relativeLayout = (RelativeLayout) findViewById(R.id.activity_main_relative);

    mText.setText("aaaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa dsa aaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa");

    ViewTreeObserver vto = mText.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mFirstTime) {
                Layout layout = mText.getLayout();
                int lines = layout.getLineCount();

                int offset = layout.layout.getLineWidth(lines - 1);
                int freeSpace = width - offset;

                TextView hour = new TextView(MainActivity.this);
                hour.setText("12:20");
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                if (freeSpace > WIDH_HOUR) {
                    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.activity_main_text);
                } else {
                    params.addRule(RelativeLayout.BELOW, R.id.activity_main_text);
                }
                hour.setLayoutParams(params);
                relativeLayout.addView(hour);
                Log.d(TAG, String.valueOf(freeSpace));
                mFirstTime = false;
            }

        }
    });


}

public int getScreensWidh() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size.x;

}

Two Public Methods

Return the number of lines of text in this layout.

Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.

Upvotes: 1

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

I think you can give textView.getLayout().getLineWidth(lineNum) a try (lineNum is obviously the last line). Then compare it to the parent layout width, and if the difference is bigger than timestamp.getWidth() plus some extra padding, then you display it on the same line.

Upvotes: 0

Related Questions