Java – Create a background image for Android applications – Java

Create a background image for Android applications – Java… here is a solution to the problem.

Create a background image for Android applications – Java

I’m just starting out on Android and need help with background images. I want to be able to have a background image and then overlay other items (buttons, text, etc.) on top of that background using a layout. I use LinearLayout just for simplicity because I don’t know what works best for me right now.

Anyway, I can’t display the image with the following code:

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;

public class NewGameActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

LinearLayout ll = new LinearLayout(this);
    ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png"));
    this.setContentView(ll);
    }
}

Solution

ll.setBackgroundResource(R.drawable.image_name); 

http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29

This is the preferred way to access drawable resources. image_name is the png image in your drawable or drawable-mdpi folder.

Related Problems and Solutions