Reset background color in a ListView - android-listview

I have a ListView where I'm changing the background color of the selected item using the onListItemClick event:
#Override
public void onListItemClick(final ListView l, final View v, final int position, final long id)
{
v.setBackgroundColor(Color.GRAY);
}
Is it possible to reset all of the rows of the ListView back to the original color before changing the single view's background color? The problem is that each selected item retains their background color and I only want the latest item the user clicked on changed.
UPDATE:
JRaymond's response seems to be the best approach, however, and it's usual when working with Android layouts, I've put the code in and nothing happens. No way to debug it, no error messages, nothing. I absolutely hate dealing with Android layouts, it's the most convoluted, terribly implemented design for dealing with UI.
Anyhow, this is what I have so far:
ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<ListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:listSelector="#drawable/listing_selector"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
/>
</LinearLayout>
listing_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:state_activated="true" android:drawable="#android:color/black" />
<item android:state_activated="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:drawable="#android:color/transparent" />
</selector>
Like I said, nothing happens when I click on items, even the built-in highlighting is gone. I even tried changing all of the color to black, but nothing happens.
UPDATE2:
I'm getting closer. After reading this article, you have to place your selector xml in a folder called color under res, then you set the selector as the background in the layout for each item in the ListView, not on the ListView itself:
<?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="fill_parent"
android:orientation="horizontal"
android:background="#color/listing_selector"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I'm seeing the background color change flash when I touch each item in the ListView however, they are not staying and reverting back to the original background color. This is the selector I'm using:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#color/blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/blue" />
</selector>
color.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="blue">#ff33b5e5</color>
</resources>
What an absolute pain in the ass this is. Whoever came up with this terrible system for dealing with the UI should be fired.
Solution:
Not 100% sure, but this may only work in Android 3.0+ devices. state_activated is what sets the background color.

What you're looking for is a combination of a stateListDrawable (also known as a selector) and choiceMode.
Include in your drawables folder an xml like this one (I'm using other drawables as my backgrounds, but color works just as well):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_pressed="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_selected="true"
android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_selected="true"
android:drawable="#android:color/black" />
<item android:drawable="#android:color/transparent" />
</selector>
and then set your listview's choiceMode to singleChoice
<ListView
...
android:choiceMode="singleChoice"
This lets the OS handle your backgrounds for you.

just a simple solution is in onitemclick just rememeber the clicked position of the item and call notifydatachanged() and in getView() of your custom adapter just before retrun statement a if loop comparing like below:::
if(postion == itemclickedposition)
converterView.setBackgroundColor(Color.GRAY);

Related

Round corners of buttons and colour change for android button

I want to following features in buttons in android button:
Buttons should change colours when clicked
Some specific buttons have features like the background color of button should remain changed until we press another button.
For rounded corners you can use below code :
#drawable/rounderd_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/YOUR_COLOR" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
And For selector Buttons use below #drawable/select_button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/YOUR_COLOR" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
And Change background resource when button is clicked
For circular and for changing color when selected/not-selected button you can use following code snippet
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#color/colorPrimaryDark"/>
</shape>
</item>
</selector>
And you problem will be solved, hope it helps.

Android ImageButton interaction

I wish to create the same activity like in google fit
And I created an activity with android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<RelativeLayout 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:background="#ddd"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="12dp"
android:background="#null"
android:onClick="onPreferencesClick"
android:src="#drawable/ic_more_vert_grey600_24dp" />
But when I click on three dots button it doesn't have any push effect in ui, but onPreferencesClick works fine.
How to create pushable ImageButton?
The easiest way would be to create a custom selector, for example like this (drawable xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#33000000"/>
<item android:drawable="#android:color/transparent"/>
</selector>
and apply it to the background of ImageButton

TabWidget background color on Samsung devices

My TabWidget's background color is light grey on HTC and Nexus devices...however it's blue-ish/dark grey on Samsung devices. Why is that? I'm even using a custom theme for the tabwidget, as created by android-holo-colors.com
Here's the style and the layout for the tab:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="TabMyTheme">
<item name="android:gravity">center_horizontal</item>
<item name="android:paddingLeft">16dip</item>
<item name="android:paddingRight">16dip</item>
<item name="android:background">#drawable/mytheme_tab_indicator_holo</item>
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">1</item>
<item name="android:minWidth">80dip</item>
</style>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="#dimen/tab_host_default_height"
android:orientation="horizontal"
style="#style/TabMyTheme"
android:gravity="center">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:contentDescription="#null" />
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="#style/TabTextMyTheme" />
</LinearLayout>
I believe the problem is the fact that the default look of tabs is different for Samsung, and given that the background images generated by android-color-colors are transparent on top, and have the color just for the bottom part (ie: the thin or thick colored line at the bottom of each tab), on the top they just use the system default color.
I changed each and every image generated by the website, replacing the transparent color with my grey, and now it looks fine everywhere.

TextView blocking ListView onItemClickListener

I have simple list view with list items defined by following 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:background="#drawable/list_item_selector" >
<TextView
android:id="#+id/menuItemTextView"
android:clickable="false"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00000000"
android:padding="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/blue"
android:textIsSelectable="true" />
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="#drawable/arrow" />
> </RelativeLayout>
When I click TextView the listView.onItemClickListener is not called. The problem occurs on Android 4. Android 2.x is OK. Is there any trick to pass an event from TextView to its ListView without defining onClickListner on the TextView?
In your main activity just make you listview as focusable true. This worked for me. If you are using a custom list view the focus is passed to its childs and not the list so to get the focus back to listview use
listView.focusable(true);
and the other things as textview just set their focus as false.
It was the android:textIsSelectable="true" attribute. Don't follow lint hints blindly.
android:inputType="textMultiLine" can also cause this problem. If you've included that attribute in your TextView xml, try removing it.

Aligning fonts in tabwidget

Myself designing an app which uses GPS and Maps. I have created the UI as tabs. But i couldn't able to keep the font below the image.
Code in my Mainactivity
TabSpec gpsspec = tabHost.newTabSpec("GPS");
gpsspec.setIndicator("GPS", getResources().getDrawable(R.drawable.gps));
Intent gpsIntent = new Intent(this, GpsActivity.class);
gpsspec.setContent(gpsIntent);
tabHost.addTab(gpsspec);
XML file in my drawable folder for tabs
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/gps_icon"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/gps_icon" />
</selector>
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
Kindly help in aligning the font.
You need to provide
Small size of icons for small density devices in drawable-ldpi folder
Medium size of icons for medium density devices in drawable-mdpi folder
Large size icons for large density devices in drawable-hdpi folder
Here is documentation guide for sizes of the drawable and layouts.
if it doesn't solve your problem, you can consider provding your own customized tab indicator, which I've used in my post here.