Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Horizontal Scrollbar not Moving

My edit text:

android:scrollbars="horizontal|vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="textMultiLine|textNoSuggestions"
android:ems="10"
android:gravity="top|left"

My code:

myet = (EditText) findViewById(R.id.myedittext);
myet.setScrollbarFadingEnabled(true);
myet.setHorizontallyScrolling(true); 

For some odd reason, it does scroll but the scrollbar itself is not moving, it is stuck to the bottom-left of the edittext.

I guess that is has to do with the edittext being with no static width.

How can I fix the horizontal scroll-bar so it could move and its size to be proportional to the longest row.

Note: it has to be multi-line and also scroll vertically.

like image 698
funerr Avatar asked Jun 05 '14 22:06

funerr


1 Answers

Alright.. I know it might not be a perfect solution, but here's what I found from other posts.

This definitely works but the progress on the scroll bar is not very noticeable. Although there is the scrolling effect and you can definitely see the progress.

Here's the code for java:

    et = (EditText) getActivity().findViewById(R.id.et1);
    et.setScroller(new Scroller(getActivity())); //This may not be needed.
    et.setHorizontalScrollBarEnabled(true); 
    et.setScrollbarFadingEnabled(true);
    et.setHorizontallyScrolling(true);
    et.setMovementMethod(new ScrollingMovementMethod()); 

And here's the layout for it:

<EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:ems="10"
        android:scrollHorizontally="true"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:scrollbarFadeDuration="2000"
        android:scrollbarSize="80dp"
        android:scrollbarStyle="insideInset"
        android:scrollbarTrackHorizontal="@drawable/ic_drawer"
        android:scrollbars="horizontal"
        android:text="Hello"
        android:textSize="80sp" >
    </EditText>

Again, all the things mentioned here are not needed, I did it for testing purpose. You may change it.

To prove that the code is working, here are two image for it.

Images http://imageshack.com/a/img850/3913/5c1i.png Images http://imageshack.com/a/img834/151/zg73.png

Try this and see if you get any update.

Please say you saw the progress.. Again, this may not be a perfect solution. Any suggestions will help or any other answer will be greatly appreciated.

EDITED: Working solution

My Layout : (activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#999999"
    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=".MainActivity" >

    <HorizontalScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#333333"
        android:fillViewport="true"
        android:scrollbars="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/TEXT_STATUS_ID"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:layout_weight="1.0"
                android:text="Hello"
                android:textColor="#cccccc"
                android:textSize="35sp" />
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

Java Code : (MainActivity.java)

public class MainActivity extends Activity {

    TextView tv1;
    HorizontalScrollView mScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.TEXT_STATUS_ID);
        mScrollView = (HorizontalScrollView) findViewById(R.id.SCROLLER_ID);
        // loadDoc();
        String s = "Nam ornare mattis nulla vitae vestibulum. "
                + "Mauris ac tellus diam. Nam dictum dolor augue,"
                + "quis hendrerit tortor aliquam et. Proin eu gravida augue."
                + "\n"
                + "Fusce lorem dolor, faucibus quis auctor eu, interdum at lorem."
                + "Suspendisse mi elit, suscipit semper mauris et, "
                + "interdum adipiscing lectus. Nulla laoreet arcu "
                + "\n"
                + "sollicitudin ultricies laoreet. Curabitur a bibendum nibh. "
                + "Etiam eget quam vitae sapien pellentesque facilisis. "
                + "Fusce nec enim vel mauris ultrices blandit ut sed purus."
                + " Vivamus ut sem nisi. Cras molestie ligula ac dui fermentum,"
                + " id placerat mauris rhoncus. Curabitur erat urna, ornare a placerat ac, "
                + "interdum nec diam. Donec sodales imperdiet nulla, ut ultricies elit.";
        tv1.setText(s);
        scrollToBottom();
    }

    private void scrollToBottom() {
        mScrollView.post(new Runnable() {
            public void run() {
                tv1.setHorizontallyScrolling(true);
                tv1.setMovementMethod(new ScrollingMovementMethod());
                mScrollView.smoothScrollTo(0, tv1.getBottom());
            }
        });
    }

}

Now this is working smooth and horizontal bar is working great and very responsive. Try it out. It works great .. Good Luck .. :)

like image 80
mike20132013 Avatar answered Oct 23 '22 05:10

mike20132013