Skip to content

Commit

Permalink
Issue #173 : Max width of messageTv is now set in onCreateViewHolder (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushi98 authored and kukreja-vikramaditya committed Nov 19, 2017
1 parent a06a012 commit 177fd88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/macbitsgoa/ard/adapters/ChatMsgAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.macbitsgoa.ard.R;
import com.macbitsgoa.ard.models.MessageItem;
import com.macbitsgoa.ard.viewholders.ChatMsgViewHolder;

import io.realm.RealmResults;

import static com.macbitsgoa.ard.utils.AHC.getScreenWidth;
import static com.macbitsgoa.ard.viewholders.ChatMsgViewHolder.MAX_WIDTH_FRACTION;

public class ChatMsgAdapter extends RecyclerView.Adapter<ChatMsgViewHolder> {

public static final int RECEIVER = 0;
Expand All @@ -29,6 +33,7 @@ public ChatMsgAdapter(final RealmResults<MessageItem> messageItems) {
public ChatMsgViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View view = inflater.inflate(R.layout.vh_activity_chat_chatmsg, parent, false);

final LinearLayout root = view.findViewById(R.id.ll_chatmsg_root);
final LinearLayout msgBox = view.findViewById(R.id.ll_chatmsg_msg_box);
if (viewType == RECEIVER) {
Expand All @@ -38,6 +43,11 @@ public ChatMsgViewHolder onCreateViewHolder(final ViewGroup parent, final int vi
root.setGravity(Gravity.START);
msgBox.setBackground(ContextCompat.getDrawable(parent.getContext(), R.drawable.bg_chat_sen));
}

final TextView messageTv = view.findViewById(R.id.textView_viewHolder_chatmsg_msg);
final int msgMaxWidth = ((int) (getScreenWidth() * MAX_WIDTH_FRACTION));
messageTv.setMaxWidth(msgMaxWidth);

return new ChatMsgViewHolder(view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
import com.macbitsgoa.ard.types.MessageStatusType;
import com.macbitsgoa.ard.utils.AHC;

import static com.macbitsgoa.ard.utils.AHC.getScreenWidth;

public class ChatMsgViewHolder extends RecyclerView.ViewHolder {

/**
* Maximum width fraction of {@link #message} + {@link #time} + {@link #status} of screen size.
* Maximum width fraction of {@link #message} of screen size.
*/
private static final float MAX_WIDTH_FRACTION = 0.8f;
public static final float MAX_WIDTH_FRACTION = 0.6f;

private TextView time;

Expand All @@ -44,13 +42,6 @@ public void populate(@NonNull final MessageItem messageItem) {
time.setText(AHC.getSimpleDayAndTime(messageItem.getMessageTime()));
message.setText(messageItem.getMessageData());

// https://stackoverflow.com/a/24035591
message.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
final int msgMaxWidth = ((int) (getScreenWidth() * MAX_WIDTH_FRACTION)) - time.getWidth() - status.getWidth();
if (message.getWidth() > msgMaxWidth) {
message.setWidth(msgMaxWidth);
}
});
if (getItemViewType() == ChatMsgAdapter.RECEIVER) {
final Context context = status.getContext();
switch (messageItem.getMessageStatus()) {
Expand All @@ -75,7 +66,7 @@ public void populate(@NonNull final MessageItem messageItem) {
status.setColorFilter(Color.GRAY);
break;
}
} else {
} else if (getItemViewType() == ChatMsgAdapter.SENDER) {
status.setVisibility(View.GONE);
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/vh_activity_chat_chatmsg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
android:autoLink="all"
android:linksClickable="true"
android:padding="8dp"
android:textColor="@color/black"
android:textColorLink="@color/blue_500"
android:textIsSelectable="true"
android:textSize="16sp"
tools:ignore="NestedWeights"
tools:text="CHATMSG" />
tools:text="Chat MSG" />

<TextView
android:id="@+id/textView_viewHolder_chatmsg_time"
Expand Down

0 comments on commit 177fd88

Please sign in to comment.