How to scroll text using the search bar?

I am learning Android Studio, and I am making a program where there is a lot of text, so I am trying to make it scrollable with seekbar (I also want it to be vertically scrollable) seekbars 0 amount is top and 100 is a bot. I made it so that these are scrolls when moving angles, but I cannot figure out how to make it so that it is related to the length of my text. Here is the code in my MainActivity.Java:

    seek_bar = (SeekBar)findViewById(R.id.seekBar);
    text_view =(TextView)findViewById(R.id.textView);
    seek_bar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                int progress_value;
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    progress_value = progress;
                    TextView newtext = (TextView) findViewById(R.id.textView2);
                    ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViev);
                    newtext.setText(hscrollViewMain.getMaxScrollAmount().toString()); // I made this because I thought that it would tell me the maximum amount of pixels in my text but toString() doesnt work somehow. It is redlined :(
                    hscrollViewMain.scrollTo(0, (hscrollViewMain.getMaxScrollAmount()/100)*progress_value); //this scrolls only little when seekbar is moved
                }

And here is my activity_main.xml

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollViev"
    android:layout_gravity="center_horizontal"
    android:fillViewport="false"
    android:clickable="false"
    android:nestedScrollingEnabled="false"
    android:isScrollContainer="false">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/testtext"
            android:id="@+id/textView2" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:text="@string/maintext" />
</LinearLayout>


</ScrollView>
</LinearLayout>

I also thought using the computeVerticalScrollRange () method instead of getMaxScrollAmount, but since it is protected by the method, I don't know how to use it. I'm pretty stuck here, so any help would be greatly appreciated.

+4
source share
1 answer

:

1) Relativelayout / .

2) TextView RelativeLayout

3) RelativeLayout. onMeasure/onLayout ( RelativeLayout) ViewTreeObserver

4) ,

5) , SeekBar, "" TextView. RelativeLayout children.layout(left, top, right, bottom), invalidate() TextView, .

,

+1

Source: https://habr.com/ru/post/1589363/


All Articles