layout - ScrollView issue Android -
<horizontalscrollview android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none"> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:singleline="true" android:text="" android:layout_gravity="right" android:gravity="right|center_vertical" android:textcolor="#666666" android:textsize="30sp" /> </horizontalscrollview>
hi, want text inside textview scrollable when fills space available. text inserted right, beginning of text disappears on left. when becames scrollable can scroll amount of space equal space of text that's not displayed scrolls right side, textview empty , can't reach i've typed before. suggestions? sorry poor english. wish it's clear enough... (the scrollview part of vertical linearlayout)
move android:layout_gravity="right" textview horizontalscrollview
change textview gravity "center_vertical"
in activity.java write method set text in textview , scroll right.
when want set text in textview use method.
example code:
xml
<horizontalscrollview android:id="@+id/scrollview" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none" android:layout_gravity="right"> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:singleline="true" android:text="" android:gravity="center_vertical" android:textcolor="#666666" android:textsize="30sp"/> </horizontalscrollview> <button android:id="@+id/button1" style="?android:attr/buttonstylesmall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="clickme" android:onclick="btclick"/> </linearlayout>
activity.java:
public class mainactivity extends activity { private textview textview; private horizontalscrollview scroll; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); scroll = (horizontalscrollview)findviewbyid(r.id.scrollview); textview = (textview) findviewbyid(r.id.textview2); settextviewtext("inicial text"); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void btclick(view v) { string text = textview.gettext().tostring(); int rnd = 1 + (int)(math.random() * ((9 - 1) + 1));//get random number between 1 , 9 text += rnd; //append random previous text settextviewtext(text); //set textview new text; } private void settextviewtext(string text)// set text texview , scroll right { textview.settext(text); scroll.post(new runnable() { public void run() { scroll.fullscroll(scrollview.focus_right); } }); } }
i hope ask for.
Comments
Post a Comment