0

i want to scroll my TextView at some portion of text that i know, and show it on top. this is my scrollView and TextView:

<ScrollView   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:scrollbars="vertical"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">     

    <TextView 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"/>  
</ScrollView>

My text is an array of strings:

ScrollView scroll= (ScrollView) getActivity().findViewById(R.id.scroll);
TextView text = (TextView) getActivity().findViewById(R.id.text);
String book ="";
for(String line: lines){
  book += line; 
}
text.setText(book);

this is when i want to scroll:

String find = book[myindex];
int go = text.indexOf(find);
makeScroll(go);

this is my runnable:

    private void makeScroll(final int go){
        scrollRegole.post(new Runnable() {
            public void run() {
                  scrollRegole.scrollTo(0, go);
            }
        });
    }

this is not work, why? thanks!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
fabio
  • 271
  • 5
  • 18

2 Answers2

0

Ok, the code is fine. It scrolls to the current (x,y) position of your "scrollView" as says: http://developer.android.com/reference/android/widget/ScrollView.html#scrollTo(int, int)

So I think that is an interpretation trouble. Is not the same a "line" on the textView that a "y" coordinate on the ScrollView. So if you want to get the row number 15 for example, "go variable" cant be 15, it should be something proportional to the "TextSize" + padding + margin, etc...

Do you know what I mean? Hope it helps.

EDIT:: Here is a test that I tried and it work fine if you know which is the row that you want. Maybe the trouble is that you didn't calculate the line properly

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

    ScrollView scroll;
    TextView text;
    EditText editText;
    Button but;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text);
        scroll = (ScrollView) findViewById(R.id.scroll);
        but = (Button) findViewById(R.id.button1);
        editText = (EditText) findViewById(R.id.editText1);

        but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int go = Integer.valueOf(editText.getText().toString());
                makeScroll(go);
            }
        });

    }

    private void makeScroll(final int go) {
        scroll.post(new Runnable() {
            public void run() {
                scroll.scrollTo(0, go * text.getLineHeight());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

and the 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:onClick="handleClick"
        android:orientation="vertical"
        tools:context=".PlayActivity" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number" />

        </LinearLayout>

        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" >

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:text="sdqwerhdf adf qwer hdfashdf aE HFASDJFH ASKDFH QWLIULER HASDLIDLF HASDKJFH SDJF HAWEUJRH AS DKJFHL AWIUE4R YAS DJHFWIERUHF AUDF JHAWHWER ASJDFH ASDFHAS JEHLRUAIKYHE KURF ASJKDFHJ ASDFKJAHSDF ASDFASDF ASDF ASDFASDFASDF QWETR DGFsdqwerhdf adf qwer hdfashdf aE HFASDJFH ASKDFH QWLIULER HASDLIDLF HASDKJFH SDJF HAWEUJRH AS DKJFHL AWIUE4R YAS DJHFWIERUHF AUDF JHAWHWER ASJDFH ASDFHAS JEHLRUAIKYHE KURF ASJKDFHJ ASDFKJAHSDF ASDFASDF ASDF ASDFASDFASDF QWETR DGF"
                android:textSize="20dp" />

        </ScrollView>

    </LinearLayout>

EDIT:: EDIT:

In order to get the line, here is a possible solution: Android TextView and getting line of text

As I said at the commentary, firstly get a vector that contains every line, then look for the word. And finally move to the current row with the previous code.

Community
  • 1
  • 1
Bardo91
  • 585
  • 2
  • 7
  • 17
  • TextView widget has an specific function to get the height of a line (or row as you said). If you know which is the line that contain the word that you're looking for, only multiplicate it with the "lineHeight". Here is the function: http://developer.android.com/reference/android/widget/TextView.html#getLineHeight() – Bardo91 Sep 10 '13 at 13:55
  • Edit your entry and post the complete code because anywhere you said about clicking at rows... I tried that code using a button and an edittext to access an specific row and everything is fine... I'm going to add what I did and compare – Bardo91 Sep 10 '13 at 16:05
  • Baldo i do not have a integer number where to go, i want to scoll at some lines that i know example: sdqwerhdf \n HFASDJFH \n etc... i want to scoll at HFASDJFH position and show on top – fabio Sep 10 '13 at 21:49
  • I thought that you had the function that gives you the current row. So I think that there isn't a function that provides you that. But, googling I found something that can be useful for your purpose (http://stackoverflow.com/questions/9325798/android-textview-and-getting-line-of-text) Firstly obtain a vector of every line and then look for the word, finally move to the specific line with the previous code. I edited the answer – Bardo91 Sep 11 '13 at 00:26
0

this is how i resolved my problem: this is my new ScrollView:

    <ScrollView 
        android:id="@+id/scroll"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:layout_weight="3" >

        <LinearLayout
            android:id="@+id/containerTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>      
    </ScrollView>

here i add many TextView into my container:

if(scroll == null){
   scroll = (ScrollView) getActivity().findViewById(R.id.scroll);
}

if(layout == null)
       layout = (LinearLayout) getActivity().findViewById(R.id.containerTextView);

layout.removeAllViews();

for(String line: lines){
   TextView text = new TextView(getActivity());
   text.setText(line);  
   text.setTextSize(...));          
   layout.addView(text);
}

this is when i want to scroll:

private void calculateScroll(final LinearLayout layout, final int indexLine){

    layout.addOnLayoutChangeListener(new OnLayoutChangeListener(){

        @Override
        public void onLayoutChange(View paramView, int paramInt1,
                int paramInt2, int paramInt3, int paramInt4, int paramInt5,
                int paramInt6, int paramInt7, int paramInt8) {

            int y = 0;
            for(int x=0; x< indexLine; x++){    
                y += layout.getChildAt(x).getHeight();
            }

            makeScroll(y);
        }

    });
}

private void makeScroll(final int go){
    scrollRegole.post(new Runnable() {
        public void run() {
            scroll.scrollTo(0, go);
        }
    });
}

here if i want to show lines[3] (for exsampe..) i know the correct height. My problem now is remove space between TextView.. but it works!

fabio
  • 271
  • 5
  • 18