This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
412 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
144 changes: 144 additions & 0 deletions
144
Android/app/src/main/java/com/thesis/smukov/anative/Chat/ChatAdapter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.thesis.smukov.anative.Chat; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.view.Gravity; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.LinearLayout; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
|
||
import com.thesis.smukov.anative.R; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by smuko on 26-Jun-16. | ||
*/ | ||
public class ChatAdapter extends BaseAdapter { | ||
|
||
private final List<ChatMessage> chatMessages; | ||
private Activity context; | ||
|
||
public ChatAdapter(Activity context, List<ChatMessage> chatMessages) { | ||
this.context = context; | ||
this.chatMessages = chatMessages; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
if (chatMessages != null) { | ||
return chatMessages.size(); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
@Override | ||
public ChatMessage getItem(int position) { | ||
if (chatMessages != null) { | ||
return chatMessages.get(position); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public View getView(final int position, View convertView, ViewGroup parent) { | ||
ViewHolder holder; | ||
ChatMessage chatMessage = getItem(position); | ||
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
|
||
if (convertView == null) { | ||
convertView = vi.inflate(R.layout.list_item_chat_message, null); | ||
holder = createViewHolder(convertView); | ||
convertView.setTag(holder); | ||
} else { | ||
holder = (ViewHolder) convertView.getTag(); | ||
} | ||
|
||
boolean myMsg = chatMessage.getIsme() ;//Just a dummy check | ||
//to simulate whether it me or other sender | ||
setAlignment(holder, myMsg); | ||
holder.txtMessage.setText(chatMessage.getMessage()); | ||
holder.txtInfo.setText(chatMessage.getDate()); | ||
|
||
return convertView; | ||
} | ||
|
||
public void add(ChatMessage message) { | ||
chatMessages.add(message); | ||
} | ||
|
||
public void add(List<ChatMessage> messages) { | ||
chatMessages.addAll(messages); | ||
} | ||
|
||
private void setAlignment(ViewHolder holder, boolean isMe) { | ||
if (!isMe) { | ||
holder.contentWithBG.setBackgroundResource(R.drawable.in_message_bg); | ||
|
||
LinearLayout.LayoutParams layoutParams = | ||
(LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); | ||
layoutParams.gravity = Gravity.RIGHT; | ||
holder.contentWithBG.setLayoutParams(layoutParams); | ||
|
||
RelativeLayout.LayoutParams lp = | ||
(RelativeLayout.LayoutParams) holder.content.getLayoutParams(); | ||
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); | ||
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); | ||
holder.content.setLayoutParams(lp); | ||
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); | ||
layoutParams.gravity = Gravity.RIGHT; | ||
holder.txtMessage.setLayoutParams(layoutParams); | ||
|
||
layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); | ||
layoutParams.gravity = Gravity.RIGHT; | ||
holder.txtInfo.setLayoutParams(layoutParams); | ||
} else { | ||
holder.contentWithBG.setBackgroundResource(R.drawable.out_message_bg); | ||
|
||
LinearLayout.LayoutParams layoutParams = | ||
(LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); | ||
layoutParams.gravity = Gravity.LEFT; | ||
holder.contentWithBG.setLayoutParams(layoutParams); | ||
|
||
RelativeLayout.LayoutParams lp = | ||
(RelativeLayout.LayoutParams) holder.content.getLayoutParams(); | ||
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); | ||
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); | ||
holder.content.setLayoutParams(lp); | ||
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); | ||
layoutParams.gravity = Gravity.LEFT; | ||
holder.txtMessage.setLayoutParams(layoutParams); | ||
|
||
layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); | ||
layoutParams.gravity = Gravity.LEFT; | ||
holder.txtInfo.setLayoutParams(layoutParams); | ||
} | ||
} | ||
|
||
private ViewHolder createViewHolder(View v) { | ||
ViewHolder holder = new ViewHolder(); | ||
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage); | ||
holder.content = (LinearLayout) v.findViewById(R.id.content); | ||
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground); | ||
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo); | ||
return holder; | ||
} | ||
|
||
private static class ViewHolder { | ||
public TextView txtMessage; | ||
public TextView txtInfo; | ||
public LinearLayout content; | ||
public LinearLayout contentWithBG; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Android/app/src/main/java/com/thesis/smukov/anative/Chat/ChatMessage.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.thesis.smukov.anative.Chat; | ||
|
||
/** | ||
* Created by smuko on 26-Jun-16. | ||
*/ | ||
public class ChatMessage { | ||
private long id; | ||
private boolean isMe; | ||
private String message; | ||
private Long userId; | ||
private String dateTime; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
public boolean getIsme() { | ||
return isMe; | ||
} | ||
public void setMe(boolean isMe) { | ||
this.isMe = isMe; | ||
} | ||
public String getMessage() { | ||
return message; | ||
} | ||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
public long getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(long userId) { | ||
this.userId = userId; | ||
} | ||
|
||
public String getDate() { | ||
return dateTime; | ||
} | ||
|
||
public void setDate(String dateTime) { | ||
this.dateTime = dateTime; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
Android/app/src/main/java/com/thesis/smukov/anative/ChatActivity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.thesis.smukov.anative; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.ActionBarActivity; | ||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ListView; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
|
||
import com.thesis.smukov.anative.Chat.ChatAdapter; | ||
import com.thesis.smukov.anative.Chat.ChatMessage; | ||
|
||
import java.text.DateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
|
||
public class ChatActivity extends ActionBarActivity { | ||
|
||
private EditText messageET; | ||
private ListView messagesContainer; | ||
private Button sendBtn; | ||
private ChatAdapter adapter; | ||
private ArrayList<ChatMessage> chatHistory; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_chat); | ||
initControls(); | ||
} | ||
|
||
private void initControls() { | ||
messagesContainer = (ListView) findViewById(R.id.messagesContainer); | ||
messageET = (EditText) findViewById(R.id.messageEdit); | ||
sendBtn = (Button) findViewById(R.id.chatSendButton); | ||
|
||
TextView meLabel = (TextView) findViewById(R.id.meLbl); | ||
TextView companionLabel = (TextView) findViewById(R.id.friendLabel); | ||
RelativeLayout container = (RelativeLayout) findViewById(R.id.container); | ||
companionLabel.setText("My Buddy");// Hard Coded | ||
loadDummyHistory(); | ||
|
||
sendBtn.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
String messageText = messageET.getText().toString(); | ||
if (TextUtils.isEmpty(messageText)) { | ||
return; | ||
} | ||
|
||
ChatMessage chatMessage = new ChatMessage(); | ||
chatMessage.setId(122);//dummy | ||
chatMessage.setMessage(messageText); | ||
chatMessage.setDate(DateFormat.getDateTimeInstance().format(new Date())); | ||
chatMessage.setMe(true); | ||
|
||
messageET.setText(""); | ||
|
||
displayMessage(chatMessage); | ||
} | ||
}); | ||
} | ||
|
||
public void displayMessage(ChatMessage message) { | ||
adapter.add(message); | ||
adapter.notifyDataSetChanged(); | ||
scroll(); | ||
} | ||
|
||
private void scroll() { | ||
messagesContainer.setSelection(messagesContainer.getCount() - 1); | ||
} | ||
|
||
private void loadDummyHistory(){ | ||
|
||
chatHistory = new ArrayList<ChatMessage>(); | ||
|
||
ChatMessage msg = new ChatMessage(); | ||
msg.setId(1); | ||
msg.setMe(false); | ||
msg.setMessage("Hi"); | ||
msg.setDate(DateFormat.getDateTimeInstance().format(new Date())); | ||
chatHistory.add(msg); | ||
ChatMessage msg1 = new ChatMessage(); | ||
msg1.setId(2); | ||
msg1.setMe(false); | ||
msg1.setMessage("How r u doing???"); | ||
msg1.setDate(DateFormat.getDateTimeInstance().format(new Date())); | ||
chatHistory.add(msg1); | ||
|
||
adapter = new ChatAdapter(ChatActivity.this, new ArrayList<ChatMessage>()); | ||
messagesContainer.setAdapter(adapter); | ||
|
||
for(int i=0; i<chatHistory.size(); i++) { | ||
ChatMessage message = chatHistory.get(i); | ||
displayMessage(message); | ||
} | ||
} | ||
} |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.