auto-scrolling TextView in android to bring text into view

前端 未结 13 1699

I have a TextView that I\'m dynamically adding text to.

in my main.xml file I have the properties set to make my max lines 19 and scrollbar

13条回答
  •  独厮守ぢ
    2020-11-28 05:27

    // Layout Views
        private TextView mConversationView;
        private ScrollView mConversationViewScroller;
    
    use it either in :
    
    public void onCreate(Bundle savedInstanceState)
    {
       //...blabla
       setContentView(R.layout.main); 
       //...blablabla
            mConversationView = (TextView) findViewById(R.id.in);       
            mConversationViewScroller = (ScrollView) findViewById(R.id.scroller);
    }
    
    or in "special" method e.g. 
    
    public void initializeChatOrSth(...){
            //...blabla
            mConversationView = (TextView) findViewById(R.id.in);       
            mConversationViewScroller = (ScrollView) findViewById(R.id.scroller);
    }
    
    public void addTextToTextView()
    {
    
                 //...blablabla some code
                    byte[] writeBuf = (byte[]) msg.obj;
              // construct a string from the buffer - i needed this or You can use by"stringing"
                    String writeMessage = new String(writeBuf);
                    mConversationView.append("\n"+"Me:  " + writeMessage);
                    mConversationViewScroller.post(new Runnable()
                    {
                        public void run()
                        {
                            mConversationViewScroller.fullScroll(View.FOCUS_DOWN);
                        }
                    });
    }
    
    this one works fine, also we can maually scroll text to the very top - which is impossible when gravity tag in XML is used.
    
    Of course XML (main) the texview should be nested inside scrollview , e.g:
    
    
    
            
    
            
        
    

提交回复
热议问题