-
-
Save trivalent/00526294fa38072a6034f5dc5910824a to your computer and use it in GitHub Desktop.
<view | |
android:id="@+id/content_layout" | |
class="com.example.ChatBubbleLayout" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="left" | |
> | |
<TextView | |
android:id="@+id/txt_send" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="left" | |
/> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="bottom|center|right" | |
android:orientation="horizontal" | |
android:background="@android:color/holo_red_light" | |
> | |
<TextView | |
android:id="@+id/txt_send_timer" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:tag="TIME" | |
/> | |
</LinearLayout> | |
</view> |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.text.Layout; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import android.widget.TextView; | |
/** | |
* Created by trivalent on 7/1/2016. | |
*/ | |
public class ChatBubbleLayout extends FrameLayout { | |
public ChatBubbleLayout(Context context) { | |
super(context); | |
} | |
public ChatBubbleLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ChatBubbleLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(21) | |
public ChatBubbleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
TextView message_textView = (TextView) getChildAt(0); | |
View localView = getChildAt(1); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
int view_size = View.MeasureSpec.getSize(widthMeasureSpec); | |
Layout messageTextviewLayout = message_textView.getLayout(); | |
int linestart = messageTextviewLayout.getLineStart(messageTextviewLayout.getLineCount() - 1); | |
int lineend = messageTextviewLayout.getLineEnd(messageTextviewLayout.getLineCount() - 1); | |
int desiredWidth = (int) Layout.getDesiredWidth(message_textView.getText() | |
.subSequence(linestart, lineend), message_textView.getPaint()); | |
int measuredWidth = message_textView.getMeasuredWidth(); | |
int requiredWidth = Math.min(measuredWidth, | |
(int) Math.ceil(Layout.getDesiredWidth(message_textView.getText(), | |
message_textView.getPaint())) + message_textView.getPaddingRight() + | |
message_textView.getPaddingLeft()); | |
if (view_size - getPaddingLeft() - getPaddingRight() | |
>= requiredWidth + localView.getMeasuredWidth()) { | |
setMeasuredDimension(requiredWidth + localView.getMeasuredWidth() + | |
getPaddingLeft() + getPaddingRight(), getMeasuredHeight()); | |
} else if ((requiredWidth - message_textView.getPaddingLeft() - message_textView.getPaddingRight() | |
< desiredWidth + localView.getMeasuredWidth())) { | |
setMeasuredDimension(getMeasuredWidth(), | |
getMeasuredHeight() + localView.getMeasuredHeight()); | |
} | |
} | |
} |
A simple way to ensure space between message text and time text is to add android:paddingLeft
to the time view. Also, to have the timestamp sits on the bottom, int bottomMargin
is not needed.
@hardikm9850 Can I have your final implementation please
Not working for Arabic language
@trivalent can you please share your styles.xml as well. as you have used INCOMING_CHAT_TEXT_STYLE and INCOMING_CHAT_TS_TEXT_STYLE from it.
@trivalent can you please share your styles.xml as well. as you have used INCOMING_CHAT_TEXT_STYLE and INCOMING_CHAT_TS_TEXT_STYLE from it.
There is nothing special in the styles. I am just specifying the text colour/font size etc in those.
Not working for Arabic language
Would be great if you can share the solution, in case you found it.
it gives null pointer exception on onMesure()
The updated code works for multiple lines but for single line it adds more space between text and time view.
I added some fill-in code where you are calculating max between
finalFrameLayoutRequiredWidth, childTextView.getMeasuredWidth() + viewPaddingLeftNRight)
Thank you for the solution 👍