Sunday, September 23, 2012

Android Chat Application

In Today's Smart Phone Life Mobile Chat is very popular and In this  Article I will explain about android chat application.Not a whole scenario but some part of the chatting application like how we can add a emoticon in to your edittext and displayed in to the screen.

end of the document i post a download full source code.Here Below some Java file and screen shots is available
MainActivity.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.example.mysmiley;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
 EditText display, writeboard;
 Button submit, select,clear;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  initUiElement();
  
  submit.setOnClickListener(this);
  select.setOnClickListener(this);
  clear.setOnClickListener(this);
 }

 private void initUiElement() {
  display = (EditText) findViewById(R.id.display);
  writeboard = (EditText) findViewById(R.id.writeboard);
  submit = (Button) findViewById(R.id.btnsubmit);
  select = (Button) findViewById(R.id.btnselect);
  clear = (Button)findViewById(R.id.btnclear);
 }

 CharSequence cs;
 CustomEmojis emojis;
 int index;

 @Override
 protected void onRestart() {
  super.onRestart();
  emojis = new CustomEmojis(this);

  SharedPreferences preferences = this.getSharedPreferences("pref",
    this.MODE_WORLD_READABLE);
  index = preferences.getInt("smiley", 0);
  System.out.println("smiley index is---> " + index);

  ImageGetter imageGetter = new ImageGetter() {

   @Override
   public Drawable getDrawable(String source) {
    Drawable d = getResources().getDrawable(emojis.images[index]);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    return d;
   }
  };
  cs = Html.fromHtml(
    "<img src ='"
      + getResources().getDrawable(emojis.images[index])
      + "'/>", imageGetter, null);
  writeboard.setText(cs);
  int position = writeboard.length();
  Editable editable = writeboard.getText();
  Selection.setSelection(editable, position);
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.btnsubmit:
   display.setText(display.getText().append(writeboard.getText()));
   writeboard.setText("");
   break;
  case R.id.btnselect:
   Intent i = new Intent(MainActivity.this, EmojiSelection.class);
   startActivity(i);
   break;
  case R.id.btnclear:
   display.setText("");
   break;
  
  default:
   break;
  }

 }

}

After main file creation you need to create another activity EmojiSelection Activity. in this activity you show that how the emoji will selected using shared preference.

EmojiSelection.java





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.mysmiley;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class EmojiSelection extends Activity {
 public static final String TAG = "EmojiSelection";

 GridView gridView;
 CustomEmojis customEmojis;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.emojis);

  initUIElement();

  customEmojis = new CustomEmojis(this);
  gridView.setAdapter(customEmojis);

  gridView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1,
     int position, long arg3) {

    Log.i(TAG, "U are in OnItemSelected");
    SharedPreferences preferences = EmojiSelection.this
      .getSharedPreferences("pref", MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putInt("smiley", position);
    System.out.println("Selected emojis ---> " + position);

    // dont forgot to commit preference
    editor.commit();

    finish();
   }
  });
 }

 private void initUIElement() {
  gridView = (GridView) findViewById(R.id.gridview1);

 }

}

in my example i am create custom gridview class for the displaying the emojis. In below code you can see.
CustomEmojis.java



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.mysmiley;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CustomEmojis extends BaseAdapter {
 private Activity activity;
 private static LayoutInflater inflater = null;

 public final int[] images = new int[] { R.drawable.a, R.drawable.b,
   R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f,
   R.drawable.g, R.drawable.h, R.drawable.j, R.drawable.k,
   R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o,
   R.drawable.p, R.drawable.q, R.drawable.r, R.drawable.s,
   R.drawable.t, R.drawable.u, R.drawable.v, R.drawable.w,
   R.drawable.x, R.drawable.y, R.drawable.z, R.drawable.aa,
   R.drawable.bb, R.drawable.cc, R.drawable.dd, R.drawable.ee,
   R.drawable.ff, R.drawable.gg, R.drawable.hh, R.drawable.ii,
   R.drawable.jj, R.drawable.kk, R.drawable.ll, R.drawable.mm,
   R.drawable.nn, R.drawable.oo, };

 public CustomEmojis(Activity act) {
  activity = act;
  inflater = (LayoutInflater) activity
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 @Override
 public int getCount() {
  return images.length;
 }

 @Override
 public Object getItem(int position) {
  return position;
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) {
   holder = new ViewHolder();
   convertView = inflater.inflate(R.layout.grid_row, null);
   holder.imageView = (ImageView) convertView
     .findViewById(R.id.imageView1);
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  holder.imageView.setImageResource(images[position]);
  return convertView;
 }

 public static class ViewHolder {
  public ImageView imageView;
 }

}

above i post a 3 useful file for project.now its up to YOU how you can made a chat application in android Now after completion of the project you can see the below screens in you project


you can download a full source code here 

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Replies
    1. @rhardik I have already added project source code

      Delete
  3. Thanks a lot for sharing this!

    ReplyDelete
  4. here it is exe file.. not zip file .
    .
    so u haven't give source code...
    upload that if possible..

    ReplyDelete
  5. how to send more the 2 icons at the same time

    ReplyDelete
  6. Its very useful for chatting application..thank alot :-)

    ReplyDelete
    Replies
    1. how to convert jse file in zip file

      Delete
  7. Its not zil file. whenever i m downloading this, it contains only .jse file.
    Can u upload full source code?

    ReplyDelete
    Replies
    1. if u have download this code so please use utorent
      and please tell me how to convert .jse file in zip..

      Delete