Thursday 25 August 2011

How to Add Smiley/Emojis in Edittext?

Emoji.java :-


package com.android.emoji;

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

public class Emoji extends Activity implements OnClickListener {
    EditText edttxtemoji;
    Button btnsubmit, btnselectemoji;
    TextView txtviewdisplay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edttxtemoji = (EditText) findViewById(R.id.edttxtemoji);
        btnsubmit = (Button) findViewById(R.id.btnok);
        btnsubmit.setOnClickListener(this);
        btnselectemoji = (Button) findViewById(R.id.btnselect);
        btnselectemoji.setOnClickListener(this);
        txtviewdisplay = (TextView) findViewById(R.id.txtviewdisplay);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btnsubmit) {
            txtviewdisplay.setText(edttxtemoji.getText());
        } else if (v == btnselectemoji) {
            Intent in = new Intent(this, Select_Emoji_Activity.class);
            startActivity(in);
        }
    }

    CharSequence cs;
    Custom_List_Emoji cstlistemoji;
    int index;

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

        cstlistemoji = new Custom_List_Emoji(this);
        @SuppressWarnings("static-access")
        SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
                this.MODE_WORLD_READABLE);
        index = myPrefs.getInt("key1", 0);
        System.out.println("Pref Data index is:- " + index);
        ImageGetter imageGetter = new ImageGetter() {
            public Drawable getDrawable(String source) {
                Drawable d = getResources().getDrawable(
                        cstlistemoji.images[index]);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                return d;
            }
        };
        cs = Html.fromHtml(
                "<img src='"
                        + getResources()
                                .getDrawable(cstlistemoji.images[index])
                        + "'/>", imageGetter, null);
        System.out.println("cs is:- " + cs);
        edttxtemoji.setText(cs);
    }
}


Select_Emoji_Activity.java :-


package com.android.emoji;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Select_Emoji_Activity extends Activity implements OnItemClickListener{
    ListView lstviewemojis;
    Custom_List_Emoji cstlistemoji;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emojis);
        lstviewemojis=(ListView)findViewById(R.id.lstviewemojis);
        lstviewemojis.setOnItemClickListener(this);
        cstlistemoji=new Custom_List_Emoji(this);
        lstviewemojis.setAdapter(cstlistemoji);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
        System.out.println("Emoji is:- " +arg2);
        prefsEditor.putInt("key1", arg2);
        prefsEditor.commit();
        finish();
    }
}

Custom_List_Emoji :-


package com.android.emoji;

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;
import android.widget.TextView;

public class Custom_List_Emoji extends BaseAdapter {

    public Activity context;
    public LayoutInflater inflater;

    public Custom_List_Emoji(Activity context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return emojis.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        String temp = emojis[position];
        return temp;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    TextView txtview1;
    ImageView image;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_emojis, null);
            txtview1 = (TextView) convertView.findViewById(R.id.txtviewemojis);
            image = (ImageView) convertView.findViewById(R.id.imgviewemoji);
        }

        image.setImageResource(images[position]);
        txtview1.setText(emojis[position]);
        return convertView;
    }

    public final int[] images = new int[] { R.drawable.emo_im_happy,
            R.drawable.emo_im_sad, R.drawable.emo_im_winking,
            R.drawable.emo_im_tongue_sticking_out, R.drawable.emo_im_surprised,
            R.drawable.emo_im_kissing, R.drawable.emo_im_yelling,
            R.drawable.emo_im_cool, R.drawable.emo_im_money_mouth,
            R.drawable.emo_im_foot_in_mouth, R.drawable.emo_im_embarrassed,
            R.drawable.emo_im_angel, R.drawable.emo_im_undecided,
            R.drawable.emo_im_crying, R.drawable.emo_im_lips_are_sealed,
            R.drawable.emo_im_laughing, R.drawable.emo_im_wtf };
    public final String[] _strEmoticonLabels = new String[] { "Happy", "Sad",
            "Winking", "Tongue sticking out", "Surprised", "Kissing",
            "Yelling", "Cool", "Money Mouth", "Foot in mouth", "Embarrased",
            "Angel", "Undecided", "Crying", "Lips are sealed", "Laughing",
            "Confused" };
    public final String[] emojis = new String[] { ":-)", ":-(", ";-)", ":-P",
            "=-O", ":-*", ":O", "B-)", ":-$", ":-!", ":-[", "O:-)", ":-\\",
            ":'(", ":-X", ":-D", "o_O" };

}




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">
    <TextView android:layout_width="wrap_content" android:text="Dipak"
        android:layout_height="wrap_content" android:id="@+id/txtviewdisplay" />
    <EditText android:layout_width="fill_parent" android:layout_below="@+id/txtviewdisplay"
        android:layout_height="wrap_content" android:id="@+id/edttxtemoji" />
    <Button android:layout_width="wrap_content" android:layout_below="@+id/edttxtemoji"
        android:layout_height="wrap_content" android:id="@+id/btnok"
        android:text="Submit" />
    <Button android:layout_width="wrap_content" android:layout_below="@+id/edttxtemoji"
        android:layout_height="wrap_content" android:id="@+id/btnselect"
        android:text="select Emojis" android:layout_toRightOf="@+id/btnok"/>
</RelativeLayout>


emojis.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">
    <ListView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/lstviewemojis" />
</RelativeLayout>


custom_emojis.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">
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/mRlayout1">
        <ImageView android:layout_width="wrap_content" android:id="@+id/imgviewemoji"
            android:layout_height="wrap_content" android:src="@drawable/icon"
            android:layout_centerVertical="true" android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dip" />
        <TextView android:layout_width="wrap_content"
            android:layout_centerVertical="true" android:layout_marginRight="10dip"
            android:layout_height="wrap_content" android:text="Emoji"
            android:layout_alignParentRight="true" android:id="@+id/txtviewemojis" />
    </RelativeLayout>
</RelativeLayout>


60 comments:

  1. hi, can u post the zip file of the images in the drawables file. because i have errors the i cant see the images

    ReplyDelete
    Replies
    1. Hi you can find the emoticons in android-sdk folder :
      android-sdks/platforms/android-16/data/res/drawable

      Delete
    2. Hi this code is really help full for me, i have one doubt in that code select the images from the drawable if there is any possible pick the image from gallery as like this.

      Delete
    3. Hello sir,I have one doubt in your code , You did select the images from the drawable folder if there is any possible to pick the images from the gallery as like this u r code .pls u can post the code to below mail ID.


      www.hubino.testing@gmail.com

      Delete
  2. .. hi .we have a graduate project could u help us??
    we wanna design a new keyboard for sign language. to help ppl translate from sign language to other normal language ..
    my question is .. how we could display an image(sign letter)in text box when the user click on a button??
    and how we could save images(sign letters) to call it in our app,,

    plz help us and thank u ^^

    ReplyDelete
  3. Hi, Thk for the example.

    But i can not typing multiple emoji in edittext.

    Help me please.

    my e-mail : sirakarnit@gmail.com >>>> please T______T

    ReplyDelete
    Replies
    1. if u got that answer plz send me...

      Delete
    2. if u got the answer send me please
      yeryadinugroho@gmail.com

      Delete
    3. Instead of setText in onRestart(), add append. That will allow you to add multiple emoticons.

      Delete
  4. hi this is very needful for me but i cnt typing multiple emoji in edittext,,, i need full source code for that task......
    plz send code to my e-mail,,,,

    prabhuaet@gmail.com.....

    ReplyDelete
  5. may i get the source code full from this project sir?

    yeryadinugroho@gmail.com

    ReplyDelete
  6. plz sir can you send full source code
    at satyab.suntech@gmail.com

    ReplyDelete
  7. Hi Dipak, you missed manifest.xml. I need that for my project.

    Looking forward to hearing from you..

    Regards.
    Abdul Rahman.

    ReplyDelete
  8. Hi Dipak, How can i Store smiley with text in database???

    ReplyDelete
  9. Hi Its very good example
    Can You Please Send me source code
    it will help a lot in my career
    My Mail : vidhydhari.ag@gmail.com

    ReplyDelete
  10. hi.....this example really a valueable one....
    for every android developer...Nice1

    ReplyDelete
  11. dipak plz send me full source code i will thnks lot ur saurya6890@gmail.com

    ReplyDelete
  12. hi, this project is needfull for me . pls send to my mail.
    wictor1375@gmail.com

    ReplyDelete
  13. Hi Deepak....Thanks for sharing this code with us....Can You Please Send me full source code
    it will help a lot in my project
    My Mail : erginnisharma@gmail.com

    ReplyDelete
  14. 97 percent of all 800 now go to voicemail. How to get prospects to contact you back (by offering what's in it for them).

    ReplyDelete
  15. Thank you very much.Please send me source code for mainfest xml.

    ReplyDelete
  16. When looking for the top iPhone apps are you really getting the most useful iPhone apps? Or are you really just finding the top marketed iPhone apps? Understand the difference and find the really cool iPad or iPhone apps. spyfone

    ReplyDelete
  17. Innovative ideas in the field of technology have simplified the work and helped our rapid development.These ideas contribute to the creation of innovative technologies over time. In order to create this innovative idea, it is necessary to have the knowledge, which is fundamental in this process. Thus we get the scheme: knowledge, idea, technology. mobile tracker free

    ReplyDelete
  18. Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... One Shot Keto Diet Pills Supplement For Weight ... - AP News

    ReplyDelete
  19. I’m glad to become a visitor in this pure site, regards for this rare info! 먹튀 검증사이트

    ReplyDelete
  20. with thanks with regard to the particular article i have been on the lookout with regard to this kind of advice on the net for sum time these days and so with thanks Caribou website

    ReplyDelete
  21. with thanks with regard to the particular article i have been on the lookout with regard to this kind of advice on the net for sum time these days and so with thanks HHRD

    ReplyDelete
  22. What¡¦s Going down i am new to this, I stumbled upon this I’ve discovered It absolutely useful and it has helped me out loads. I am hoping to give a contribution & aid other users like its helped me. Great job. Matthew Fleeger

    ReplyDelete
  23. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… We Are Caribou

    ReplyDelete
  24. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… Custom Coins

    ReplyDelete
  25. with thanks with regard to the particular article i have been on the lookout with regard to this kind of advice on the net for sum time these days and so with thanks glastonburychamber

    ReplyDelete
  26. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… รีวิวเว็บแทงบอล

    ReplyDelete
  27. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… HHRD

    ReplyDelete
  28. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… Joker123

    ReplyDelete
  29. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… Matthew Fleeger sold his mansion

    ReplyDelete
  30. Ey kya backchodi he MainActivity he nhi or de rukha he .xml or sala program me error hi error he bhai kya bnatey ho kum se kum shai to post kro ajib chutya ho

    ReplyDelete
  31. Buy VCC Online, PayPal VCC, PayPal VBA, USA Real Number, Amazon VCC, Facebook VCC, USA Bank, And other lots of digital products are for sell at chep price , Fully Verified PayPal Accounts, Aged PayPal Accounts, Virtual Credit Card for Paypal Verification, Amazon.com, Exploits, Trials, and Purchases. buy verified paypal accounts

    ReplyDelete
  32. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… #instaketo - YouTube

    ReplyDelete
  33. If you want to be a manager, you have to supervise, guide, and direct. These principles are just as true for construction equipment management. Construction equipment management refers to the proper management of construction equipment on the behalf of a construction business. This article looks at effective management in the construction industry. Mediterranean Food Near Me

    ReplyDelete
  34. Thanks for this post, I am a big fan of this website would like to go on updated. รีวิวufacasino

    ReplyDelete
  35. Lawyers… [...]here are some links to sites that we link to because we think they are worth visiting[...]… Norway Trolltunga

    ReplyDelete
  36. Hey this is a good post. I’m going to mail this to my friends. I stumbled on this while exploring on yahoo I’ll be sure to come back. thanks for sharing. facebook login

    ReplyDelete
  37. There is a wide variety of security camera choices available today. If you are looking to add surveillance cameras to your home security system, you may be a little overwhelmed by all of the options available. However, with a little research you will be able to find the security cameras that best suit your particular situation. Whether you are looking for home surveillance cameras or cameras for your business, you'll want to check out all of the options and make an informed decision. This guide can help diy spy camera wifi 4k

    ReplyDelete
  38. I’ve recently started a blog, the knowledge you provide on this website has reduced the problem tremendously. Thank you for your time & work… bet188

    ReplyDelete
  39. Hey there! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Appreciate it! buy steroids

    ReplyDelete
  40. Can I just now say thats a relief to uncover someone that in fact knows what theyre dealing with online. You actually realize how to bring a problem to light to make it important. Lots more people have to ought to see this and can see this side of your story. I cant believe youre less well-liked since you also absolutely hold the gift. gym clothing

    ReplyDelete
  41. Thank you designed for ad the following, It’s quickly what precisely As i seemed to be looking for for the purpose of upon yahoo. I’d quite a lot reasonably pick up viewpoints at a individual, a bit when compared with a business web page, that’s the reason I really like web logs for that reason a lot. Bless you! banking and finance app development

    ReplyDelete
  42. Thanks for sharing this article. Discovered your website through a friends blog – very informative and entertaining. backlinks to my site

    ReplyDelete
  43. A lot of people were enthusiastic players or enjoyed music and dancing. You could possibly recollect that you were happiest on the performing track. Yet, with increasing obligations possibly you have found no time to indulge in any of an interests. Do you suffer from depression and would like to get free from its abysmal depths without lifelong antidepresants? You could attempt and help yourself to overcome depression naturally. buy sugar paste

    ReplyDelete
  44. You must indulge in a contest for one of the greatest blogs over the internet. Ill suggest this web site! 안전놀이터 모음 세다벳

    ReplyDelete
  45. “Cold Case” is definitely one of the best detective tv shows that you can watch on TV. the story is great“ handmade jewellery nz

    ReplyDelete
  46. Spot lets start on this write-up, I seriously think this excellent website wants much more consideration. I’ll probably be once more to study far more, many thanks for that information. buyback

    ReplyDelete
  47. Nice blog here! Also your website quite a bit up fast! What host are you the usage of? Can I get your associate link to your host? I desire my web site loaded up as fast as yours lol. acrylic charms

    ReplyDelete
  48. I genuinely enjoy looking through on this web site , it holds superb articles . where to buy steroids

    ReplyDelete
  49. I appreciate, cause I found exactly what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a great day. Bye buy clenbuterol

    ReplyDelete
  50. People: LeBron James is nowhere near the competitor that Michael Jordan was. Quit comparing the two.|whereishawkins| sonic the hedgehog winter coat

    ReplyDelete
  51. Hello there! Do you use Twitter? I’d like to follow you if that would be okay. I’m absolutely enjoying your blog and look forward to new posts. kempox streaming

    ReplyDelete
  52. Nice post. I find out something more difficult on distinct blogs everyday. It will always be stimulating you just read content judi baccarat online using their company writers and use a little from their site.

    ReplyDelete