![]() I recently made a major update to my LFCHistory Android app. Most of the work involved replacing the current UI to use SherlockActionBar. I spent a couple of weeks working on the changes and tested them on a Galaxy Nexus, Nexus 7 and Nexus 10. Everything was looking fine so I decided to release the application to Google Play. After several hours and a number of people started updating to the latest version of the app I started to get a lot off error alerts from my ACRA Crash reporting. All of them were from Xperia (SEMC) devices failing to open up the first activity. Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0059 at android.content.res.Resources.getValue(Resources.java:896) at android.content.res.Resources.getDrawable(Resources.java:584) at android.widget.AbsListView.setFastScrollThumb(AbsListView.java:794) at android.widget.AbsListView.setFastScrollEnabled(AbsListView.java:740) at android.widget.AbsListView.<init>(AbsListView.java:670) at android.widget.ListView.<init>(ListView.java:167) at android.widget.ListView.<init>(ListView.java:163) It turns out that the resource ID used for the ListView scroller does not exist within the Xperia rom, which means as soon as the LisView tries to load the exception is thrown and the app fails. At first I fixed this by setting: android:fastScrollEnabled=”false” <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fadingEdge="vertical" android:fastScrollEnabled="false" android:scrollbars="vertical" /> This fixed the issue but then meant that all the other users without Xperia devices were now missing out on functionality (The app contains some large lists which can take a long time to move through without fast scroll being enabled). To fix this I decided to do a check in the code to see if the device being used was an Xperia, if it was not then the fast scroll can be enabled. e.g. if (!android.os.Build.BRAND.equalsIgnoreCase("SEMC")) { listView.setFastScrollEnabled(true); } This means that for everyone apart from Xperia users the fast scroll will now work. Note, the fast scrolling used to work on Xperia devices before I made the changes to work with ActionBarSherlock.
0 Comments
When working on my own app I had a lot of trouble trying to find a complete working solution on how to change the highlight colour which is used when a list view item is selected or pressed. There are lots of examples online but I struggled to find one that demonstrated the complete solution. I have managed to get this working with the following. To begin with I have created my own theme which extends the default android one. The benefit of doing this is that I can make changes in one place and they will affect the entire application. Anything that I do not change in the theme will simply fall back to using the default settings. Create the theme in styles.xml - /res/values/styles.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="android:Theme"> <item name="android:listViewStyle">@style/MyListView</item> </style> <style name="MyListView"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> <item name="android:background">@android:color/transparent</item> <item name="android:cacheColorHint">@android:color/transparent</item> <item name="android:listSelector">@drawable/selector</item> </style> <color name="red">#ff0000</color> </resources> The only change which is made to the default theme is the list view style which is defined in the same xml file along with the colour red. The lisView style references a @drawable/selector. This must also be defined in its own xml file. /res/drawable/selector.xml: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When Pressed --> <item android:drawable="@drawable/selector_pressed" android:state_pressed="true"/> <!-- When Selected --> <item android:drawable="@drawable/selector_selected" android:state_selected="true" android:state_focused="false" android:state_pressed="false"/> </selector> The selector specifies two items which are used to determine what is displayed when the listView item is selected and pressed. These must also be set up in their own xml files. /res/drawable/selector_pressed.xml: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/red"/> <size android:width="1dip"/> <size android:height="1dip"/> </shape> </item> </layer-list> <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/red"/> <size android:width="1dip"/> <size android:height="1dip"/> </shape> </item> </layer-list> /res/drawable/selector_pressed.xml: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/red"/> <size android:width="1dip"/> <size android:height="1dip"/> </shape> </item> </layer-list> They define a rectangle which is used to highlight the selected/pressed row. The colour is set to red which was also defined in the styles.xml. The content of the two files are exactly the same which means that when selected or when pressed the item will be highlighted in red. The selector could just reference the same file for the different states but by having them in separate files it allows more control and demonstrates how it would be possible to set different colours when the item is pressed or selected. The final thing to do is update the manifest file to use the custom theme. This can be done on individual activities or on the whole application. e.g. <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme"> Hopefully if you are trying to do the same thing as me you will find this helpful. |
MeI am a Liverpool supporting software developer originally from North Wales, now living and working in Manchester. Archives
May 2019
Categories
All
|