5

I've recently started working on an Android app. I'm not a programming noob, I have been programming in Java since 2009, but this is my first Android app. I have tried following some tutorials on how to set the background colour but it simply isn't working for me. I can't work out what I'm doing wrong.

In my layout directory I have files called: main.xml and view_fact.xml. They both use the linear layout and my LinearLayout Tag is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>

And in my "values" directory I the contents of "strings.xml" is:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Facts</string>
<color name="background_grey">#E8E8E8</color>
</resources>

But the background colour is not changing from the default black.

I've also tried adding: "android:theme="@android:style/Theme.Light"" in my AndroidManifest.xml file.

None of this is working, any suggestions?

Thank you.

On my view_fact.xml page it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fact:"
android:padding="10dp"
android:textColor="#080808"
/>

<TextView 
android:id="@+id/factData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=""
/>
<Button
android:id="@+id/anotherFactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Another Fact"/>
</LinearLayout>

What's interesting is that the text in "factData" is grey, while the text: "Fact:" in the Text View above is white, and I've tried to change it's colour to red and it doesn't work. It stays white.

Any suggestions? I still haven't managed to get this working; thank you.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • 1
    if you want to use @color/background_grey you need to make a color.xml file I believe. Otherwise you could use the android defaults to check... see this question http://stackoverflow.com/questions/3769762/android-color-xml-resource-file – O'Mutt Sep 24 '12 at 20:25
  • Change your layout color to @android:color/white and start from there. Also, where are you using your view_fact? You only have that LinearLayout in your main? – Tiago Almeida Sep 24 '12 at 21:10
  • I'll try changing the layout color to that now. And yes, view_fact is another page, and when a button is clicked it changes the content view to that page. – EM-Creations Sep 24 '12 at 21:21
  • I just tried setting: android:background="@android:color/white" in main.xml and still it didn't change the background colour. – EM-Creations Sep 24 '12 at 21:25
  • What is inside the linear layout? Is it possible that there is something blocking the linear layouts background? – O'Mutt Sep 25 '12 at 17:09
  • That's the entire linear layout start tag and the background is still black. – EM-Creations Sep 25 '12 at 18:04

4 Answers4

0

If you want to use @color/color_name you have to create a Color.xml file and then it will be accessible in the manner you tried to use it.

O'Mutt
  • 1,557
  • 1
  • 13
  • 27
  • 3
    I use color references like that all the time without a specific color.xml file. They work fine when defined as shown in the strings.xml. – Robert Nekic Sep 24 '12 at 20:42
  • 1
    No, that isn't the problem. I tried moving my colour definition to color.xml and it still didn't work. Thanks for your suggestion though. – EM-Creations Sep 24 '12 at 21:19
  • 1
    nope, I'm wrong there. See my answer but also http://developer.android.com/guide/topics/resources/more-resources.html tells us the filename is arbitrary – Paul D'Ambra Sep 24 '12 at 22:00
0

Forgive the somewhat obvious questions, but: are you calling setContentView(R.layout.main) in your Activity onCreate? Is there something else in the layout that is filling the LinearLayout and covering the background grey?

Robert Nekic
  • 3,087
  • 3
  • 24
  • 36
  • In my onCreate method I'm calling: setContentView(R.layout.main); – EM-Creations Sep 24 '12 at 21:08
  • Are you using Eclipse? If you open you layout file you should be able to switch to the Graphical Layout tab and see that the background color is properly set. If you see the right color there, then presumably something else is going on. – Robert Nekic Sep 24 '12 at 21:10
-1

Looking at the pastie you linked in the comment I think you need to remove the <?xml version="1.0" encoding="utf-8"?> line at the start. Eclipse won't build a freshly created app with that xml root in a layout..

Although that's strange as the first two examples I've found on the Android Developer site (for example) include it

I'd be interested to see what happens if you remove that line...


In Eclipse if I use

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E8E8E8"
>

then the background colour changes.

If I change that to

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@string/background_grey"
>

by adding <string name="background_grey">#E8E8E8</string> to /res/values/strings.xml the background changes.

This also works if I declare it as android:background="@color/background_grey" in the layout and <color name="background_grey">#E8E8E8</color> in strings.xml

and if I setup res/values/colors.xml <= I've used colors.xml (while you've used color.xml) however http://developer.android.com/guide/topics/resources/more-resources.html tells us the filename is arbitrary as you can see by the declaration working in the strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background_grey">#E8E8E8</color>
</resources>

and declare the layout as

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>

then the background also changes...

If it doesn't behave the same for you then there's something funky going on or you're using a different layout than the simple example above and, well, there's something funky going on in that layout.

Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
-1

Okay, I've tried lots of different methods of trying to set the background colour and none of them are working. And I'm now having problems with text not updating. So I'm going to try remaking it. Using eclipse and ill make a new question if I'm still having issues. Thanks for the suggestions.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56