From d3ee5dd3492632003f44771064fc4a1eff5e36a9 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 14 Feb 2019 14:35:19 +0100 Subject: [PATCH] Improved chat bubble width if multiline --- .../linphone/chat/ChatMessageViewHolder.java | 5 ++ .../MultiLineWrapContentWidthTextView.java | 77 +++++++++++++++++++ app/src/main/res/layout/chat_bubble.xml | 6 +- app/src/main/res/layout/chat_devices.xml | 1 + 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/linphone/views/MultiLineWrapContentWidthTextView.java diff --git a/app/src/main/java/org/linphone/chat/ChatMessageViewHolder.java b/app/src/main/java/org/linphone/chat/ChatMessageViewHolder.java index 447c06789..f6e3b2560 100644 --- a/app/src/main/java/org/linphone/chat/ChatMessageViewHolder.java +++ b/app/src/main/java/org/linphone/chat/ChatMessageViewHolder.java @@ -125,6 +125,11 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi } } + private boolean isTooLarge(TextView text, String newText) { + float textWidth = text.getPaint().measureText(newText); + return (textWidth >= text.getMeasuredWidth()); + } + public void bindMessage(final ChatMessage message, LinphoneContact contact) { eventLayout.setVisibility(View.GONE); securityEventLayout.setVisibility(View.GONE); diff --git a/app/src/main/java/org/linphone/views/MultiLineWrapContentWidthTextView.java b/app/src/main/java/org/linphone/views/MultiLineWrapContentWidthTextView.java new file mode 100644 index 000000000..6dab5e7f0 --- /dev/null +++ b/app/src/main/java/org/linphone/views/MultiLineWrapContentWidthTextView.java @@ -0,0 +1,77 @@ +package org.linphone.views; + +/* +MultiLineWrapContentWidthTextView.java +Copyright (C) 2019 Belledonne Communications, Grenoble, France + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +import android.annotation.SuppressLint; +import android.content.Context; +import android.text.Layout; +import android.util.AttributeSet; +import android.widget.TextView; +import androidx.annotation.Nullable; + +/** + * The purpose of this class is to have a TextView declared with wrap_content + * as width that won't fill it's parent if it is multi line + */ +@SuppressLint("AppCompatCustomView") +public class MultiLineWrapContentWidthTextView extends TextView { + + public MultiLineWrapContentWidthTextView(Context context) { + super(context); + } + + public MultiLineWrapContentWidthTextView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public MultiLineWrapContentWidthTextView( + Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + protected void onMeasure(int widthSpec, int heightSpec) { + int widthMode = MeasureSpec.getMode(widthSpec); + + if (widthMode == MeasureSpec.AT_MOST) { + Layout layout = getLayout(); + if (layout != null) { + int maxWidth = + (int) Math.ceil(getMaxLineWidth(layout)) + + getCompoundPaddingLeft() + + getCompoundPaddingRight(); + widthSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST); + } + } + + super.onMeasure(widthSpec, heightSpec); + } + + private float getMaxLineWidth(Layout layout) { + float max_width = 0.0f; + int lines = layout.getLineCount(); + for (int i = 0; i < lines; i++) { + if (layout.getLineWidth(i) > max_width) { + max_width = layout.getLineWidth(i); + } + } + return max_width; + } +} diff --git a/app/src/main/res/layout/chat_bubble.xml b/app/src/main/res/layout/chat_bubble.xml index e1ddfdade..eeacac9ec 100644 --- a/app/src/main/res/layout/chat_bubble.xml +++ b/app/src/main/res/layout/chat_bubble.xml @@ -8,7 +8,7 @@ android:layout_width="1dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" - android:layout_alignParentRight="true" /> + android:layout_alignParentRight="true"/> -