TextView with parent ScrollView + non-UI thread

Put your problem here if it does not fit any of the other categories.

TextView with parent ScrollView + non-UI thread

Postby glock45 » Sat Apr 19, 2008 11:48 pm

Hello

I'am doing a small socket client for android platform. Socket is reciving packets using non-UI-thread. If socket recives a packet, socket-thread adds text to TextView. I'm using my own TextViewUpdater object to make it easy ( TextViewUpdater.AddText("text for TextView")).

My app needs auto-scroll for TextView, so I added a ScrollView as a parent of TextView. Here is a code of my autoscroll:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class TextViewUpdater {
  3.  
  4.         TextView     chat_view = null;
  5.  
  6.         Handler      handler   = null;
  7.  
  8.         ScrollView   scroll    = null;
  9.  
  10.        
  11.  
  12.         public class UpdateTask implements Runnable{
  13.  
  14.         CharSequence text;
  15.  
  16.         public UpdateTask(CharSequence  tmp) {
  17.  
  18.                 text = tmp;
  19.  
  20.         }
  21.  
  22.         public void run(){
  23.  
  24.                 if (  chat_view != null )  chat_view.append(text);
  25.  
  26.                 if ( scroll != null )
  27.  
  28.                 // first solution scroll after "chat append", doesn't work
  29.  
  30.                 scroll.fullScroll(View.FOCUS_DOWN);  
  31.  
  32.                 // second solution with post method, works fine
  33.  
  34.                 handler.post(new Runnable() {
  35.  
  36.                                         public void run() {
  37.  
  38.                                                                                  scroll.fullScroll(View.FOCUS_DOWN);  
  39.  
  40.                                                                         }
  41.  
  42.                                 }) ; // maybe a need here only a layout refresh ???
  43.  
  44.         }              
  45.  
  46.     }
  47.  
  48.        
  49.  
  50.         public TextViewUpdater(Handler ui_thread_handler,ScrollView sview,TextView view) {
  51.  
  52.                 handler   = ui_thread_handler;
  53.  
  54.                 chat_view = view;
  55.  
  56.                 scroll    = sview;
  57.  
  58.         }
  59.  
  60.        
  61.  
  62.         public void AddText(CharSequence add_seq){     
  63.  
  64.                         if ( handler != null ) handler.post(new UpdateTask(add_seq));                          
  65.  
  66.         }              
  67.  
  68. }
  69.  
  70.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
  4.     <ScrollView ...                      
  5.  
  6.                                 <TextView ..../>         
  7.  
  8.     </ScrollView>        
  9.  
  10.     <EditText .../>  
  11.  
  12. </RelativeLayout>
  13.  
  14.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


My questions about this:
1. It's possible to scroll ScrollView after "chat.append" ? ( need to refresch layout, how ? )
2. In which order, "layout code" and "Handler post method" are working on android platform ?
glock45
Freshman
Freshman
 
Posts: 4
Joined: Sat Apr 19, 2008 11:21 pm

Top

Postby EveningNewbs » Sat Oct 24, 2009 2:31 am

I've been wondering the same thing. I tried a few workarounds, but nothing seemed to work. Thanks for finding a solution!
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests