Android Open Source - SimpleAndroidChat Chat Activity






From Project

Back to project page SimpleAndroidChat.

License

The source code is released under:

GNU General Public License

If you think the Android project SimpleAndroidChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anderscore.simpleandroidchat;
//ww w.j a  v a2 s .  c  o m
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.anderscore.simpleandroidchat.Constants.Extra;

public class ChatActivity extends AbstractActivity {
  ListView list;
  ChatListAdapter listAdapter;
  EditText eTextChatMessage;
  Button btnSendMessage;
  Contact contact;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_chat_list);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    list = (ListView) findViewById(android.R.id.list);
    list.setEmptyView(findViewById(android.R.id.empty));
    eTextChatMessage = (EditText) findViewById(R.id.eTextChatMessage);
    btnSendMessage = (Button) findViewById(R.id.btnSendMessage);
    listAdapter = new ChatListAdapter(this);
    list.setAdapter(listAdapter);
    btnSendMessage.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View v) {
        String msgText = eTextChatMessage.getText().toString();
        if (!msgText.equals("")) {
          eTextChatMessage.getText().clear();
          ChatMsg msg = new ChatMsg(contact.getUser(), contact.getId(), false, msgText);
          ChatMsg showMsg = ChatActivity.this.mBinder.sendMsg(msg);
          listAdapter.updateList(showMsg);
          InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(eTextChatMessage.getWindowToken(), 0);
        }
      }
    });
    super.onCreate(savedInstanceState);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      NavUtils.navigateUpFromSameTask(this);
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  @Override
  void onServiceAvailable() {
    int userID = getIntent().getIntExtra(Extra.USER_ID, 0);
    contact = mBinder.getContact(userID);
    listAdapter.updateList(contact.getChatHistory());
  }

  @Override
  void notifyContact(Contact contact) {
    // do nothing
  }  
  @Override
  void notifyMsg(ChatMsg msg) {
    if (msg.getUserId() == contact.getId()) {
      listAdapter.updateList(msg);
    }
  }
}




Java Source Code List

com.anderscore.simpleandroidchat.AbstractActivity.java
com.anderscore.simpleandroidchat.ChatActivity.java
com.anderscore.simpleandroidchat.ChatListAdapter.java
com.anderscore.simpleandroidchat.ChatMsg.java
com.anderscore.simpleandroidchat.ConnectionAdapterEventbus.java
com.anderscore.simpleandroidchat.ConnectionAdapter.java
com.anderscore.simpleandroidchat.Constants.java
com.anderscore.simpleandroidchat.ContactListActivity.java
com.anderscore.simpleandroidchat.ContactListAdapter.java
com.anderscore.simpleandroidchat.Contact.java
com.anderscore.simpleandroidchat.DBConnection.java
com.anderscore.simpleandroidchat.DBModel.java
com.anderscore.simpleandroidchat.MessengerService.java
com.anderscore.simpleandroidchat.NotificationBuilder.java