I’m trying to created a chat application based on smack library. I think my issue has nothing to do with this library as logcat sends and receives everything correctly. My problem is when i receive a message it does not display. Following is the method i’m using to display text.
public void TextCreating(String message) {
Log.d("START", "Method Excution started.");
layout = (LinearLayout) findViewById(R.id.layoutLinear);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
tv = new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(message);
tv.setTextColor(Color.WHITE);
Log.d("STARTED", "Text color OK.");
layout.addView(tv);
Log.d("STARTED", "Text didsplayed.");
}
This peace of code will be executed as a message received. Those received messages are handled through a listener called PacketListener. This receiving part is working fine. In this listener when i call above TextCreating method it executes up to tv.setTextColor(Color.WHITE); Last line does not executes. What is the reason for this? How can I solve this problem ? Thanks in advance 🙂
EDIT :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat Room"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Chat History :" />
<LinearLayout
android:id="@+id/layoutLinear"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:background="#000000"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp" >
</LinearLayout>
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
First thank u every one for your ideas and helps. I solver this by introducing a Handler to the code. I was trying to do everything in my UI thread. So i modified my code as follows.
This link helped me a lot to figureout this.
Update textView from thread