Background image of Activity - android

How can I include an image as the background and where and how to put an image in the application, I am using Eclipse and all the development tools.
Here's what I have done so far in the main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0"
tools:context=".MainActivity">
</RelativeLayout>

put your image in res/drawable directory
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_imagename"
tools:context=".MainActivity" >
</RelativeLayout>

just search and you will get it.
just put your image inside res/drawable folder and change
android:background="#C0C0C0"
to
android:background="#drawable/your_image_name" (dont give extension of image)

Put a .jpg or .png image inside the res/drawable folder and change the background attribute to this:
android:background="#drawable/image-name"
where image-name is the actual name of the image.
As you'll see, there are several drawable folders, like drawable-mdpi or drawable-hdpi. They are designed to store images for devices with different screen density. To make sure your application looks good on different devices, provide alternative images for every density. The #drawable/image-name attribute doesn't need to be changed then, the Android will pick the right drawable folder for you. Hope this helps.

You can only use .jpg and .png format images. after that,
copy your image and paste it in res Folder in your application.
if the name of your image is image.png
use this code..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image"
tools:context=".MainActivity" >
</RelativeLayout>

Related

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

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>

How to run an Pinch-Zoom example on android?

To run an example on android to use PinchZoom I found the following example. I was able to write a working example code which shows the View from the example - but it is all white! How can I use an actual image in this example?
I tried the following in the activity-xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<com.example.alexander.capycoding_pinchzoom.CanvasView
android:id="#+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/image"
android:textColor="#FFFFFF" />
</FrameLayout>
to use an actual non-white image, but it does not work. (I also renamed the class from ZoomableImageView to CanvasView).
Maybe there is another/better example on how to make PinchZoom workable on a Canvas/Bitmap in android?
you are using a default image in the xml.use your image and by the way simply putting xml cannot alone perform the action.i suggest to use library files for the pinch zoom and then create a class for the zoom specification

Android Studio - Failed to convert #drawable/ into a drawable

I am getting this error when I am trying to use an image in Layout file:
Couldn't resolve resource #drawable
Failed to convert #drawable/image into a drawable
Here is my layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/image"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I tried restart IDE, clean-rebuild but nothing, and I read all threads related to it ( I think ).
And yes my image name contains only [a-z0-9_.].
I had the same problem, I solved it by renaming my image into something like : my_image.
Well, I found a way to avoid this
RightClick on drawable folder then New => File insert its name (myImage)
for example Then Select Image Files.
Then in drawable folder I replaced myImage file with the real image.

ImageView of PNG doesn't appear (maybe it's too big)

Inside my AlertDialog I've an ImageView, when I choose to show the android:src="#drawable/logo_small" (a png around 64x64px in every folders /hdpi /ldpi ect..) all works fine. Instead when I show android:src="#drawable/logo_big" (it's just ONE png of 1417x1417pixel and 593KB in the drawable root folder) the image doesn't appear.
The layout:
<?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:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageLogoBig"
android:layout_width="30dp"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo_big" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="#drawable/gplay_logo" />
</LinearLayout>
The imageLogoBig doesn't show in the upper example and in the lower example:
<ImageView
android:id="#+id/imageLogoBig"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo_big" />
I just discovered while I was writing the question that if I delete from drawable root directory the image logo_big and if I copy it in drawable-hdpi, drawable-ldpi, drawable-mdpi and drawable-xhdpi, all works fine. And this is strange because the other image: imageView1, is only in the drawable folder (but is 172x60px) and is showed correctly.
Why is this happening? Any suggest? (For example put logo_big only in drawable-xhdpi...)
I solved moving logo_big png image only to the directory drawable-xhdpi, don't know really why, but it works now.
This can be for a number of reasons, one is the name of the file not being compliant, that's clearly not your case but I'm pretty sure it's of many others like me that would reach this question when looking for an answer.
In a nutshell, underscores '_' are permitted but hyphens '-' are not, maybe because these are used to add resource qualifiers to file names (or at least directories).
This is something easy to remember, but even easier to forget. So, to put an example, this file didn't work:
poa-sample.png
And the solution was to rename it to:
poa_sample.png
I leave this here JFTR, because as stupid as this is, it a made me waste a lot of time and try everything else before. You'd expect Android Studio to give an error at build time, as it would do with an invalid variable identifier for example, but as of today it won't give you even a proper warning.
try renaming your image file to "logobig", without an underscore and see if that works. Even though file-based resource names allow underscores, i've had trouble occasionally

how can i insert an image having a .gif format?

Can you give some projects how to insert an image like that on android?I want to add that image in my xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
How can I display it my image?
Use an ImageView. It takes all the standard formats.
You can use an ImageView:
<ImageView
android:id="#+id/logo_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/logo" />
and place your image file into res/drawable/logo.gif (or in a resolution specific drawable directory)
If you want to display moving GIF then there is not direct aproach, You have to try a way around, Refer bellow link for more details.
REF GIF in Android
OR you can load the image in a web View and display it. ( web view should be able to display the animation )

Categories

Resources