The background color I'm setting in main.xml isn't showing when the app runs

Go To StackoverFlow.com

0

I'm setting a background color in main.xml.

When I preview the layout in Eclipse, the background color shows up correctly, but when the app runs on my device, the background color is default black. It seems none of my changes in main.xml are reflected when the app runs.

Here is my main.xml file

<?xml version="1.0" encoding="utf-8"?>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lst"
android:layout_width="match_parent"
android:background="@color/listViewBG"
android:divider="@drawable/divider"
/>

Here is the OnCreate in the main activity

public class AleWorldActivity extends ListActivity
{
String classes[] = { "Movies", "Pictures" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(AleWorldActivity.this, android.R.layout.simple_list_item_1, classes));
}

Any ideas? Thanks

Here is my strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, AleWorldActivity!</string>
<string name="app_name">Ale World</string>
<color name="listViewBG">#e101f5</color>

</resources>

Kevin

2012-04-05 00:44
by Kevin
The attribute "android:background="@color/listViewBG" has to do with a style, correct? Can we see the style.xml as well - Jared Burrows 2012-04-05 00:53
I don't have a style.xml. I will add my strings.xml which contains the color - Kevin 2012-04-05 00:58
Sorry, I mean't to say strings.xml. Here is another StackOverFlow example: http://stackoverflow.com/questions/2748830/how-to-change-background-color-in-android-app.

What does show up? Have you tried just setting it manually? "android:background="#FFFFFF - Jared Burrows 2012-04-05 01:05

A listview shows up, but its black. So it has to be reading the layout file, no - Kevin 2012-04-05 01:44
I am saying try changing the background color like the way I posted, if that works, we can rule out the default way you input background colors - Jared Burrows 2012-04-05 01:52
Changed it to android:background="#FFFFFF". Still black - Kevin 2012-04-05 01:56


1

You should declare your color on colors.xml. Create that file in the same folder that strings.xml is.

Besides that you have some errors:

  • You are missing the setContentView(R.layout.main) on your AleWorldActivity.
  • The id of your ListView is wrong. Change it to : android:id="@android:id/list
  • Add android:layout_height="wrap_content" in your ListView xml.
2012-04-05 01:06
by Tiago Almeida
Well, I added setContentView and it crashes: Your content must have a ListView whose id attribute is 'android.R.id.list'

I set the ID of the listview in main.xml to android.R.id.list and it didn't help. Still crashes, same error - Kevin 2012-04-05 01:42

in the id use: android:id="@+id/android:list - Tiago Almeida 2012-04-05 01:46
OK i switched it, and it no longer crashes. Unfortunately it is still the default color - Kevin 2012-04-05 01:50
Edited my answer for you to point out all errors you have. Just tested in eclipse with those errors corrected and it worked : - Tiago Almeida 2012-04-05 02:30


0

Change @color/listViewBG to #FFFFFF see if the screen changes colors that means that there is a problem with you declaring it in your strings.xml.

http://developer.android.com/resources/samples/SoftKeyboard/res/values/colors.html

2012-04-05 01:09
by EGHDK
Ads