There is a markup:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:theme="@style/AppTheme.NoActionBar"> <ImageView android:background="@drawable/food" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/potbacground" /> <LinearLayout android:id="@+id/potehki1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/food" android:orientation="horizontal"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/a1scroll" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/eda" android:id="@+id/eda_text" android:textColor="@color/blue" android:textAlignment="center" android:textSize="40sp" android:textStyle="bold" /> </LinearLayout> </ScrollView> </LinearLayout> </FrameLayout> 

there is a text in the string resources, for example: I flipped through to the middle, went out, I entered the application again and loaded the position where I turned the application. I found an example on the net:

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); final ScrollView scrollView = (ScrollView) findViewById(R.id.a1scroll); final TextView textView = (TextView) scrollView.getChildAt(0); final int firstVisableLineOffset = textView.getLayout().getLineForVertical(scrollView.getScrollY()); final int firstVisableCharacterOffset = textView.getLayout().getLineStart(firstVisableLineOffset); outState.putInt(ScrollViewContainerTextViewFirstVisibleCharacterOffset, firstVisableCharacterOffset); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); final int firstVisableCharacterOffset = savedInstanceState.getInt(ScrollViewContainerTextViewFirstVisibleCharacterOffset); final ScrollView scrollView = (ScrollView) findViewById(R.id.a1scroll); scrollView.post(new Runnable() { public void run() { final TextView textView = (TextView) scrollView.getChildAt(0); final int firstVisableLineOffset = textView.getLayout().getLineForOffset(firstVisableCharacterOffset); final int pixelOffset = textView.getLayout().getLineTop(firstVisableLineOffset); scrollView.scrollTo(0, pixelOffset); } }); } 

but it is somehow not clear, especially that it is:

 ScrollViewContainerTextViewFirstVisibleCharacterOffset 

    2 answers 2

    Here is the working code for you - saving the position when you exit the application. Used your markup, everything works as it should, but the code requires optimization

     package firstknowok.us.ivanovsoftware.com.myapplicationtextview; import android.content.SharedPreferences; import android.os.Parcelable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ScrollView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView editText1; Parcelable state; int y=0; SharedPreferences sPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1 = (TextView)findViewById(R.id.eda_text); String t = ""; //Заполнил строками привет eda_text for(int i=0;i<100;i++){ t=t+"Привет "+i+"\n"; } editText1.setText(t); //Находим ранее сохраненное значение после события onDestroy sPref = getPreferences(MODE_PRIVATE); final int y = sPref.getInt("YPOSITION", ""); if (((ScrollView)findViewById(R.id.a1scroll)) != null){ //Если было сохранение после onDestroy final ScrollView scrollView = (ScrollView) findViewById(R.id.a1scroll); scrollView.post(new Runnable() { public void run() { scrollView.scrollTo(0, y); //Восстановим позицию scrollView } }); } Log.i("ivansoft", "onCreate: "+savedText); } @Override protected void onDestroy() { super.onDestroy(); y = ((ScrollView)findViewById(R.id.a1scroll)).getScrollY(); sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putInt("YPOSITION",y); //Сохраним позицию при разрушении MainActivity ed.commit(); Log.i("ivansoft", "onDestroy: "+y); } } 
    • Why putString ()? - rjhdby
    • 2
      Just like that, let the person think;) ... I pointed out - the code requires optimization. - ivansoft
    • The business is of course yours, but if suddenly a person does not optimize, but also releases, and then accompany someone to it ... This is savedText.length ()! = 0 for example - rjhdby
    • You are right, you never know ... Corrected the description of the answer - ivansoft
    • Using String as an integer variable is certainly no good. Also onDestroy() not the right place for save logic. If the application simply goes into the background without destroying or even trivially the device is rotated, the position will not be restored. Do you suggest other logic to write for these cases? I think it's easier to use onPause() . Here we have a database of working solutions, not quests - I wrote, and you remake it at the prompt. Using String so ridiculous - this is, first of all, your reputation. What can you think about the programmer who wrote this solution? - pavlofff

    See ivansoft's working solution.

    If you need to maintain a position when you rotate the screen, then there is already an artificial need to bind to the elements inside the ScrollView. Calculate exactly which View is in the upper left corner of the ScrollView and save its tag for example. And after re-creation, scroll to it.

    • Why is it impossible to use the same decision on defining and restoring a position when turning, as in the answer to which you refer? It is much simpler .. In fact, you only need to transfer the save logic from onDestroy() , to onPause() - pavlofff
    • Because when turning, the geometric dimensions of the views can change (there were two lines of text, and it became one for example) and scrolling along the coordinates will lead to completely different content - rjhdby