I’m creating a small game for a programming assignment. I’m trying to figure out how to set the background properly, but I can’t seem to figure out.
I have a two linear layouts in my main.xml; one has a background color, and another layout that’s inside it has a background image (transparent PNG). It shows up fine when I start up the app:
<?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="#aacceeff"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
>
</LinearLayout>
</LinearLayout>
However, I now have to add the animation. To do that I created a View object that loads up images and animates them. However, I now have to set the content view from the main activity to an instance of this new view object. When I do that, I get a null pointer exception because it cannot find the linear layout with id background:
BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
LinearLayout backgroundLayout = (LinearLayout) findViewById(R.id.background);
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
backgroundLayout.setBackgroundDrawable(tiledBackground); //backgroundLayout is null
I figure this is because the view is no longer the view that’s specified in main.xml. If I remove this code, the code runs fine but I get a black background. I tried moving this specific snippet to the custom view and set the background to the image, but then I get the image with a black background; what I actually want is a transparent image with the background color showing through.
I guess what I’m trying to figure out is how to tell the custom view to use the layout I’ve defined in main.xml, or figure out a way to specify a background color and a background drawable for the custom view.
I was able to solve this by using a color filter: