I create textView in scrollView. Every time i setText the textView, the text not updating, but when i open the keyboard then close it, the text updated.. after googling, i got the solution is call textView.invalidate() and textView.requestLayout(). But i am currious why it's not updating without call invalidate and requestLayout? Is scrollView has somethis 'special' so i need to call invalidate and requestLayout?
here is the code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.zihadrizkyef.belajarinternalstorage.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:orientation="vertical">
<EditText
android:id="@+id/etWrite"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="top"
android:hint="write text here"/>
<View android:id="@+id/separator1"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#aaa"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/tvRead"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:hint="(no text)"/>
</ScrollView>
<View android:id="@+id/separator2"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#aaa"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="2"
android:gravity="center">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="onClick"
android:text="save"/>
<Button
android:id="@+id/btnLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="onClick"
android:text="load"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.zihadrizkyef.belajarinternalstorage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private final String FNAME = "mydata";
EditText etWrite;
TextView tvRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etWrite = (EditText)findViewById(R.id.etWrite);
tvRead = (TextView)findViewById(R.id.tvRead);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnSave:
String data = etWrite.getText().toString();
try {
FileOutputStream fOut = openFileOutput(FNAME, MODE_PRIVATE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(MainActivity.this, "File saved", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btnLoad:
int cRead;
String read="";
try {
FileInputStream fIn = openFileInput(FNAME);
while((cRead=fIn.read())!=-1) {
read += Character.toString((char)cRead);
}
tvRead.setText(read);
tvRead.invalidate();
tvRead.requestLayout();
Toast.makeText(MainActivity.this, "File loaded", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
github : https://github.com/zihadrizkyef/FileWriteRead_InternalStorage