Improved chat bubble width if multiline

This commit is contained in:
Sylvain Berfini 2019-02-14 14:35:19 +01:00
parent 2f2dba8e34
commit d3ee5dd349
4 changed files with 86 additions and 3 deletions

View file

@ -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);

View file

@ -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;
}
}

View file

@ -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"/>
<CheckBox
android:id="@+id/delete_event"
@ -80,7 +80,7 @@
android:layout_below="@+id/time"
android:layout_marginLeft="45dp"
android:layout_marginTop="1dp"
android:layout_marginRight="5dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="1dp"
android:layout_toLeftOf="@+id/imdn"
android:background="@drawable/chat_bubble_outgoing_full"
@ -96,7 +96,7 @@
android:layout_marginRight="5dp"
app:flexWrap="wrap" />
<TextView
<org.linphone.views.MultiLineWrapContentWidthTextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View file

@ -35,6 +35,7 @@
android:ellipsize="marquee"
android:gravity="center"
android:padding="15dp"
android:singleLine="true"
android:text="@string/group_chat_room_devices" />
<ImageView