Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Image Placement

I'm trying to set my root LinearLayout element (which is set to fill_parent in both dimensions) to have a background image which is always located in the lower left corner of the screen no matter the orientation of the device. It would be excellent if there were some way to set the background image position such as that which you can do with css using "background-position: left bottom;" but I'm not seeing a way to achieve this in Android. Is there a way to do this?

Thanks for the help.

like image 794
Ryan Avatar asked May 06 '10 13:05

Ryan


People also ask

How many ways background picture can be positioned?

background-position There are several ways to specify the position of a background image, but the easiest is to use the positioning keywords left , right , top , bottom , and center . This gives you nine possibilities: left top.

Which is correct relative path for background image?

Use the Relative Path /image/picture to Set the Background Image in CSS. We can use the /image/picture file path format to set the background image in CSS. In such a format, the image is located inside the image folder. The image folder is located at the root of the current web.

How do I change the position of an image in CSS?

You can easily position an image by using the object-position property. You can also use a bunch of other ways like float-property that will be discussed further in this article. Methods: object-position property: Specify how an image element is positioned with x, y coordinates inside its content box.


2 Answers

You'll have to create a custom bitmap drawable with your bitmap in an XML file (eg "res/drawables/my_drawable.xml"

<?xml version="1.0" encoding="utf-8"?> <bitmap     xmlns:android="http://schemas.android.com/apk/res/android"     android:src="@drawable/my_png_file"     android:gravity="bottom|left" /> 

And then set this drawable xml as your view's background ("@drawables/my_drawable"). The drawable XML format is very poorly documented in the Android site, though, so it's definitely not an easy problem to figure out how to solve on your own.

like image 65
Yoni Samlan Avatar answered Oct 03 '22 00:10

Yoni Samlan


If found an advantage doing this programatically: the image fits the layout instead of being bigger then it. No ideia why.

The code:

Bitmap bmp = BitmapFactory.decodeResource(getResources(),                 R.drawable.image); BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); bitmapDrawable.setGravity(Gravity.BOTTOM|Gravity.RIGHT); LinearLayout layout = (LinearLayout) findViewById(R.id.background_root); layout.setBackgroundDrawable(bitmapDrawable); 
like image 29
neteinstein Avatar answered Oct 03 '22 00:10

neteinstein