Created
July 1, 2016 12:29
-
-
Save trivalent/00526294fa38072a6034f5dc5910824a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it gives null pointer exception on onMesure()