Skip to content

Instantly share code, notes, and snippets.

@trivalent
Created July 1, 2016 12:29
Show Gist options
  • Save trivalent/00526294fa38072a6034f5dc5910824a to your computer and use it in GitHub Desktop.
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());
}
}
}
@trivalent
Copy link
Author

@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.

@trivalent
Copy link
Author

Not working for Arabic language

Would be great if you can share the solution, in case you found it.

@sakshi5724
Copy link

it gives null pointer exception on onMesure()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment