Okay I have a layout for my ListView's items:
<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/draweritemtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:fontFamily="sans-serif-thin"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#111"
android:textSize="20sp" />
<View
android:id="#+id/separator"
android:layout_width="fill_parent"
android:layout_height="0.2dp"
android:background="#android:color/darker_gray"
android:visibility="visible" />
</LinearLayout>
So all works fine with this setup. It shows up like this:
ITEM
-------
ITEM
-------
ITEM
-------
But I don't want the blue select color from android, so I would like to change it by changing the textviews background to #drawable/drawer_active_selector.
Here is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/notquiteverylightgray" />
<item android:drawable="#drawable/white" />
</selector>
And my drawables
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="semitransparent_white">#77ffffff</drawable>
<drawable name="verylightgray">#eee</drawable>
<drawable name="notquiteverylightgray">#ddd</drawable>
<drawable name="white">#fff</drawable>
</resources>
Now it will look like this:
ITEM
ITEM
ITEM
------
The padding is right, but the things I miss are the seperators (The <View />)
How can I get them back?
There are a couple of mistakes in your solution. By defaultListView's item clicks are handled by the ListView itself, not by the child view that was clicked. So instead of changing the TextView's background to #drawable/drawer_active_selector you should set the item selector drawable of the ListView using android:listSelector xml property or one of List.setSelector() methods. The other mistake concerns the dividers. Your solution is unnecessarily verbose. There is a much easier way to add dividers. Just use ListView's android:divider property or ListView.setDivider() method.
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 row of buttons each with numbers on them. I show which one is selected by changing the background, but one of these number would be the "current" number (kind of like a current date) and I want to indicate that it is the current one even when it is selected (since it will have the same background as all other selected ones) by adding a circle around the text
Kind of like this (with 23 being both selected and the current one):
I tried doing this by adding a radius to the button and adding padding but was having a bit of trouble, but even if it worked it feels like there is probably a better way.
How do I make the circle around the text in the button?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/num_txt"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="0dp"
android:background="#drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
</RelativeLayout>
bg_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="#FF0000"/>
</shape>
or try This or This
I'm hoping there is an xml solution to this... I am trying to create a layout that has rounded corners and then filling that layout with an image (tiled, repeat). So basically I have a LinearLayout that has a drawble layered-list assigned to the background. Here is the test layout (xml):
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/rate_buttons"
android:layout_width="100dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="0dp"
android:background="#drawable/test_rounded">
</LinearLayout>
</LinearLayout>
here is the test_rounded drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item ><bitmap android:src="#drawable/my_tiled_image" android:tileMode="repeat"/></item>
<item >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dip" android:color="#B1BCBE" />
<corners android:radius="20dip"/>
</shape>
</item>
</layer-list>
The image is overflowing at the corners of the shape/layout border. I get the following
As you can see the corners are overflowed. I heard about modifying the image part (blue pattern) in code but I have issues with that since I do modify it and animate it along the way. SO, if its at all possible to just fill in the layout with that image, please let me know. Thank you all for your help.
You can do this only programatically. But if you want to do this using xml, you have to add the background image in a layout above the rounded corner layout with the help of Relative layout and set margin. This will not give a perfect solution but will solve your problem.
The first is the XML way
Create an xml file in your respective drawable folder for the round border.
I have namedd it as popupborder.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" />
<corners android:radius="15dip" />
</shape>
The main layout
<?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:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/popupborder" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" >
<LinearLayout
android:id="#+id/property_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner view with Background and margin"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
i recommend you to use an image loader library such as universal image loader, picasso etc. Here is an example code for it:
String url = "file://" + your_file_path
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, ivPicture, options);
You have to add
.displayer(new RoundedBitmapDisplayer(px))
to your Universal Image Loader display options.
Also there is another library (i think this is what you are looking for) https://github.com/vinc3m1/RoundedImageView
You can check this. But in second way you can get "Out of Memory Exception" cause of loading big sized image.
I hope this'll help you.
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 just want to make an ImageView as my headbar ... it should just fill out his parent linear-layout.. apparently it does not.
is there any way to tell the scaleType just to fit its parent-layout? (best solution in XML) i saw the was a programming-solution which setting the scaleType to "fill" ... but there seems to be no matching xml-attribute, or didn't i see it?
<?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="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="#+id/linearlayoutmain">
<ImageView
android:layout_marginTop="0dp"
android:src="#drawable/headbar"
android:id="#+id/mainheadbar"
android:scaleType="fitStart"
android:paddingTop="0dp"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="7"
android:layout_below="#id/linearlayoutmain">
</LinearLayout>
</RelativeLayout>
any thoughts/solutions about it? I thought i don't need to mind about the aspect ratio because I 9patched the whole thing.. so it shouldn't mind.
edit: it seems to work better when i use an ImageBUtton instead of an imageview... however, i got problems on lower -size screens... they seem to don't mind my 9patching... lol
You need to use three types of layouts in general
layout-hdpi
layout-mdpi
layout-ldpi
Also you need to use three values layouts in general
values-hdpi
values-mdpi
values-ldpi
Here i explain for LDPI Device Header you have to do for all you device HDPI,MDPI, etc
For example simple xml that has Image as Header : layout-ldpi - Header
<?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="50px"
android:orientation="horizontal" >
<ImageView android:id="#+id/header_main_title" android:layout_marginTop="10dip"
android:layout_width="wrap_content" android:layout_height="50dip"
android:background="#drawable/header_image"
android:layout_centerHorizontal="true" android:layout_marginRight="10dip"
/>
Similarly values-ldpi contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">50px</item>
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground
</item>
</style>
</resources>
Similarly for Styles.xml :
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="WindowTitleBackground" parent="android:WindowTitleBackground">
<item name="android:background">#android:color/transparent</item>
</style>
In your Manifest.xml include this Line :
<application android:icon="#drawable/vmi_icon" android:label="#string/app_name" android:theme="#style/MyTheme" >
Put the Styles.xml and Themes.xml inside of values-ldpi folder
I shown Header for ldpi device similarly you need to create for MDPI and HDPI device
changes should be made in this line of all
if it's HDPI :
<item name="android:windowTitleSize">100px</item>
If it's Mdpi :
<item name="android:windowTitleSize">70px</item>
Similarly for header.xml :
if it's HDPI :
android:layout_height="100px"
If it's Mdpi :
android:layout_height="70px"