5

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.

funerr
  • 7,212
  • 14
  • 81
  • 129

1 Answers1

5

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 .. :)

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • "The program has stopped", and this isn't multi-line :/ I want the scoller to be set to the screen width, but its size to be proportional to the longest row. – funerr Jun 08 '14 at 19:19
  • I know.. that's the issue. I'll try something else too.. But is there a log why the app stopped ? It maybe cause of the image.. don't use too large reso image. – mike20132013 Jun 08 '14 at 20:09
  • It logged: "E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException" - but I repeat, this isn't really want I am asking for. I really appreciate the effort though! – funerr Jun 10 '14 at 18:34
  • No issues.. I tried this on my device and the code works fine for me. Maybe the solution is different for this question..Will keep searching for the answer .. :D – mike20132013 Jun 10 '14 at 18:51
  • do you have a new solution? – funerr Jun 15 '14 at 19:23
  • I tried to find some solution but it seems like it still works the same as this solution. Will continue to search on this. Although, the app doesn't crash on my device for some reason. I mean, it shouldn't crash anyways. – mike20132013 Jun 15 '14 at 19:48
  • Figured out the solution man. I am editing the answer. Works great for me.. :D – mike20132013 Jun 15 '14 at 20:27
  • Hi mike, first thanks for the new update! But I have a few problems, first off it isn't an editext(although it works for that ass well - but partially). When it is an edittext the scroll is wired when vertical scroll is added, and trying to copy isn't working. – funerr Jun 24 '14 at 20:50
  • @agam360 I'll try to check on that too..But what exactly is happening on the EditText for vertical scrolling ? – mike20132013 Jun 24 '14 at 21:04
  • Try to type in a few newlines and try out the vertical scrolling with horizontal scrolling, you'll notice it. Thanks – funerr Jun 25 '14 at 20:10