Changing TextView text - old text does not disappear (Android 4.1.2)

I am new to Android development. Trying to do something fairly simple - change the displayed text when the timer is ticked. Here is the potentially relevant code:

CountDownTimer currentTimer; Resources res; TextView timerText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exercise); res = getResources(); timerText = (TextView) findViewById(R.id.timer_text); } @Override protected void onStart() { super.onStart(); //"Get ready" countdown currentTimer = new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { timerText.setText("" + (int)Math.ceil(millisUntilFinished / 1000.0)); } @Override public void onFinish() { ... } }; currentTimer.start(); } 

This works fine on emulated device 4.2.2, but on device 4.1.2 (both physical and emulated) the modified TextView appears as such, and the countdown continues:

Changed TextView

If you cannot say that the numbers 5.4.3 are superimposed. Therefore, when I set a new line for TextView, a new line is displayed, but without replacing the old line. Any other text elements used in my application behave the same.

Any ideas what the problem is and how to fix it?

Edit : from XML layout file:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".ExerciseActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:keepScreenOn="true" android:orientation="vertical" > ... <TextView android:id="@+id/timer_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textIsSelectable="false" android:hint="@string/timer_default" /> ... </LinearLayout> 

That all of this can make a difference.

+6
source share
2 answers

public class MainActivity extends the action {

 TextView timerText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerText = (TextView) findViewById(R.id.timer_text); } @Override protected void onStart() { super.onStart(); CountDownTimer currentTimer = new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { timerText.setText("" + (int) Math.ceil(millisUntilFinished / 1000.0)); } @Override public void onFinish() { } }; currentTimer.start(); } 

}

Tested on 4.1.2 emulator and solved.

I assume the final reference variable is causing the problem.

0
source

Workaround : You must set the background for LinearLayout: android: background = "@ color / white". I am looking for a reason.

0
source

Source: https://habr.com/ru/post/957396/


All Articles