TextView text disappears when the device is rotated

I am writing an Android mobile phone application. I created a keyboard layout that contains a TextView and 10 buttons. Buttons are keys for 10 digits (0 to 9), and TextView is for displaying numbers according to the keys pressed.

In my application, I add text ("0" or "1", etc.) to the TextView for each button clicked. If I pressed the buttons 1, 2, 3 , the text in the TextView is 123 . The problem is that it allows you to screen in landscape mode, and the TextView contains 123, if I rotate it, in portrait mode there is no text in the TextView.

Please help me regarding this.

+8
android textview
source share
5 answers

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

  • use the onSavedInstance method and save the states of the components / views for merging.
  • Just use the following true flag in your manifest file in the android activity tag: configChanges = "keyboardHidden | orientation". as below:

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

What @jeet recommended did not help me. I had to add "screenSize". This is the line you should add to your .xml manifest in the <activity> node of your activity:

 android:configChanges="keyboardHidden|orientation|screenSize" 

So a full node might look like this:

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

The reason for this is because Android basically destroys the activity and creates it again every time the device is rotated. This basically allows you to use different layouts based on portrait / landscape mode.

The best way to handle this is to save all the data that needs to be saved in the Activity Bundle by responding to the onSavedInstance event (called just before Android destroys the action), and then reapplying them in the standard onCreate event.

Although you can add โ€œorientationโ€ to the configChanges property, keep in mind that you basically tell Android that you will handle everything regarding orientation changes, including changing the layout, etc.

+7
source share

If anyone still has problems ... 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")); } 

Basically, you save any values โ€‹โ€‹you want before rotation (which when calling onSaveInstanceState) in the Bundle and after rotation (onRestoreInstanceState) you just retrieve all the values โ€‹โ€‹from the Bundle. To clarify, TVNazivPodatka and TVEnotaMere are TextView widgets.

... using How to prevent custom views from changing due to screen orientation changes

0
source share

To save TextView text, you can simply set the TextView freezesText property to true . How in:

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

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

0
source share

All Articles