• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Change UI from thread

9Ate7

Newbie
May 9, 2012
33
0
Hi,

Doing some simple android networking. My problem is as stated above, i would like to change the UI from a separate thread.

What i would like to do is add "Client successfully connected" to the EditText i have. The connection to the server is handled by a separate thread, and that is why i would like to do this. Another reason is, i have a different thread always listening for to what the server says, and what ever the server does say i would like to show it in the EditText.

I have tried many methods, how ever no luck. That is why i am here.

I always get the following error:
Only the original thread that created a view hierarchy can touch its views

Thanks for any advice.

EDIT: Ran into another problem. My code involves socket connections. It works just fine on the emulator. How ever it doesn't work on my phone. It was working a day or two ago, then just magically stopped. LogCat simply says permission denied.
 
Right, but if i am constantly listening to the server, then that makes my whole application stuck in the loop that keeps listening to the server. If there is a loop inside the run method, then the UI thread is stuck in that method and the UI is no longer responsive.

I'm not following you. The only code you want in run method for runOnUiThread() is code that updates your UI.
 
Upvote 0
Just tried to knock something up quickly.

[HIGH]public class MainActivity extends Activity {

TextView textView;
EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
editText = (EditText)findViewById(R.id.editText1);
runServer();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public void setText(final String text){
runOnUiThread(new Runnable() {
public void run()
{
textView.setText(text);
}
});
}

public void runServer(){

Thread thread = new Thread(){
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(8090);
while (true){
Socket client = server.accept();
setText("connection from " + client.getInetAddress());
client.close();
}

}
catch (Exception e){
System.out.println(e.toString());
}
}

};
thread.start();
}
}

[/HIGH]

Maybe I'd missing something but EditText remains usable. Running it on my phone (I don't know how to set the emulator up), I can connect and textView gets updated:

jon@worthy:~> telnet 192.168.0.228 8090
Trying 192.168.0.228...
Connected to 192.168.0.228.
Escape character is '^]'.
Connection closed by foreign host.
 
Upvote 0
Thanks! i had this problem for over 2 days and could not find a way around it.

But DAT PUBLIC VOID SET TEXT... That method did the trick.

But you say the EditText remains uneditable? even when you tried the setText method with it?

I just tried it, and it seems to work. However Thanks again. This solved a big issue for me.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones