When I click on the list item in Android, I always get a blue background. Event if I use a custom layout, when I click, maybe 2-5dp space around my layout is blue. Also, the text views get darker. How can I disable any changes in view when clicking on the list item?
I know its kind of late, but one can also use the setSelector method of the listview and set it to android.R.color.transparent. You can also use android:listSelector in the layout file to achieve the same result.
Create custom theme for your application in /res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- application theme -->
<style name="CustomTheme" parent="android:style/Theme.Light">
<!-- widget styles -->
<item name="android:listViewStyle">#style/ListView</item>
</style>
<!-- list view -->
<style name="ListView" parent="#android:style/Widget.ListView">
<item name="android:background">#ffffff</item>
<item name="android:cacheColorHint">#ffffff</item>
<item name="android:divider">#cccccc</item>
<item name="android:dividerHeight">1px</item>
<item name="android:listSelector">#drawable/list_selector_background</item>
<item name="android:fadingEdge">none</item>
</style>
</resources>
In your AndroidManifest.xml specify the theme:
<application
...
android:theme="#style/CustomTheme" >
You can simply set the android drawSelectorOnTop attribute to false on your ListView and there will not be any background when you click on an item.
Eg:
<ListView android:layout_width="fill_parent" android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
Use this
<ListView
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
/>
For more details you can visit here
http://developer.android.com/reference/android/widget/ListView.html
I did it Like this:
<ListView
android:listSelector="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Related
First of all, this is the code I use for ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:contentDescription="#string/top_layer"
android:id="#+id/top_layer_Q1"
android:layout_width="fill_parent"
android:src="#drawable/top_bar"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/Quotelist1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
In order to test the code, I created a temporary project in Studio, and the Output I got was Black text in white background. And when I added the same code to the project I am working, I get the output with a Black Background and white text.
Though I did not change any code, what might be causing this? What is the best fix for this?
Thanks,
EDIT1
Here is the styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Try this:
Add this to your style.xml
<style name="ListStyle" parent="#android:style/Widget.ListView">
<item name="android:textColor">#FF000000</item>
<item name="android:background">#FFFFFFFF</item>
</style>
Then set it as the style for your listView
style="#style/ListStyle"
sometring strange is happening in my app, when I try to change color of ActionBar by
<style name="CustomAppTheme" parent="Theme.AppCompat.Light">
<item name="android:background">#ff00</item>
<item name="android:textColor">#DCDCDC</item>
</style>
is also changes color of my ListView and in other activity changes whole content below. Why is that? How to fix this?
ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/listviewbackground"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:longClickable="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
The documentation is pretty straight forward when it comes to overriding the default color & styles of the action bar. I'm assuming that you're probably making changes to your application's resource styles file at res/values/styles.xml. I'm also assuming that in your AndroidManifest.xml, you have the following property applied to the application tag:
android:theme="#style/AppTheme"
Again, from the documentation:
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
So, when you change that styles.xml file, you're inadvertently changing the background color for all elements in your app.
Follow the instructions from the link 1, and you'll be good. Specifically,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
</style>
</resources>
I want to change the default color (blue) of a selected item in a Navigation Drawer in Android. I have gotten it to work in past projects, but I cannot in the project I am currently working on.
This is the app theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:activatedBackgroundIndicator">#drawable/activated_background</item>
<item name="android:buttonStyle">#style/AppTheme.Button</item>
</style>
This is the activated_background drawable.
<?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/list_selected"/>
<item android:state_selected="true" android:drawable="#drawable/list_selected"/>
<item android:state_pressed="true" android:drawable="#drawable/list_selected"/>
</selector>
Finally, this is the navigation drawer fragment.
<ListView 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:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
tools:context=".NavigationDrawerFragment" />
Like I said, this has worked for me before. The only difference between projects that I can think of is that before, my app theme was inherited from "Theme.AppCompat" and this project's theme is inherited from "android:Theme.Holo.Light.DarkActionBar". Also, I am using v4 support fragments in my new project, while as in the other project I was just using the standard fragment.
Any advice would be greatly appreciated. Thanks.
Edit:
Seems like the question is how to keep an item in a ListView selected, not actually how to set the resulting background of a selected item.
See this great answer for how change the background of a ListView item once it is selected:
Android - Keep ListView's item highlighted once one has been clicked
The basic idea is that when the item is tapped, you change it's background (or whatever else). The tricky part is you must have your List adapter remember the row that is selected. Otherwise when/if you scroll the list/navigation drawer, you'll lose the selection.
Original Answer:
You need to set a custom ListView style in AppTheme in which you set the android:listSelector item to #drawable/activated_background.
If you don't want that style for your entire app, just specify that style for the ListView in your application drawer layout, or even more simply just set the android:listSelector property of that ListView and don't even bother defining a style.
I figured it out. Instead of using my theme to try and apply the style, I just created my own xml layout for the list items instead of using the default and set the selector from there.
Here is what the list item xml looks like.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#drawable/list_txtcolor"
android:gravity="center_vertical"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="#drawable/activated_background"
android:minHeight="?android:attr/listPreferredItemHeightSmall">
</TextView>
As you can see, I just set the item background to my selector.
android:background="#drawable/activated_background"
And again, 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/list_selected"/>
<item android:state_selected="true" android:drawable="#drawable/list_selected"/>
<item android:state_pressed="true" android:drawable="#drawable/list_selected"/>
</selector>
My goal is to set a modal view in my Android application. I would like to open one view whose background would be transparent so the user can see the view behind.
I've heard about using transparent activity but this might freeze the activity behind, ain't it ?
I would like some kind of reusable stuff since this view will call in more than one activity.
Thanks
You can make an Activities background transparent using this style:
<style name="TransparentActivity" parent="android:Theme.Black.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowFullscreen">true</item>
</style>
Declared in the manifest like so:
<activity
android:label="#string/app_name"
android:name=".activity.DialogActivity"
android:theme="#style/TransparentActivity" >
I then use this as the layout for my dialogue:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/containerPageContainer">
<FrameLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#drawable/panel_picture_frame_bg_focus_blue"
android:layout_gravity="center"
android:id="#+id/dialog"/>
</FrameLayout>
You can either inflate other layouts and add it to #+id/dialog or use fragments (depending on how brave you are).
Hope that helped!
Perhaps you could use a Dialog? You can use a custom content view to display whatever you want, and use setCancelable(false).
I using listviews and expandedviews that has dividers and I can set them but on spinner its looks like it is no divider between items.
Someone that has a idea of how to fix this?
This worked for me:
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#d1d1d1</item>
<item name="android:dividerHeight">0.5dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
The advantage of using this is that it doesn't remove the ripple effect on hover.
I managed to find a more proper solution for this issue (without including the divider in the single item layout).
What you have to do is define in your activity's theme
<item name="android:dropDownListViewStyle">#style/App.Style.Spinner</item>
and then create the proper style with
<style name="App.Style.Spinner" parent="#style/Widget.Sherlock.Light.ListView.DropDown">
<item name="android:dividerHeight">10dip</item>
<item name="android:divider">#drawable/mydivider</item>
</style>
Based on #Talihawk answer, I made it work for specific spinner only. Instead of setting your activity theme, set the theme directly for the spinner view:
<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:divider">#123456</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="MatchSpinnerTheme" parent="AppTheme">
<item name="android:dropDownListViewStyle">#style/MatchSpinnerStyle</item>
</style>
and
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MatchSpinnerTheme"/>
Sorry, I am answering years after the question was asked but the solution is very simple, you just have to do a simple thing.
Go to your style.xml file and add this item in your active theme
<item name="android:dropDownListViewStyle">#style/MySpinner</item>
after this add another theme with the name MySpinner and same parent of your active theme
<style name="MySpinner" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dividerHeight">2dp</item>
<item name="android:divider">#000</item>
</style>
this will separate your single item and will not show the separator while we hover on a single item
but be sure while doing this, we are applying this theme on all the spinner in the activity. Now after every spinner is will work on this same spinner theme.
For people with same problem i after almost gived up i got an idea of how to get the divider.
I added the divider line at the bottom of my custom layout for each item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" style="#style/ListItem2">
<TextView android:id="#+id/Text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
style="#style/SpinnerView_Text" android:paddingLeft="10dip" />
<ImageView android:id="#+id/icon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/arrowright"
android:layout_alignParentRight="true" android:layout_centerInParent="true"
android:layout_marginRight="20dip" />
</RelativeLayout>
<ImageView android:id="#+id/Divider1" android:layout_width="fill_parent"
android:layout_height="1dip" style="#style/Divider"></ImageView>
None of the other solutions worked for me, so I used a drawable as the background of the Spinner items to produce the desired effect.
I have created a new Drawable dropdown_divider.xml and a custom SpinnerAdapter class where I adapted the getDropDownView() method to set the background to the Spinner items.
The android:bottom="-56dp" in the drawable is what centers the line perfectly between two items for me, but that depends on the exact margins and paddings that you've applied in your layout.
dropdown_divider.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="16dp"
android:right="16dp"
android:bottom="-56dp">
<shape android:shape="line" >
<stroke
android:width=".2dp"
android:color="#FF666666" />
</shape>
</item>
</layer-list>
SpinnerAdapter:
#Override
public View getDropDownView(int position, View convertView,
#NonNull ViewGroup parent) {
TextView text = (TextView) super.getDropDownView(position, convertView, parent);
text.setBackground(context.getDrawable(R.drawable.dropdown_divider));
label.setTextColor(Color.BLACK);
label.setText(lists.get(position).getTitle());
return text;
}
The result looks like this: