Creating a background image for an Android application - Java

I am just starting Android development and I need help with background images. I want to have a background image and then overlay other elements (buttons, text, etc.) on top of this background using a layout. I used LinearLayout just to be simple, and because I don’t know what is best for me at the moment.

In any case, I cannot get the image to display using 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); } } 
+4
source share
2 answers
  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 resources with the ability to draw. Image_name is a png-image in a folder with the ability to draw or draw-mdpi.

+4
source

yes, if you use XML, then find the identifier of this line output, for example

 ll=(LinearLayout)findViewById(R.id.linear1) 

Then set its background as

 ll.setBackgroundResource(R.drawable.image_name); 

else here, as your code shown here, you can directly go to

 ll.setBackgroundResource(R.drawable.image_name); 
+1
source

All Articles