How do I add a background image to an XML file? - android

How do I insert a background image in an xml file? I would like to add a background image to my APK (for mobile phone), I have already found the xml file, but I don't know where to insert the ID of the image and also not whether android: background or android: icon Can someone help me? The XML is in the picture and btw sorry for the picture i dont know how to copy a xml code in the question

It's done with android:background="", like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_image_id">
</LinearLayout>

Related

Dynamic "src" for <bitmap> tag in Android xml

Question: Is there a way to dynamically set the "src" attribute on an XML "bitmap" in an Android XML file?
I have a custom XML drawable named "repeat_background.xml" defined as such:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
I use this drawable to "tile" a 1x1 jpg (named background.jpg) as the background for all the pages in my app, and it works great - here's an example setting it as the background of a LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/repeat_background">
However, I want the actual jpg (in this case "background.jpg" from #drawable/background) to be based on the user's preference - so I'll have a list that allows the user to pick red, blue, orange, etc. so the user can override the background color shown in the app, and I'll have a 1x1 jpg corresponding to each available color in my resource bundle - but how do I show the preferred jpg as the background?
I don't want to manually have to call some code in every Fragment or Activity, I want something that will respect the user's preferences and react accordingly.
I've tried to extend the BitmapDrawable class, but didn't get very far. Any suggestions for how I can accomplish this are greatly appreciated.
You can do it in java code, using setBackgroundDrawable(new ColorDrawable(color)). It can't be done in xml.

Setting up a background image in Android Project

I'm building an Android App using Eclipse. I'm trying to create a simple image display when the application opens.
I've saved my intro_image.png on res\drawable-hdpi in my workspace, I've created another xml for the layout with the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/intro_image">
</LinearLayout>
An error message appears when I view the result on the Graphical Layout, saying: "Failed to convert #drawable/into_image into a drawable. Couldn't resolve resource #drawable/intro_image"
How do I solve this?
Thank you.
try to refresh or clean your project. it will work.whenever you paste an image into drawable you have to refresh that folder.

xml file vs xml layout

I created two layout files.
Android XML Layout File which created this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and an Android XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
From the looks of it they are both the same. When to use one over the other since they are both creating the same xml?
Yeah they are prograticaly the same, there isn't different type of xml. What matters, is the location of the xml (layout, values, ...).
In eclipse, there maybe is a small difference in the IDE view, i'm not sure that you have the Graphical Layout view if you create a Android XML file instead of a layout file (it's a detail because, you still can add this view ;) )
But results apk will be identical.
The reason why you would use an Android XML, is if you wanted to use the Layout Tool (the GUI layout editor). The Eclipse IDE will react to this file differently.
But, the code is the same, so the APK you create will also be the same - it doesn't matter in your final output.
I always use Android XML files, so they integrate into the ADT tools better.
the only difference as far as I can see is that in xml you can't use both "android:layout-width:" and "android:layout-height:" attributes which makes xml for example suitable for the use of menu and item bar and other uses.

Android Framework modification : Adding a custom layout

I am trying to add a new layout to the framework say my_layout.
I succeeded partially in that .
I defined my layout in an xml and put it inside frameworks/base/core/res/res/layout
I then mentioned the id in frameworks/base/core/res/res/values
public.xml and ids.xml
And after performing a complete build :
make framework
make update-api
make
i am able to access it as android.R.layout.my_layout
But now i want to access it through xml also :Eg: "#android:layout/my_layout"
In which place in the source do i mention this reference ?
Try to declare the id in your my_layout.xml file in your layout folder.
for example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
...
Happy coding :)

Android background doesnt show correctly

Ok I have created a background image in Inkscape. Exported that image and it looks correct, when I open it from Eclipse. However, when I build the project and apply that background to the Layout, it shows the complete opposite in the designer and on the device.
Here is what the image is supposed to look like. Correct background
Here is what the image looks like in the Eclipse Designer.Incorrect background.
Here is my Xml on a brand new Xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_view_bg"
android:orientation="vertical" >
</LinearLayout>
I am utterly confused on this one. Any help is greatly appreciated.
You are most likely using a theme where the background in dark. As your background gradient is transparent the black background is showing through.
Switch to a light theme.

Categories

Resources