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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With