Cubsoft
Cubsoft

Reputation: 1157

Android - Unable to set background image but visible in designer

I'm a bit of a Android noob, as of today.

I have tried setting a picture to the background as follows;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="C:\Users\Jacob\Downloads\xmas.jpg"
android:layout_height="match_parent"
tools:context=".MainActivity" >

But when i build it comes up with;

'Description Resource Path Location Type error: Error: String types not allowed (at 'background' with value 'C:\Users\Jacob\Downloads\xmas.jpg'). activity_main.xml /FirstAndroidApp/res/layout line 1 Android AAPT Problem'

When i go in to the designer it shows the background as i would like it, but just comes up with an error.

Am I not allowed to set a background in this way? or am i missing something?

Upvotes: 0

Views: 1311

Answers (3)

Android Killer
Android Killer

Reputation: 18489

In android you should not set background like this. Search a little and you can get your answer.

  1. Put your image xmas.jpg in res/drawable folder.
  2. Change android:background="C:\Users\Jacob\Downloads\xmas.jpg" to android:background="@drawable/xmas"

In android .png is more recommended to use and while using like as i said above no need to give extension like .png or .jpg..

This will solve your problem

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

to get work it you will need to copy your image in <Your_Project>/res/drawable folder and then set RelativeLayout background as :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/xmas"
android:layout_height="match_parent"
tools:context=".MainActivity" >

and for more information for adding images in your project see

http://developer.android.com/guide/topics/resources/drawable-resource.html

Upvotes: 1

A--C
A--C

Reputation: 36449

You have to copy the picture into your drawable folder then reference it like so:

android:background="@drawable/xmas"

Upvotes: 1

Related Questions