Textview changes background color for no reason

I have two text images in action defined by xml - both with a gray background color. In my application, I set one of the background colors of the textviews to blue. This works as expected.

BUT: When I rotate the device (rotate) or leave the application and return again, the other text image is also blue - the same color as the one that was intentionally installed ...!?

When I leave the application and start it again, the second text remains blue. When I stop the application from starting (kill) and start it again, the second text appears gray. But the same problem occurs as soon as I rotate the device or launch the application the next time.

Faulty device working 4.1.1. - The same application on 2.3.4 works without problems.

SDK Tools 22.0.1, Eclipse Juno Service Release 2 32 bit, Windows 7 64 bit

EDIT: Same issue for SDK Tools 14, Eclipse Indigo SR1 32 bit on Windows 7 32 bit

I have no idea what is going on there. This is a kind of unwanted magic. Could you help me?

BeforeAfter

Below is the actual source code without any changes from the problematic project.

MainActivity.java:

package com.example.test; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv1 = (TextView) findViewById(R.id.textView1); tv1.setBackgroundColor(0xff33b5e5); } } 

acitivity_main.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="100dp" android:background="#cccccc" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="100dp" android:layout_marginTop="20dp" android:background="#cccccc" /> </LinearLayout> 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:icon="@drawable/ic_launcher" android:label="TextView Test" > <activity android:name="com.example.test.MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

EDIT 2: To make things even weirder: if I change the color of textview2 a bit to ie #cdcdcd, the problem will not appear. Only if both colors (textview1 and textview2) are identical in XML.

+4
source share
3 answers

I found a solution to this problem - although not an explanation. The problem exists only if the original colors of both text images in xml are identical. Thus, the solution should give text images different colors.

So, if you have the same problem, this is what works for me:

acitivity_main.xml: With issue

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="100dp" android:background="#cccccc" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="100dp" android:layout_marginTop="20dp" android:background="#cccccc" /> </LinearLayout> 

acitivity_main.xml: WITHOUT a problem

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="100dp" android:background="#ffcccccc" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="100dp" android:layout_marginTop="20dp" android:background="#fecccccc" /> </LinearLayout> 

In other words, I used a slightly different color (in fact, there is a different transparency) - and the problem is gone. I would not believe if someone told me.

+4
source

This is actually great :) Try something like this -

 public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv1 = (TextView) findViewById(R.id.textView1); tv1.setBackgroundColor(0xff33b5e5); TextView tv2 = (TextView) findViewById(R.id.textView2); tv2.setBackgroundColor(Color.Red); } } 
+2
source

I think I came across a similar error with Android 4.1.1. After several hours of headache, I found that when you declare the color of an element in XML, and then later edit it programmatically, something breaks. I managed to get rid of it without declaring a color in xml for the element, which I will change the background color.

0
source

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


All Articles