scroll - Problems with scrollTo() in android -


i´m developing application incorporates chat , when type new message stores file in internal memory. also, when type new message, automatically scrolls bottom of edittext (where messages are), , works fine on first try,but when open application again , loads history file, automatic scrolling no longer works because, although scrolling bottom well, hides last line of text.

here .java file:

public class chatactivity extends activity {  private static edittext messagehistory; // private edittext messageinput; // private button sendbutton; // private scrollview scroller;  @override public void oncreate (bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.chat);      //prevents keyboard appearing when not rquested     getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_always_hidden);      projectdata.chatlogfile = new filehandler(getapplicationcontext(), projectdata.chat_log_file);     projectdata.chatlogfile.create();      getviews();      getchatlog();      addlistenneronbutton(); }  /**  * finds views in activity , stores them in global variables  */ private void getviews () {     messagehistory = (edittext) findviewbyid(r.id.messagehistory);     messageinput = (edittext) findviewbyid(r.id.messageinput);     sendbutton = (button) findviewbyid(r.id.sendbutton);     scroller = (scrollview) findviewbyid(r.id.historyscroller); }  private void getchatlog () {     messagehistory.settext(projectdata.chatlogfile.getdata());     scroller.scrollto(0, messagehistory.getbottom()); }  private void addlistenneronbutton () {     sendbutton.setonclicklistener(new onclicklistener() {         public void onclick(view v) {             if (!messageinput.gettext().tostring().equals(""))             {                 newmessage(messageinput.gettext().tostring(), 0);                 scroller.scrollto(0, messagehistory.getbottom());                 messageinput.settext("");             }         }     }); }  public static void newmessage (final string msg, int id) {     stringbuilder strbuilder = new stringbuilder();     switch (id) {         case 0: strbuilder.append("user:\n");break;         case 1: strbuilder.append("pc medic:\n");         default: break;     }      strbuilder.append(msg + "\n");     messagehistory.append(strbuilder.tostring());     messagehistory.refreshdrawablestate();     projectdata.chatlogfile.adddata(strbuilder.tostring()); }  @override public boolean oncreateoptionsmenu (menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.options_menu, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item)  {     intent intent;     switch (item.getitemid()) {         case r.id.menufaq:             intent = new intent();             intent.setclass(getapplicationcontext(), faqactivity.class);             startactivity(intent);             return true;         case r.id.menuconfig:             intent = new intent();             intent.setclass(getapplicationcontext(), prefactivity.class);             startactivity(intent);             return true;         case r.id.menuabout:             intent = new intent();             intent.setclass(getapplicationcontext(), aboutactivity.class);             startactivity(intent);             return true;         case r.id.menuexit:             intent = new intent();             intent.setclass(getapplicationcontext(), backgroundservice.class);             stopservice(intent);             setresult(projectdata.exit_code);             finish();             return true;         default:             return false;     } } } 

and .xml file:

<?xml version="1.0" encoding="utf-8"?> <linearlayout  android:id="@+id/chatmainlayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >  <imageview     android:id="@+id/logochat"     android:layout_width="match_parent"     android:layout_height="90dp"     android:scaletype="fitstart"     android:adjustviewbounds="true"     android:src="@drawable/logo_top"     android:contentdescription="@string/logo" />   <scrollview     android:id="@+id/historyscroller"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:fillviewport="true"     android:layout_weight="1" >      <edittext         android:id="@+id/messagehistory"         android:layout_width="fill_parent"         android:layout_height="0dp"         android:background="@color/black"         android:clickable="true"         android:editable="false"         android:focusable="false"         android:gravity="top"         android:inputtype="textmultiline"         android:textcolor="@color/white" /> </scrollview>  <edittext      android:layout_width="0dp"     android:layout_height="0dp"     android:inputtype="none"     android:focusable="true" />   <linearlayout     android:id="@+id/chatbottomlayout"     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal" >       <edittext         android:id="@+id/messageinput"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:maxheight="60dp"         android:nextfocusleft="@id/messageinput"         android:nextfocusup="@id/messageinput"         android:hint="@string/message_input"         android:layout_weight="1"         android:textcolor="@color/gray"         style="@style/textinput" />     <button          android:id="@+id/sendbutton"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="3"         android:text="@string/send" />  </linearlayout> 

can see or understand problem, please?

thanks in advance,

ricardo amendoeira

sana's answer allowed me solve own problem. think need allow textview recalculate window bounds, try wrapping scrollto() method in post handler.

see code below:

@override public void onwindowfocuschanged(boolean hasfocus) {     super.onwindowfocuschanged(hasfocus);     scrollview.post(new runnable() {         @override         public void run () {             scrollview.scrollto(0, 500);         }     }); } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -