So I've been searching a long time for this, trying out a lot of stuff, but it doesn't seem to work... I've recently started programming for Android, and I'm making a chat-application for a website.
Now I've come to a point where I want to add smileys/emoticons to the application, and by searching I found these websites: http://blog.stylingandroid.com/archives/177 http://www.coderanch.com/t/488673/Android/Mobile/styling-items-ListView
My code:
private void updateList()
{
ListView list = this.getListView();
if(!parsedData.isEmpty())
{
ArrayAdapter<String> adapter = new MyAdapter(this, R.layout.list_item, parsedData);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
}
}
class MyAdapter extends ArrayAdapter<String>
{
ArrayList<String> mStrings;
LayoutInflater mInflater;
public MyAdapter(Context context, int textViewResourceId, ArrayList<String> parsedData)
{
super(context, textViewResourceId, parsedData);
mStrings = parsedData;
mInflater = (LayoutInflater) HopeloosChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.main, null, false);
}
TextView text = (TextView)convertView.findViewById(R.id.TextView);
SpannableStringBuilder ssb = new SpannableStringBuilder("Test ");
Bitmap emoticon = BitmapFactory.decodeResource( getResources(), R.drawable.icon);
ssb.setSpan(new ImageSpan(emoticon), 5, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
text.setText(ssb, BufferType.SPANNABLE);
return convertView;
}
}
I do use setContentView(R.layout.main); in the OnCreate()-method
My main.xml contains the ListView part:
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
And list_item.xml contains the TextView:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:autoLink="web"
android:linksClickable="true" >
</Textview>
When I run my code, I get a Nullpointer because text is null, and I'm kinda stuck. Could anyone point me in the right direction or help me a bit?
Thanks in advance!