I have two textviews in an activity, defined by xml - both with background color gray. In my app, I set one of the textviews background color to blue. This works as expected.
BUT : When I turn the device (rotate), or leave the app and come back again, the other textview is also blue - same color as the one set intentionally...!?
When I leave the app and start it again the second textview stays blue. When I stop the app from running (kill) and start it again the second textview is gray. But same problem appears as soon as I rotate the device or start the app the next time.
Problem device is running 4.1.1. - same app on 2.3.4 device runs with no problem.
SDK Tools 22.0.1, Eclipse Juno Service Release 2 32 bit , Windows 7 64 bit
EDIT : Same problem on SDK Tools 14, Eclipse Indigo SR1 32 bit on Windows 7 32 bit
I have no idea what is going on there. It is some kind of undesired MAGIC. Could you please help me?
Below is the real source code with no modification from the problem 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 stranger: If I change color of textview2 slightly to i.e. #cdcdcd the problem does NOT come. It is only in case both colors (textview1 and textview2) are identical in XML.
To change the background color of TextView widget, set the background attribute with required Color value. You can specify color in rgb , argb , rrggbb , or aarrggbb formats. The background color is applied along the width and height of the TextView.
In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .
I found a solution to that problem - although not an explanation. The problem only exists if initial colors of both textviews in xml are identical. The solution is therefore to give the textviews different colors.
So, if you have the same problem, this is what works for me:
acitivity_main.xml: WITH 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="#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 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 have used just a slightly different color (actually here it is different transparency) - and the problem is gone. I would not believe it if someone told me.
Really this is wonderful :) Try something like that-
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With