Simple Android Chat Application, Customer Side.

Posted on

It’s customer side of Simple Android Chat Application, for the server side, refer lastly ship service “Implement elementary Android Chat Application, server side“.


MainActivity.java

package com.example.androidchatclient;  import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException;  import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;  world course of report MainActivity extends ActionBarActivity {    static lastly int SocketServerPORT = 8080;    LinearLayout loginPanel, chatPanel;   EditText editTextUserName, editTextAddress;  Button buttonConnect;  TextView chatMsg, textPort;    EditText editTextSay;  Button buttonSend;  Button buttonDisconnect;   String msgLog = "";    ChatClientThread chatClientThread = null;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);      loginPanel = (LinearLayout)findViewById(R.id.loginpanel);   chatPanel = (LinearLayout)findViewById(R.id.chatpanel);    editTextUserName = (EditText) findViewById(R.id.username);   editTextAddress = (EditText) findViewById(R.id.address);   textPort = (TextView) findViewById(R.id.port);   textPort.setText("port: " + SocketServerPORT);   buttonConnect = (Button) findViewById(R.id.connect);   buttonDisconnect = (Button) findViewById(R.id.disconnect);   chatMsg = (TextView) findViewById(R.id.chatmsg);    buttonConnect.setOnClickListener(buttonConnectOnClickListener);   buttonDisconnect.setOnClickListener(buttonDisconnectOnClickListener);      editTextSay = (EditText)findViewById(R.id.say);   buttonSend = (Button)findViewById(R.id.send);      buttonSend.setOnClickListener(buttonSendOnClickListener);  }    OnClickListener buttonDisconnectOnClickListener = novel OnClickListener() {    @Override   world void onClick(View v) {    if(chatClientThread==null){     return;    }    chatClientThread.disconnect();   }     };    OnClickListener buttonSendOnClickListener = novel OnClickListener() {    @Override   world void onClick(View v) {    if (editTextSay.getText().toString().equals("")) {     return;    }        if(chatClientThread==null){     return;    }        chatClientThread.sendMsg(editTextSay.getText().toString() + "\n");   }     };   OnClickListener buttonConnectOnClickListener = novel OnClickListener() {    @Override   world void onClick(View v) {    String textUserName = editTextUserName.getText().toString();    if (textUserName.equals("")) {     Toast.makeText(MainActivity.this, "Enter User Name",       Toast.LENGTH_LONG).show();     return;    }     String textAddress = editTextAddress.getText().toString();    if (textAddress.equals("")) {     Toast.makeText(MainActivity.this, "Enter Addresse",       Toast.LENGTH_LONG).show();     return;    }        msgLog = "";    chatMsg.setText(msgLog);    loginPanel.setVisibility(View.GONE);    chatPanel.setVisibility(View.VISIBLE);     chatClientThread = novel ChatClientThread(      textUserName, textAddress, SocketServerPORT);    chatClientThread.start();   }   };   mortal course of report ChatClientThread extends Thread {    String name;   String dstAddress;   int dstPort;      String msgToSend = "";   boolean goOut = false;    ChatClientThread(String name, String address, int port) {    this.name = name;    dstAddress = address;    dstPort = port;   }    @Override   world void run() {    Socket socket = null;    DataOutputStream dataOutputStream = null;    DataInputStream dataInputStream = null;     endeavor {     socket = novel Socket(dstAddress, dstPort);     dataOutputStream = novel DataOutputStream(       socket.getOutputStream());     dataInputStream = novel DataInputStream(socket.getInputStream());     dataOutputStream.writeUTF(name);     dataOutputStream.flush();      spell (!goOut) {      if (dataInputStream.available() > 0) {       msgLog += dataInputStream.readUTF();        MainActivity.this.runOnUiThread(new Runnable() {         @Override        world void run() {         chatMsg.setText(msgLog);        }       });      }            if(!msgToSend.equals("")){       dataOutputStream.writeUTF(msgToSend);       dataOutputStream.flush();       msgToSend = "";      }     }     } grab (UnknownHostException e) {     e.printStackTrace();     lastly String eString = e.toString();     MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();      }           });    } grab (IOException e) {     e.printStackTrace();     lastly String eString = e.toString();     MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();      }           });    } finally {     if (socket != null) {      endeavor {       socket.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }      if (dataOutputStream != null) {      endeavor {       dataOutputStream.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }      if (dataInputStream != null) {      endeavor {       dataInputStream.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }      MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       loginPanel.setVisibility(View.VISIBLE);       chatPanel.setVisibility(View.GONE);      }           });    }    }      mortal void sendMsg(String msg){    msgToSend = msg;   }      mortal void disconnect(){    goOut = true;   }  }  } 

/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <TextView         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Char Client"         android:textStyle="bold" />      <LinearLayout         android:id="@+id/loginpanel"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         android:visibility="visible" >          <EditText             android:id="@+id/username"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:hint="User name" />          <EditText             android:id="@+id/address"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:hint="dstAddress" />          <TextView             android:id="@+id/port"             android:layout_width="match_parent"             android:layout_height="wrap_content" />          <Button             android:id="@+id/connect"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="Connect..." />     </LinearLayout>   <LinearLayout         android:id="@+id/chatpanel"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         android:visibility="gone" >                  <EditText             android:id="@+id/say"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:hint="Say something" />               <Button             android:id="@+id/send"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="Send" />                  <Button             android:id="@+id/disconnect"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="Disconnect" />          <ScrollView             android:layout_width="match_parent"             android:layout_height="match_parent" >              <TextView                 android:id="@+id/chatmsg"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content" />         </ScrollView>     </LinearLayout>  </LinearLayout> 

Permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml.

Implement elementary Android Chat Application Simple Android Chat Application, customer side.Download the files.