Android ListView style issue with background while scroll - android

I need to style ListView and can't figure out why It turns to black background when scroll.. I have ListView inside LinearLayout and that layout has Background set to image.
This is what I have for ListView:
<style name="MyListView" parent="#android:style/Widget.ListView">
<item name="android:listSelector">#drawable/selector_list_item</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#drawable/shape_list_item_normal" />
<item android:state_pressed="true"
android:drawable="#drawable/shape_list_item_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/shape_list_item_normal" />
</selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#80ffffff" android:endColor="#80eff3ef" android:angle="45" />
<padding android:left="5dip" android:right="5dip" android:top="7dip" android:bottom="7dip"/>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#80808080" android:endColor="#80eff3ef" android:angle="45" />
<padding android:left="5dip" android:right="5dip" android:top="7dip" android:bottom="7dip"/>
</shape>
When I apply MyListView style I don't see much happening. When I click on item - yes, I see gradient. When ListView stable - I see background as it should be from parent layout. When I scroll - I see black background, it's erasing parent's background. And I would be fine with that if I knew how to set it with something else..

Use the following property in the xml of listView:
android:cacheColorHint="#android:color/transparent"

Try this line of code in the xml of your listview:
android:listSelector="#android:color/transparent"

If you require transparent ListView in a PreferenceActivity, try using a theme with
<resources>
<style name="MyPreferencesTheme">
<item name="android:scrollingCache">false</item>
</style>
</resources>
And put the theme in your AndroidManifest.xml
<activity
android:name="YourActivity"
android:theme="#style/MyPreferencesTheme">
</activity>

Related

How to add border to a spinner without affecting its popup menu

I'm trying to add border to my spinners.
This is what I've done so far:
In my styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- ... -->
<item name="android:spinnerItemStyle">#style/spinnerItemStyle</item>
<!-- ... -->
</style>
<style name="spinnerItemStyle">
<item name="android:padding">#dimen/form_horizontal_padding_normal</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Subhead</item>
<item name="android:background">#drawable/spinner_border</item>
<item name="android:textColor">#color/secondary_text</item>
</style>
spinner_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="6dp"/>
<stroke
android:width="1dp"
android:color="#color/detail_accent_pane_background"/>
</shape>
Without the changes this is how the popup menu looks:
After the changes the spinners have the border
But the popup menu also has the the border, which is not what I want.
How can I add border to spinners without affecting their popup menus?
Thanks.
This might be helpful to you
backgroud_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="0dp" android:color="#80FFFFFF" />
<solid android:color="#80FFFFFF"/>
</shape>
And apply this,
android:popupBackground="#drawable/backgroud_spinner"
It will make the popup fully transparent, But I don't know whether it suits you or not, When I used your drawables, it didn't show the image as you've shown in image.
Add
android:popupBackground="#android:color/transparent"
to your <Spinner/> view in xml

Android drawable: state_selected working, superseding state_pressed?

Newbie in Android.
I have the following defined in res/drawable/ for a button in a menu that's defined in style.xml
<style name="menu_icon">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">#dimen/menu_item_height</item>
<item name="android:background">#drawable/menu_item_bg_sel</item>
</style>
Now, menu_item_bg_sel in drawable has two different color gradients for 2 states that I am interested in- pressed and selected.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item>
<shape>
<gradient android:angle="180"
android:endColor="#color/background_menu_gray_selected2"
android:centerColor="#color/background_menu_gray_selected1"
android:startColor="#color/background_menu_gray_selected" />
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list>
<item>
<shape>
<gradient android:angle="180" android:endColor="#color/background_menu_home2"
android:centerColor="#color/background_menu_home1"
android:startColor="#color/background_menu_home" />
</shape>
</item>
</layer-list>
</item>
<item android:drawable="#color/transparent"/>
However, when I press the button (that transient state) the button still creates the gradient taking colors from the selected_state only.
What am I doing wrong? Any ideas?
state_selected is commonly used when navigating using d-pads or maybe a pen like what Samsung Notes so I suggest not to use state_selected since when you press automatically it will be selected or gain focus although selected and focus are kind or different. I suggest to use state_pressed, state_focused and state_hovered instead.
For more info click here
You can use this code. You can copy paste this code into one drawable file. You can assign different drawable files or define shapes in this file itself for the different states.I have assigned a drawable file for the focused state as an example.
simple_button_states.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:drawable="#drawable/rounded_edittext_focused"
/>
<!--pressed -->
<item
android:state_pressed="true" />
<!--selected -->
<item
android:state_selected="true" />
<!-- focused -->
<item
android:drawable="#drawable/rounded_edittext_unfocused"/>
</selector>
round_edittext_focused.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#FFFFFF" />
<stroke
android:width="2dp"
android:color="#color/CornflowerBlue" />
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
/>
</shape>
Finally assign the background of the actual button as the drawable file that you have declared above.
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/simple_button_states"
/>
Hope this helps

Android: make list item with background clickable

I have a list where I have defined my own list view items with a custom layout. This layout has a background with a custom drawable.
My custom layout for the ListView item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
...
</RelativeLayout>
My custom drawable item.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- background: shadow -->
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemShadowColor" />
</shape>
</item>
<!-- foreground: surface -->
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemBackgroundColor" />
</shape>
</item>
</layer-list>
Now this item is not clickable anymore.
Can you explain me why, and what I have to do to have the same behavior (selector with the blue background) like a button click?
Define a selector with its pressed and default states in res/drawable folder(one of the state will be your #drawable/item). Set it as the bg of your list row layout.
See similar question and answer :Selector on background color of TextView
Edit:
Best way to understand and apply something like google did is, to look into SDK and do similar things to that. For instance look at the btn_default_holo_dark drawable. It is a selector with states and yes it is a xml.
This is a selector taken from sdk (sdk\platforms\android-18\data\res\drawable\btn_default_holo_dark.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
These are the images taken from sdk (sdk\platforms\android-18\data\res\drawable-xhdpi):
When you apply this drawable/selector (#drawable/btn_default_holo_dark) to any view, you are going to have its states. I hope this sample makes my answer more clear.

Gridview: custom selector keeps blank

I want to use a list selector for my gridview. I add it in xml via the android:listSelector attribute.
My selector looks like that:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/selected_item_highlight" />
<item android:state_focused="true" android:drawable="#drawable/selected_item_highlight" />
<item android:drawable="#android:color/transparent" />
</selector>
where the selected_item_highlight looks like that:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="3dp" />
<solid
android:background="#color/ListHighlighting" />
</shape>
But the grid keeps blank. When I change the drawable attribute in the selector to drawable="#color/ListHighlighting" the whole grid - every item - get's highlighted in the color.
What's wrong?

Selector color on LinearLayout

I'm trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.
i followed the instructions on this post, the answer talking about shapes.
Now i have 3 xml on drawables folders:
normal.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
</shape>
pressed.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
and finally, bg.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:state_focused="true" android:drawable="#drawable/pressed" />
<item android:state_selected="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/normal" />
</selector>
I am accessing this in the following way:
Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
view.setBackgroundDrawable(d);
The "normal" state its fine, with the color set at "normal.xml", but no way with the other ones, I press my view and nothing happens, it's not changing color in any way...
I can't see what i'm doing wrong...
Thank you
Your view needs to be clickable in order to get the state pressed when you click on it.
Use :
view.setClickable(true);
or in the layout xml :
android:clickable="true"

Categories

Resources