Home » Android » android – TextView's text disappearing when device rotated

android – TextView's text disappearing when device rotated

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am writing a phone dialer app for android. I created a layout for keypad, which contains a TextView and 10 buttons. Buttons are as keys for 10 digits(0 to 9) and TextView is for displaying the number according to keys pressed.

In my app, i am appending the text (“0” or “1”, etc.) to the TextView for each button pressed. If i pressed the buttons 1, 2, 3 then the text on TextView is 123.
The problem is, let’s take the screen is in landscape mode and TextView contains 123, if i turn it, in portrait mode no text on TextView.

Please Help Me Regarding this.

How to&Answers:

Please check on orientation change, on create method is called, which requires all the views to be created again, so you need to use one of the following methods:

  1. use onSavedInstance method and save the states of components/views to bundle.
  2. Just use following flag true in your manifest file in activity tag android:configChanges=”keyboardHidden|orientation”. like below:

    <activity android:name=".SampleActivity" android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation">
        ...
    </activity>
    

Answer:

What @jeet recommended didn’t work for me. I had to add “screenSize”. This is the line you should add in your manifest.xml in the <activity> node of your activity:

android:configChanges="keyboardHidden|orientation|screenSize"

Thus, the complete node may look like this:

<activity
android:name=".YourActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar">

Answer:

The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.

The best way to handle this is to store whatever data you need to keep within the Activity Bundle, by responding to the onSavedInstance event (called just before Android destroys the activity), and then reapplying those in the standard onCreate event.

Although you can add “orientation” to the configChanges property, keep in mind that you’re basically telling Android that you’re going to be handling everything relating to orientation change yourself – including changing layout, etc.

Answer:

To preserve a TextView’s text, you can simply set the TextView’s freezesText property to true.
As in:

    <TextView
    ...
    android:freezesText="true"
    .../>

This is the accepted answer here:
Restoring state of TextView after screen rotation?

Answer:

If someone is still having troubles… this did the trick for me

public class BranjeKP extends AppCompatActivity {

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

    //...
}

@Override
protected void onSaveInstanceState(Bundle out) {
    super.onSaveInstanceState(out);
    out.putString("TVNazivPod", TVNazivPodatka.getText().toString());
    out.putString("TVEnotaMere", TVEnotaMere.getText().toString());
}

@Override
protected void onRestoreInstanceState(Bundle in) {
    super.onRestoreInstanceState(in);
    TVNazivPodatka.setText(in.getString("TVNazivPod"));
    TVEnotaMere.setText(in.getString("TVEnotaMere"));
}

You basically save any values you want before rotation (that’s when onSaveInstanceState is called) into a Bundle and after rotation (onRestoreInstanceState) you just pull all values out from Bundle.
Just to clarify, TVNazivPodatka and TVEnotaMere are TextView widgets.

…with a help from
How to prevent custom views from losing state across screen orientation changes