Skip to content

Commit

Permalink
Bubble background color and chat message row background color (#22)
Browse files Browse the repository at this point in the history
Fixes Issue #16:
- Added 2 additional attributes 'backgroundRcv' and 'backgroundSend' that will allow devs to specify the background color of the entire row of a received and/or sent message
- Altered and added code in method setBackground of MessageViewHolder to apply background color to both the bubble and entire row of sent/received messages
- Added the 2 new attributes to activity_main.xml to demonstrate their use
- Added a 'SENT' message with 1.5 seconds delay to MainActivity
  • Loading branch information
samuelo-101 authored and MichaelObi committed Apr 9, 2018
1 parent e6def21 commit 5f871fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
13 changes: 9 additions & 4 deletions chat-ui/src/main/java/co/intentservice/chatui/ChatView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
Expand All @@ -28,7 +27,6 @@
import co.intentservice.chatui.fab.FloatingActionsMenu;
import co.intentservice.chatui.models.ChatMessage;
import co.intentservice.chatui.models.ChatMessage.Type;
import co.intentservice.chatui.viewholders.MessageViewHolder;
import co.intentservice.chatui.adapters.ChatViewListAdapter;
import co.intentservice.chatui.views.ViewBuilder;
import co.intentservice.chatui.views.ViewBuilderInterface;
Expand Down Expand Up @@ -68,14 +66,15 @@ public void run() {

private float bubbleElevation;

private int backgroundRcv, backgroundSend;
private int bubbleBackgroundRcv, bubbleBackgroundSend; // Drawables cause cardRadius issues. Better to use background color
private Drawable sendButtonIcon, buttonDrawable;
private TypedArray attributes, textAppearanceAttributes;
private Context context;



ChatView(Context context) {
ChatView(Context context) {
this(context, null);
}

Expand Down Expand Up @@ -118,6 +117,7 @@ private void initializeViews() {
private void getXMLAttributes(AttributeSet attrs, int defStyleAttr) {
attributes = context.obtainStyledAttributes(attrs, R.styleable.ChatView, defStyleAttr, R.style.ChatViewDefault);
getChatViewBackgroundColor();
getAttributesForChatMessageRow();
getAttributesForBubbles();
getAttributesForInputFrame();
getAttributesForInputText();
Expand All @@ -127,7 +127,7 @@ private void getXMLAttributes(AttributeSet attrs, int defStyleAttr) {
}

private void setListAdapter() {
chatViewListAdapter = new ChatViewListAdapter(context, new ViewBuilder(), bubbleBackgroundRcv,bubbleBackgroundSend,bubbleElevation);
chatViewListAdapter = new ChatViewListAdapter(context, new ViewBuilder(), backgroundRcv, backgroundSend, bubbleBackgroundRcv,bubbleBackgroundSend,bubbleElevation);
chatListView.setAdapter(chatViewListAdapter);
}

Expand All @@ -144,6 +144,11 @@ private void getChatViewBackgroundColor() {
backgroundColor = attributes.getColor(R.styleable.ChatView_backgroundColor, -1);
}

private void getAttributesForChatMessageRow() {
backgroundRcv = attributes.getColor(R.styleable.ChatView_backgroundRcv, ContextCompat.getColor(context, R.color.default_chat_message_background_color_rcv));
backgroundSend = attributes.getColor(R.styleable.ChatView_backgroundSend, ContextCompat.getColor(context, R.color.default_chat_message_background_color_send));
}

private void getAttributesForBubbles() {

float dip4 = context.getResources().getDisplayMetrics().density * 4.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
* List Adapter for use in the recycler view to display messages using the Message View Holder
* <p>
* Created by Timi
* Extended by James Lendrem
* Extended by James Lendrem, Samuel Ojo
*/

public class ChatViewListAdapter extends BaseAdapter {

public final int STATUS_SENT = 0;
public final int STATUS_RECEIVED = 1;

private int backgroundRcv, backgroundSend;
private int bubbleBackgroundRcv, bubbleBackgroundSend;
private float bubbleElevation;
private ViewBuilderInterface viewBuilder = new ViewBuilder();
Expand All @@ -34,10 +35,12 @@ public class ChatViewListAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;

public ChatViewListAdapter(Context context, ViewBuilderInterface viewBuilder, int bubbleBackgroundRcv, int bubbleBackgroundSend, float bubbleElevation) {
public ChatViewListAdapter(Context context, ViewBuilderInterface viewBuilder, int backgroundRcv, int backgroundSend, int bubbleBackgroundRcv, int bubbleBackgroundSend, float bubbleElevation) {
this.chatMessages = new ArrayList<>();
this.context = context;
this.inflater = LayoutInflater.from(context);
this.backgroundRcv = backgroundRcv;
this.backgroundSend = backgroundSend;
this.bubbleBackgroundRcv = bubbleBackgroundRcv;
this.bubbleBackgroundSend = bubbleBackgroundSend;
this.bubbleElevation = bubbleElevation;
Expand Down Expand Up @@ -83,7 +86,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
break;
}

holder = new MessageViewHolder(convertView, bubbleBackgroundRcv, bubbleBackgroundSend);
holder = new MessageViewHolder(convertView, backgroundRcv, backgroundSend, bubbleBackgroundRcv, bubbleBackgroundSend);
convertView.setTag(holder);
} else {
holder = (MessageViewHolder) convertView.getTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* with any messages required.
* <p>
* Original Code by Timi
* Extended by James Lendrem, Michael Obi
* Extended by James Lendrem, Michael Obi, Samuel Ojo
*/

public class MessageViewHolder {
Expand All @@ -24,12 +24,15 @@ public class MessageViewHolder {
Context context;

private MessageView messageView;
private int backgroundRcv, backgroundSend;
private int bubbleBackgroundRcv, bubbleBackgroundSend;

public MessageViewHolder(View convertView, int bubbleBackgroundRcv, int bubbleBackgroundSend) {
public MessageViewHolder(View convertView, int backgroundRcv, int backgroundSend, int bubbleBackgroundRcv, int bubbleBackgroundSend) {
row = convertView;
context = row.getContext();
messageView = (MessageView) convertView;
this.backgroundRcv = backgroundRcv;
this.backgroundSend = backgroundSend;
this.bubbleBackgroundSend = bubbleBackgroundSend;
this.bubbleBackgroundRcv = bubbleBackgroundRcv;
}
Expand Down Expand Up @@ -58,18 +61,22 @@ public void setSender(String sender) {

public void setBackground(int messageType) {

int background = ContextCompat.getColor(context, R.color.cardview_light_background);
int chatMessageBackground = ContextCompat.getColor(context, R.color.cardview_light_background);
int bubbleBackground = ContextCompat.getColor(context, R.color.cardview_light_background);

switch (messageType) {
case STATUS_RECEIVED:
background = bubbleBackgroundRcv;
chatMessageBackground = backgroundRcv;
bubbleBackground = bubbleBackgroundRcv;
break;
case STATUS_SENT:
background = bubbleBackgroundSend;
chatMessageBackground = backgroundSend;
bubbleBackground = bubbleBackgroundSend;
break;
}

messageView.setBackgroundColor(background);
messageView.setBackgroundColor(chatMessageBackground);
messageView.setBackground(bubbleBackground);

}

Expand Down
3 changes: 3 additions & 0 deletions chat-ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<attr name="sendBtnIconTint" format="color" />
<attr name="sendBtnBackgroundTint" format="color" />

<attr name="backgroundRcv" format="color" />
<attr name="backgroundSend" format="color" />

<attr name="bubbleBackgroundRcv" format="color" />
<attr name="bubbleBackgroundSend" format="color" />
<attr name="bubbleElevation">
Expand Down
3 changes: 3 additions & 0 deletions chat-ui/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<color name="facebook_blue">#3B5998</color>
<color name="main_color_gray">#7d7d7d</color>

<color name="default_chat_message_background_color_rcv">@color/white</color>
<color name="default_chat_message_background_color_send">@color/white</color>

<color name="default_bubble_color_rcv">@color/black</color>
<color name="default_bubble_color_send">@color/white</color>

Expand Down
3 changes: 3 additions & 0 deletions chat-ui/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<item name="sendBtnIconTint">@color/white</item>
<item name="sendBtnBackgroundTint">?attr/colorAccent</item>

<item name="backgroundRcv">@color/default_chat_message_background_color_rcv</item>
<item name="backgroundSend">@color/default_chat_message_background_color_send</item>

<item name="bubbleElevation">elevated</item>
<item name="bubbleBackgroundRcv">@color/default_bubble_color_rcv</item>
<item name="bubbleBackgroundSend">@color/default_bubble_color_send</item>
Expand Down

0 comments on commit 5f871fd

Please sign in to comment.