how to add background image to activity? - android

using theme or ImageView ?

use the android:background attribute in your xml. Easiest way if you want to apply it to a whole activity is to put it in the root of your layout. So if you have a RelativeLayout as the start of your xml, put it in here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
</RelativeLayout>

You can set the "background image" to an activity by setting android:background xml attributes as followings:
(Here, for example, Take a LinearLayout for an activity and setting a background image for the layout(i.e. indirectly to an activity))
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/icon">
</LinearLayout>

Place the Image in the folder drawable. drawable folder is in res. drawable have 5 variants
drawable-hdpi
drawable-ldpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi

We can easily place the background image in PercentFrameLayout using the ImageView. We have to set the scaleType attribute value="fitXY" and in the foreground we can also display other view's like textview or button.
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ImageView
android:src="#drawable/logo"
android:id="#+id/im1"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<EditText android:layout_gravity="center_horizontal"
android:hint="Enter Username"
android:id="#+id/et1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="30%"
/>
<Button
android:layout_gravity="center_horizontal"
android:text="Login"
android:id="#+id/b1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="40%"/>
</android.support.percent.PercentFrameLayout>

ez way : copy your picture in this location: C:\Users\Your user name\AndroidStudioProjects\yourProjectName\app\src\main\res\drawable
and write this code in your xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/background">
</RelativeLayout>
in line 7 write your file name instand of background
and Tamam...

Nowadays we have to use match_parent :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/background">
</RelativeLayout>

and dont forget to clean your project after writing these lines you`ll a get an error in your xml file until you´ve cleaned your project in eclipse: Project->Clean...

Related

Image not Showing

Image is showing in studio but not in Phone.
Image is placed in res.
Running on one plus one.
using linear layout
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/wifi"
android:id="#+id/imageView3"
android:layout_centerHorizontal="true"
/>
You have to add below line in to your root layout.
xmlns:app="http://schemas.android.com/apk/res-auto"
Please see below example.
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:clickable="true"
android:orientation="vertical">
<ImageView
app:srcCompat="#drawable/ic_user_red"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
without xmlns:app="http://schemas.android.com/apk/res-auto" you can not use app:srcCompat.
Inside res folder there like drawable,drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi etc.
You should make sure that you copied your image in drawable folder not in Idpi or mdpi
try it by Changing the code to
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/wifi"
android:id="#+id/imageView3"
android:layout_centerHorizontal="true"
/>

layout_below item inside included layout

I'm trying to figure out a way to align an item in a layout in respect to an item within an included layout.
Here's a visual representation:
And here is example code that I'm looking for (which obviously doesn't work):
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/info" />
//This would be the dark grey box
<RelativeLayout
android:layout_below="#id/item1">
</RelativeLayout>
</RelativeLayout>
included.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp">
<ImageView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingLeft="100dp"/>
</RelativeLayout>
I'm assuming the best way to accomplish this would be through dynamically positioning the dark grey box in code, but I have no idea where to start. Any help would be awesome.
Can you make this below your include? Something like this (instead item1 use info):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/info" />
//This would be the dark grey box
<RelativeLayout
android:layout_below="#id/info">
</RelativeLayout>
</RelativeLayout>

Adding ImageView in XML File causes program to stop unexpectedly

I am attempting to add a tiled image as a background of the program.
The code that I am currently using in main.xml, which is crashing:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ImageView>
However, when the ImageView is removed and the xmlns is moved to the LinearLayout, it functions fine (without, of course, the image).
I can't see any related errors in LogCat.
More information:
Using Eclipse and Android 2.2, API 8. The program runs but crashes instantly.
You can not put anything in an ImageView. It's not a layout. Use the background attribute of the LinearLayout to set the background to your image.
Another option would be to wrap everything in a RelativeLayout. Separate the ImageView and the LinearLayout. The image will fill the RelativeLayout and the LinearLayout will be on top of it.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</RelativeLayout>
You should not add a linear layout inside an imageview. Have a linearlayout at the top as root and then add the image view in there

Programming with eclipse, not recognizing my android code in XML

I'm brand new to eclipse and programming and I'm following video tutorials but I can't figure out why my eclipse doesn't recognizing the coding. In the video I see that he adds
android:gravity=center
android:background="#color/red"
and it changes color but mine does not..
the android:gravity changes to a pink color and the =... changes to blue but mine stays black and when I look at the graphic layout the code is not registering
<?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" android:orientation="vertical">
android:gravity=center
android:background="#color/red"
<TextView
android:layout_height="wrap_content"
android:text="#string/red" android:layout_width="wrap_content"/>
</LinearLayout>
Here is the corrected Code,
<?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"
android:orientation="vertical"
android:gravity="center"
android:background="#ff0000"
>
<TextView
android:layout_height="wrap_content"
android:text="#string/red"
android:layout_width="wrap_content"
/>
</LinearLayout>
Color can be also defined in XML files.
Create a new xml file name color.xml inside /res/values.
Paste the following code inside
<color name="red">#FF0000</color>
Also Make sure you have the following line in strings.xml
<string name="red">Your text</string>
and change the code as follows.
<?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"
android:orientation="vertical"
android:gravity="center"
android:background="#color/red"
>
<TextView
android:layout_height="wrap_content"
android:text="#string/red"
android:layout_width="wrap_content"
/>
</LinearLayout>
<?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" android:orientation="vertical">
-----------------
android:gravity=center
android:background="#color/red"
-----------------
That bit is wrong. It has be declared inside the layout object. Remove it.
<TextView
android:layout_height="wrap_content"
android:text="#string/red" android:layout_width="wrap_content"/>
</LinearLayout>
Have you declared the color red in your resource folder under string xml file?
Read
Missing the quotes in android:gravity="center"
You need to create a color resource: <color name="red">#FF0000</color>
You can find the documentation for the color resource here: http://developer.android.com/guide/topics/resources/more-resources.html

How to add padding for background image

I have a LinearLayout which has a background image (a 9 patched png file).
How can I add padding to left and right so that the background image does not take up the whole width? I have tried android:paddingLeft and android:paddingRight, but that does not change anything.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:background="#drawable/background">
The whole background still stretches the whole screen width.
You should use xml drawable, specially designed for this purposes - Inset Drawable:
http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset
For example:
res/drawable/inset_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/your_background_image"
android:insetRight="25dip"
android:insetLeft="25dip" />
and then in your xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/inset_background">
Ur background drawable should be like this,using android:bottom
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="48dp" android:drawable="#drawable/bg">
</item>
</layer-list>
That doesn't work because padding only acts on the contents of the LinearLayout. By using a second LinearLayout inside this one the padding will take effect. You must define the background color of the first LinearLayout that will be visible in the padding area.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:background="#FF000000">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
</LinearLayout>
</LinearLayout>
Note: This is probably also possible by using an XML file for the background.
You can just use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:background="#drawable/background">

Categories

Resources