Like Us on Facebook

How to Create Chat Application in Android Studio

Hi Friends, for Specific demand  I am going to Share  How to create chat Application in Android Studio with Source code this is simple chat Application Demo example , basically give you concept how to create  application  front end. Later in upcomming  Post I will explain how Chat Application works  including server part.

Read More
How to Create Chat Application using Firebase in Android
How to make Realtime chat Application in Android
How to create push notification in Android Studio
How  to make Speech to Text conversion App in Android

Prerequisites:
  • JDK 6.0 or above
  • Android Studio
  • Nine patch images

Steps to follow:  
  1. Create a New Android Studio project name Chatappsdemo
  2. Select minimum  API level 16 so it will run 82.6% of Android device that are Active on google Play then click next.
  3. Select Blank Activity and your Activity Name is MainActivity click next > click finish.
  4. Inside  MainActivity  write the fllowing source code.
 
 public class MainActivity extends Activity {
 private static final String TAG = "ChatActivity";
 private ChatArrayAdapter adp;
 private ListView list;
 private EditText chatText;
 private Button send;
 Intent intent;
 private boolean side = false;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Intent i = getIntent();
 setContentView(R.layout.main);
 send = (Button) findViewById(R.id.btn);
 list = (ListView) findViewById(R.id.listview);
 adp = new ChatArrayAdapter(getApplicationContext(), R.layout.chat);
 list.setAdapter(adp);
 chatText = (EditText) findViewById(R.id.chat_text);
 chatText.setOnKeyListener(new OnKeyListener() {
 public boolean onKey(View v, int keyCode, KeyEvent event) {
 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode ==  
 KeyEvent.KEYCODE_ENTER)) {
 return sendChatMessage();
 }
 return false;
 }
 });
 send.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View arg0) {
 sendChatMessage();
 }
 });
 list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
 list.setAdapter(adp);
 adp.registerDataSetObserver(new DataSetObserver() {
 public void OnChanged(){
 super.onChanged();
 list.setSelection(adp.getCount() -1);
  }
  });
  }
 private boolean sendChatMessage(){
 adp.add(new ChatMessage(side, chatText.getText().toString()));
 chatText.setText("");
 side = !side;
 return true;
 }
 }
  
 


5. Now it will ask you to create a new Class ChatArrayAdapter.java. write the folowing code inside it
        
 public class ChatArrayAdapter extends ArrayAdapter<ChatMessage>{
 private TextView chatText;
 private List<ChatMessage> MessageList = new ArrayList<ChatMessage>();
 private LinearLayout layout;
 public ChatArrayAdapter(Context context, int textViewResourceId) {
 super(context, textViewResourceId)
 public void add(ChatMessage object) {
 // TODO Auto-generated method stub
                               
 MessageList.add(object);
 super.add(object);
 }
  
  public int getCount()
 {
   return this.MessageList.size();
 }
               
  public ChatMessage getItem(int index){
                                 
  return this.MessageList.get(index);
  }
                 
 public View getView(int position,View ConvertView, ViewGroup parent){
                                 
 View v = ConvertView;
 if(v==null){
                                                  
 LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 v =inflater.inflate(R.layout.chat, parent,false);
                                                  
 }
                                 
 layout = (LinearLayout)v.findViewById(R.id.Message1);
 ChatMessage messageobj = getItem(position);
 chatText =(TextView)v.findViewById(R.id.SingleMessage);
 chatText.setText(messageobj.message);
 chatText.setBackgroundResource(messageobj.left ? R.drawable.bubble_a :R.drawable.bubble_b);
 layout.setGravity(messageobj.left?Gravity.LEFT:Gravity.RIGHT);
 return v;
 }

 public Bitmap decodeToBitmap(byte[] decodedByte) {
 return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
 }
 }


 


 
 6. Now insert the nine patch images bubble_a , bubble_b inside the drawable folder



 7.Create Class ChatMessage.java Inside it write
 
 public class ChatMessage {
 public boolean left;
 public String message;

 public ChatMessage(boolean left , String message) {
 // TODO Auto-generated constructor stub
 super();
 this.left=left;
 this.message = message;
 }
 } 
 

8. And these are my Xml files
main.xml
                   
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"

   android:orientation="vertical"
  >
 
 <ListView
   android:id="@+id/listview"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginBottom="80dp"
   />
   
  <RelativeLayout
   android:id="@+id/form"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:orientation="vertical"
   >
       
 <EditText
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:inputType="textMultiLine"
   android:ems="10"
   android:id="@+id/chat_text"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_toLeftOf="@+id/btn"
    />
       
 <Button
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="Send"  
   android:id="@+id/btn"
   android:layout_alignBottom="@+id/chat_text"
   android:layout_alignParentRight="true"
    />
       
 </RelativeLayout>
       
  </RelativeLayout>
   


  Chat.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
   
 <LinearLayout
  android:id="@+id/Message1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
   >
 <TextView
  android:id="@+id/SingleMessage"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_margin="6dp"
  android:background="@drawable/bubble_b"
  android:paddingLeft="10dip"
  android:text="Hello friends"
  android:textColor="@android:color/primary_text_light"
   />
 </LinearLayout>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 49 )

Faradost Mehrdad:
hi very good
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Amol Wangate:
excellent tutorial.... can u give me server part source file ?
Admin:
Download source code from above link.
Firoj Mohammad:
excellent tutorial.... can u give me server part source file ?
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Firoj Mohammad:
firoj.devpoint@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Sachin Verma:
where is server side code?? can i get it??
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Meghna Shinde:
Can you give me server side code at meghashine22@ Gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
James Momoh:
excellent tutorial.... can u give me server part source file ?
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
James Momoh:
excellent tutorial.... can u give me server part source file ? jaymomoh@gmail.com Thank You very much
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
shuaib ahmed:
can u give me server part source file shuaibahmed878@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Milind Rohit:
Can you give me server side code at milind0359@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Prashant Patil:
plz send me server side code at prashantpatil2kl32@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
hafiz chauhan:
awesome tutorial please i need the source code for chatting app. Hafiz.chauhan@hotmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Moe Lewis:
Can you give me the server side code @ mblewis84@yahoo.com and the nine patch images. Thank You
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Abhay Ganjoo:
Can i have the server side code@ abhaybbmaster@gmail.com Thankyou.
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Jemu Patel:
Very good.Please Can you give me server side source code jenishpatel879@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
tariq ali:
thanks a looot.......plesase send me the server side code ... afaqtara@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ruben De Luca:
Thanks for sharing! Just a couple of questions: Did you use simple pushnotification to send message? Didnt you have build any Service to handle pushnotification? Thanks in advance
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
carlos mollapaza:
where is the project chatserver
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
shivashankar bhuvaneshwaran:
please send the android source code for make group chat application using android studio
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Mukesh Jadhav:
can i have full source code
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Vishal Kumar:
Vishal Singh please send the server side code. My email id is vishal25791@gmail.com Thanks in advance
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Eden Hazard:
excellent.... can u give me server part source code sir? maruf@psn.co.id, TY
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Sandhya Bairi:
Sir, can you please send me the server side code at sandhya.bairi154@gmail.com. Thank you.
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Sandhya Bairi:
Sir,can you please send me the server side source code? It would be very helpful. My email: sandhya.bairi154@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Sunghoon Yoon:
wow!!! excellent!! can u give me server part source file ? yshlove114@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Rutvik Shah:
Pls send me server side source code...rhutvik@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
jay joshi:
Please send server side code on jay.nodib@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Dari capun:
excellent work, thank you very much.... can u give me server part source code please?, my email: dfa17@hotmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Dari capun:
Please send server side code on dfa17@hotmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Chandan Bhushan:
please give me the server side source code, bhushan2chandan@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ethan Dominique Martinez:
send the server side code please! ethandomz3@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ethan Dominique Martinez:
send the server code please on ethandomz3@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ethan Dominique Martinez:
the server side please send to ethandomz3@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ethan Dominique Martinez:
please send the server code at ethandomz3@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
heba elsayed:
excellent tutorial.... can u give me server part source file please? To : pop.eng33@yahoo.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Still Daun:
great tutorial, can i get the server side source code please? band_age4eva@yahoo.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
yoga kanlam:
Hi Admin... Can you send me the source code yoga.kanlam@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Boakye Yaw:
Hi..That is a great tutorial.. can u give me server part source file please? To : scholarprogrammer89@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ibtihel Gharbi:
excellent tutorial.... can u give me server part source file please? ibtihel2bibi@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Ali Daulay:
hi...excellent tutorial. can u give me server part source file please? ali.hudzaifah@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
اسامہ کیانی:
HI excellent tutorial can you give me server side code please? usama.kiyani93@yahoo.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
dario vicenzo:
exellent tutorial.. can give me server part source and front end also? thanks dario.vicenzo74@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
John Nikson:
Nice kindly send me the server part source and front end also? thanks johnnikson2012@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Vincent Van de Wouw:
Great tutorial, could you send me the Server side code please? vincewouw@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
Šłìmãñ Mâřáê:
If you can send me the source code i will be very thankful. This is my gmail : solimarae06@gmail.com
Admin:
thanx to all of you ... you can download server side source code from how to create realtime chat application in Android
raja mohamed:
useless chat application on androi studio,if i ave a own server how do implement the chat on android stuio,where is the code part for connect server using android studio
Admin:
see How to create Chat Application Using Firebase tutorial
reddymams mams:
Great tutorial, could you send me the Server side code please? nireyrecha@gmail.com
Admin:
see How to create Realtime chat Application or How to Create Chat Application Using Firebase
mohmamd alhamed:
i wanna talk with you ( gnlyo10@gmail.com ) about app
Admin:
what u want to talk?
Luann Lawrence:
Hi,boos can we chat

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also