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 

Monday, September 17, 2012

Exapand - Collapse Android TextView.

Hello,
Here I want to share some knowledge about android textview. Android Textview is a very Useful element in your application.
If you want to make functionality like expand - collapse of textview it is easy after end of this article you can make this functionality.

I guess,you have a basic knowledge of Android technology. Than you can easily grasp this article

e.g. If you want to expand or collapse your textview like in  below is image of Play store app.



Here I am only describe that how more/less button functionality are work. 
first of all in your activity_main.xml file paste below code.
activity_main.xml

<ScrollView 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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/description_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="5"
            android:text="@string/desc_content" />

        <ImageButton
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/description_text"
            android:background="@drawable/arrow_down"
            android:clickable="true" />

        <View
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_below="@+id/description_text"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/show"
            android:background="#000" />

        <ImageButton
            android:id="@+id/hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/description_text"
            android:background="@drawable/arrow_up"
            android:clickable="true"
            android:visibility="invisible" />
    </RelativeLayout>

</ScrollView>


now open your main java file and paste below code
MainActivity.java

package com.example.expand.textview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView descText;
ImageButton show, hide;

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

descText = (TextView) findViewById(R.id.description_text);
show = (ImageButton) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("Show button");
show.setVisibility(View.INVISIBLE);
hide.setVisibility(View.VISIBLE);
descText.setMaxLines(Integer.MAX_VALUE);

}
});
hide = (ImageButton) findViewById(R.id.hide);
hide.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("Hide button");
hide.setVisibility(View.INVISIBLE);
show.setVisibility(View.VISIBLE);
descText.setMaxLines(5);

}
});

}

}

and finally change your string.xml file
string.xml

<resources>

    <string name="app_name">Expand TextView</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="desc_content">    Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It\'s the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.

Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.
</string>

</resources>

and run your Project It will give you below output.