How can I add a background to a view, without resizing its size, only showing the part of the image that fits the previous space?
Example:
I have the following initial layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8900"
android:orientation="vertical"
tools:context="com.tappx.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#0f0"
android:gravity="center"
android:text="HELLO" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#f0f"
android:gravity="center"
android:text="HELLO" />
</LinearLayout>
</LinearLayout>
And I want to apply the following image as gradient to the white background:
To achieve this (please note the gradient is filling the horizontal and has bottom gravity):
TARGET:
My problem is that setting the gradient as android:background expands the container view size to this (losing the red bottom part)
Also tried making a xml bitmap with no results:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="bottom"
android:src="#drawable/gradient" />
</item>
</layer-list>
How can I achieve it?
The image you are using is very long, and your container is set to wrap content and so it is expanding to accommodate the image.
Since you are already using fixed heights on the inner elements, you can simply set a fixed height for the container (200dp) and it'll work
Related
I'm trying to stretch vertical line in calendar_header.png horizontally as background in xml style:
<style name="CaldroidCalendarViewLayout">
<item name="android:background">#drawable/calendar_header</item>
</style>
But it changes height of the control by height of the background. How can I solve that? I just want to fill the actual size of the control with stretched background. (It acts like background image is a part of the content - not like it's just a background.)
The following answer helped me: https://stackoverflow.com/a/19405297/865475
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/activation_popup"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignTop="#+id/activation_layout"
android:layout_alignBottom="#id/activation_layout"
android:layout_alignLeft="#id/activation_layout"
android:layout_alignRight="#id/activation_layout"
android:contentDescription="#string/act_code_label" />
<LinearLayout
android:id="#id/activation_layout"
android:clipToPadding="true"
android:padding="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- LinearLayout wraps a bunch of smallish views here -->
</LinearLayout>
</RelativeLayout>
I have a problem with aligning 2 imageviews in a RelativeLayout:
these 2 images need to be close to eatch other without space. Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_portrait"
android:orientation="vertical" >
<!-- header -->
<ImageView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/header" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/dia1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/dia1" />
<ImageView
android:id="#+id/dia2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dia1"
android:contentDescription="#string/desc"
android:src="#drawable/dia2" />
</RelativeLayout>
What am I doing wrong?
UPDATE
dia1 is:
dia2 is:
From the 1st picture you posted, it seems the bottom image has a great deal of transparent "padding" (not actual padding, just transparent image pixels between the visible part and the upper limit).
In my opinion, you should combine RelativeLayout with cropped versions of your upper and lower images.
Otherwise, consider checking this SO thread about overlapping images.
Your question doesn't explain much about the problem. So you can add a single image. I mean merge the 2 images before and use it as 1.
Hey Guys facing a problem with setting a background image on my ListView. Code is as below
history.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:src="#drawable/back">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
rowlayout.xml
<?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">
<TextView android:id="#+id/ProductName"
android:textSize="25sp"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Java Code
...
setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,R.id.ProductName,Names));
...
I dont know what i am doing here but the background does not appear. Its black throughout. If i put the background on the text view in rowlayout.xml then it appears but its like whole screen carries one entry of the ListView. Tried alot of tutorials. Maybe doing some rookie mistake.. help me out please
use android:background="#drawable/icon" for listview like
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="#drawable/icon">
</ListView>
use android:background="#00000000" on your listview items, then set the parent forms background to your image
As I understand your code concept correctly you want to achieve two Views lying on the LinearLayout, one of which (TextView in your case) cover layout with it's background and another one is transparent. So there is no other way than to set background for your layout and make your ListView transparent, using android:background="#00000000" and also you should set android:cacheColorHint="#00000000" to avoid filling your ListView background with black during scrolling
I believe what you're experiencing is described here: http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
Because of an optimization called "cache color hint", the background of your list is being set by default to the window's background color, #191919 in Android's dark theme.
If you set a background on each row, I think you'll see what I mean. Try setting the following background shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
android:color="#color/mid_gray"
android:width="1dp"
/>
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="3dp"
/>
<corners android:radius="10dp" />
<solid android:color="#android:color/white" />
</shape>
like this:
<?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="#drawable/shape_data_background">
<TextView android:id="#+id/ProductName"
android:textSize="25sp"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
i am currently making one android application, i moded title bar, so all content for title bar is holded in window_title.xml where i have LinearLayout set: paddingLeft="5dip" . And now i need to load one imageView on the right side of this titleBar how i am able to some kind of overwrite this paddingLeft i cant delete it as it required for text and image on the left but i need it on right too...
Here is current xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextTexast"
android:textSize="10pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dip"
android:textColor="#F7F7F7"
/>
</LinearLayout>
If I understand your question correctly, you're looking to apply some padding to an ImageView that you display to the right of the titlebar TextView. You can use android:paddingRight in your root LinearLayout like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip
android:background="#222222">
Alternatively, you can specify the android:paddingRight attribute in the ImageView that you want to align to the right.
To move the grey figure to the right just set android:layout_gravity for that to "right".
Hello can any one help me, I am trying to get a graphic to be at the bottom of the screen and centered using LinearLayout. I would rather use the Bitmap method, because this is for a game. Thanks
Not sure what you mean by bitmap method but this will do what you ask:
<?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="horizontal"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_gravity="bottom"
/>
</LinearLayout>