How to add padding for background image - android

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">

Related

three types of tiles for one Android screen

I want to use three kinds of repeating tiles as background for one Android screen. The following is what I want to achieve as an end result.
Can it be done in Android?
Create a xml in drawable folder, and use the following code
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/YOUR_IMAGE
android:antialias="true"
android:dither="false"
android:filter="false"
android:gravity="fill"
android:tileMode="repeat" />
Then set this file to your background. :)
Creating three layouts inside the main layout achieves the design.
<LinearLayout 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:orientation="vertical"
tools:context=".SplashActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_weight="4"
android:background="#drawable/woodentile" >
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1"
android:background="#drawable/woodenshadowtile" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1"
android:background="#drawable/papertile" >
</LinearLayout>
</LinearLayout>
You need three bitmap files in the drawable folder. I will show woodentile.xml as an example.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="#drawable/inno_luck_wood_pattern_background"
android:tileMode="repeat" >
</bitmap>
That's it. Doing this results in the kind of design that I want to create.

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>

Background image in linear layout is not well sized

I made a linear layout with a background image, a png... I don't know how to show it into the layout ( and centered ) keeping proportions... here is the code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
Obviously image has the same height and width of the display... Any help?? Thanks in advance
=.4.S.=
Thank you all ^^ I solved in this way:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/background"
android:layout_gravity="center"/>
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFFFFF"
android:layout_weight="2"
android:drawSelectorOnTop="false"
android:layout_gravity="center"/>
</FrameLayout>
Try this related question/answer:
Android: Scale a Drawable or background image?
I haven't tried it, but it sounds similar to what you're talking about.
If you do not want your picture to be stretched, you can use BitmapDrawable with gravity set us instead of png.
Refer docs: Bitmap

how to add background image to activity?

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...

My ListView backgrounds go black during scrolling - how to fix?

Ive specified white backgrounds for pretty much all widgets and it works, except when scrolling. The background container goes black during scrolling resulting in annoying flickering.
You will need to use the setCacheColorHint() method of ListView.
Example: list.setCacheColorHint(yourBackColor);
Explanation of the issue and solution here
ScrollingCache="false" in your activity's *.xml file for the list object should do the trick, too.
I fixed this problem by making the background color of the cells the same color as the ListView, so:
<?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:background="#FFFFFF">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"> <---- Background color of ListView
</ListView>
</LinearLayout>
and then the row:
<?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"
android:padding="8dip"
android:background="#FFFFFF"> <--- Background color of the cell
<LinearLayout
android:id="#+id/projects_cover_row_image_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#444444"
android:layout_centerInParent="true"
android:layout_marginRight="8dip">
<ImageView
android:id="#+id/projects_cover_row_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/projects_image_content"/>
</LinearLayout>
...
</RelativeLayout>
that completely got rid of the problem for me and seems like the best solution
Just put background on the top and no highlight or other bad effects simple and no need of hint or cache
in listview layout
android:background="#000000" for black background or android:background="#FFFFFF" for white
<?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="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/testscheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:gravity="left"
android:textColor="#color/ics"
android:textSize="14sp"
/>
</LinearLayout>

Categories

Resources