java - Automatic scrolling a text view after appending text in Android -


i'm trying make content of textview automatically scroll bottom @ recent text appended. tried many ways many post seems there's no way make work. trie different layout_wheight , width parameters , on. silly thing made in past , worked, using same set not. here's latest xml code:

    <tablerow     android:id="@+id/tablerow1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="1.5"     android:weightsum="2">      <scrollview         android:id="@+id/askscv"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:fillviewport="true"         android:scrollbars="vertical">          <textview             android:id="@+id/asktxv"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:layout_weight="1.0"             android:maxlines="99999"             android:scrollbars="vertical"             android:text=""             android:textappearance="?android:attr/textappearancelarge" />     </scrollview>      <scrollview         android:id="@+id/answerscv"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:fillviewport="true"         android:scrollbars="vertical">           <textview             android:id="@+id/answertxv"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:layout_weight="1.0"             android:maxlines="99999"             android:scrollbars="vertical"             android:text=""             android:textappearance="?android:attr/textappearancelarge" />     </scrollview> </tablerow> 

and java one:

   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mquestionedt = (edittext) findviewbyid(r.id.questionedt);      manswertxv = (textview) findviewbyid(r.id.answertxv);     manswertxv.setmovementmethod(new scrollingmovementmethod());      manswerscv=(scrollview)findviewbyid(r.id.answerscv);     manswerscv.fullscroll(view.focus_down);      masktxv=(textview) findviewbyid(r.id.asktxv);     masktxv.setmovementmethod(new scrollingmovementmethod());      maskscv=(scrollview)findviewbyid(r.id.askscv);     maskscv.fullscroll(view.focus_down);      maskbtn = (imagebutton) findviewbyid(r.id.askbtn);     maskbtn.setonclicklistener(this);      mstartbtn = (imagebutton) findviewbyid(r.id.startbtn);     mstartbtn.setonclicklistener(this); }  @override public void onclick(view view) {     bundle result;      if (view.equals(maskbtn)) {         result=mansweringmachine.answer(mquestionedt.gettext().tostring());         masktxv.append("\n" + mquestionedt.gettext().tostring());         maskscv.smoothscrollto(0,masktxv.getbottom());         manswertxv.append("\n" + result.getstring(answeringmachine.dialogue_result));         manswerscv.smoothscrollto(0,manswertxv.getbottom());     } else if (view.equals(mstartbtn)) {          result = mansweringmachine.runstartingprompt();         manswertxv.append(result.getstring(answeringmachine.dialogue_result));         //manswerscv.fullscroll(view.focus_down);     }  } 

and here's old working code, xml:

<scrollview      android:id="@+id/terminalscv"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <textview         android:id="@+id/terminaltxv"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:textappearance="?android:attr/textappearancelarge" />  </scrollview> 

and java one:

    /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_terminal);     terminaltxv=(textview) findviewbyid(r.id.terminaltxv);     terminalscv=(scrollview) findviewbyid(r.id.terminalscv);     terminalscv.fullscroll(view.focus_down); }  @override protected void onbtdatareceived(int[] data) {     super.onbtdatareceived(data);     string out=string.valueof(terminaltxv.getlinecount())+" - ";     (int x=0;x<data.length-1;x++){         out+=data[x];         out+=" ";     }     out+="\n";     terminaltxv.append(out);     terminalscv.smoothscrollto(0, terminaltxv.getbottom()); } 

hope can make me understand enigma.... thanks!

in xml textview element try add:

android:gravity="bottom"

also sure need specify android:scrollbars="vertical" in both scrollview , textview? in addition, noticed difference between old working version doesn't uses setmovementmethod() - use method without scrollview, since have scroollview don't see reason why need it...


Comments