java - how to add auto texview scroll like google translator -
how add horizontalscrollview: auto-scroll end when new views added in textview image want create type of textview scoll when type text plz me?
my layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.textviewdemo.mainactivity"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="name" android:ems="10" android:layout_alignparenttop="true" android:id="@+id/edittext" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:text="textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext" android:gravity="right|center_vertical" android:layout_gravity="right|center_vertical" android:scrollhorizontally="true" android:textsize="30dp" android:layout_margintop="8dp" android:id="@+id/textview" /> </relativelayout>
my main activity
public class mainactivity extends appcompatactivity { edittext edittext; textview textview; horizontalscrollview horizontalscrollview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext = (edittext) findviewbyid(r.id.edittext); textview = (textview) findviewbyid(r.id.textview); edittext.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { textview.settext(s); } @override public void aftertextchanged(editable s) { } }); } }
wrap textview
horizontalscrollview
below , remove android:scrollhorizontally="true"
<horizontalscrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext" android:id="@+id/horizontalscrollview" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:textsize="30dp" android:layout_margintop="8dp" android:id="@+id/textview" /> </horizontalscrollview>
modify oncreate()
below:
edittext edittext; textview textview; horizontalscrollview horizontalscrollview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext =(edittext)findviewbyid(r.id.edittext); textview =(textview)findviewbyid(r.id.textview); horizontalscrollview =(horizontalscrollview)findviewbyid(r.id.horizontalscrollview); edittext.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { textview.settext(s); horizontalscrollview.post(new runnable() { public void run() { horizontalscrollview.fullscroll(horizontalscrollview.focus_right); } }); } @override public void aftertextchanged(editable s) { } }); }
Comments
Post a Comment