How do I set the background to a solid color? When I use setContentView the screen is blank

0

I have looked and tried a lot of different things but no matter what I end up doing the screen is always blank and I'm sure it's something really dumb I'm doing and I'm hoping someone will catch it.

I'm trying to alternate background colors but before I even get to that I need to get it so that even one background color will display properly.

First, my xml layout works fine and when I got to the layout view it displays the color just as I want it to. When I go to setContentView() in the activity that calls the xml it is never displayed and I only get a blank screen.

Second, since this initial issue described above I have tried several fixes and have numbered them accordingly. As I did a fix I usually only bothered to comment it out instead of deleting it after it didn't work. After certain lines there is a number, so if three lines have 1's behind them then those were the three lines used in attempt #1.

Third, while trying these fixes I added a colors xml file while I'll display as well.

Finally, I'll show my main activity first, xml file second, and colors file last. As you can see my ultimate goal would be to change the background dynamically but I can't even to get it to work normally right now. And FYI my splash screen works fine. But that's an image.

Thanks for you help.

public class Blink extends Activity {
    long startTime= System.currentTimeMillis();
    long now=0;//the current time in millis

public void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //TextView backgroundColor=new TextView(this);2,3,4,5,6
    //backgroundColor.setBackgroundColor(0xFFFF0000);5
    //backgroundColor.setBackgroundResource(R.color.royalBlue);2,3,4
    //backgroundColor.setVisibility(0);//make visible 3
    setContentView(R.layout.blank);1
    //setContentView(backgroundColor);4,5,6
    //backgroundColor.setBackgroundColor(Color.argb(255, 255, 255, 255));6

            //setContentView(R.layout.blink_blue);
    //blink from royal blue to blank
            /*while(true){
        startTime= System.currentTimeMillis();
        do{
            now=System.currentTimeMillis();
            setContentView(R.layout.blink_blue);
        }while((-(startTime-now))>1000);

        do{
            now=System.currentTimeMillis();
            setContentView(R.layout.blank);
        }while((-(startTime-now))>1000);
    }*/
}

This begins the xml file

//it is formatted properly but for some reason stack overflow doesn't like it so I'm only posting relevant lines. 

//This is a Linear layout
android:id="@+id/blinkBlue"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/royalBlue"

This begins the colors file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="royalBlue">#4169e1</color>//Yes I have tried #FF4169e1 instead
    <color name="plainBlue">#ff000000</color> 
    <color name="darkBlue">#ff000000</color>
    <color name="black">#00000000</color>
    <!-- I also know that the blues here aren't those colors... I'll change that when I fix this thing. -->
</resources>
android
screen
asked on Stack Overflow Feb 2, 2011 by Craig • edited Sep 8, 2013 by Jonathan Leffler

2 Answers

0

You don't set colors that way, You specify colors of your Layouts that are defined in your XMLs and then you setContentView() to that XML file.

For example, lets pretend that your XML file has the name as my_layout.xml, then you have specified the color in my_color.xml so you go this way:

  • You write a layout in my_layout.xml
  • Now you write a resource XML for color as you have written above, and save it in the /res/values/my_color.xml
  • Set the layout (defined in my_layout.xml) background to be `android:background="@color/my_color"
  • in your code, use setContentView(R.layout.my_layout)

This will make my_layout.xml to be your content's layout and further the background color thing will be handled by the layout inside my_layout.xml file.

I hope that helps.

answered on Stack Overflow Feb 2, 2011 by Aman Alam • edited Feb 2, 2011 by Aman Alam
0

Take a look at using a state list drawable as a background. Each item can point to a shape drawable that specifies a different background color. Alternatively, use a shape drawable as a background that points to a color state list as the solid color.

If the built-in attributes available for defining a state list drawable or a color state list are not right for your application, you can use the technique shown in this thread to define your own.

answered on Stack Overflow Feb 2, 2011 by Ted Hopp

User contributions licensed under CC BY-SA 3.0