I have a listview of countries in non-alphabetic order and started using fastscroll. I would like to display the country-flag when scrolling with fastscroll but it seems like the APIs has the FastScroll class as private so I cannot override it.
Have anyone else implemented a custom fastscroll view?
References:
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:fastScrollEnabled
In your ListView XML definition, add
android:fastScrollEnabled="true"
or in code
listView.setFastScrollEnabled(true);
Create file fastscroll_thumb.xml in the res/drawable folder as follows:
<?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/fastscroll_pressed" />
<item android:drawable="#drawable/fastscroll" />
</selector>
In AndroidManifest.xml, set a custom theme for your application:
<application
android:theme="#style/ApplicationTheme"
...>
Create a values folder in the res folder. Create themes.xml files in res/values as follows:
<resources>
<style name="ApplicationTheme">
<item name="android:fastScrollThumbDrawable">#drawable/fastscroll_thumb</item>
</style>
</resources>
Lastly make sure that fastscroll.png and fastscroll_pressed.png exist in your drawable folder
(optional)
You can also set fast scroll always visible while you are debugging if you like
listView.setFastScrollAlwaysVisible(true);
or in XML
android:fastScrollAlwaysVisible="true"
Related
We have a checkbox inside a listview whose background image needs to change according the content of the listview item, like the gmail email listview.
Our approach right now is to set the checkbox background by using the android:button="#drawable/startcheckbox" option where startcheckbox is an xml in the drawable folder containing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/star_rate_white_54x54" />
<item android:state_checked="true" android:drawable="#drawable/star_rate_yellow_54x54" />
<item android:drawable="#drawable/star_rate_white_54x54" /> <!-- default state -->
</selector>
My question is how can we change the state_checked="false" and state_checked="true" from inside the fragment where the listview is being populated.
Mutate the drawable via Drawable.mutate() then you can change whatever you want.
In general though I do find that if you want to do anything programmatic with a drawable other than scale it, you're better off avoiding xml and using code and custom drawables.
I am using Leanback BrowseFragment and need to be able to set background colour of header item when selected. Any direct method/XML attributes available to do that? I have been looking into BrowseFragment and leanback themes.xml.
Reference:
https://developer.android.com/reference/android/support/v17/leanback/app/BrowseFragment.html
https://android.googlesource.com/platform/frameworks/support/+/master/v17/leanback/res/values/themes.xml
In my case I used custom header items with icons. You can see details in this tutorial. For TextView from my layout I defined xml file in the color directory, something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/accent_color" android:state_selected="true"/>
<item android:color="#color/white"/>
</selector>
and the same xml files (but with android:drawable attributes ) in the direcroty drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_focused"
android:state_selected="true"/>
<item android:drawable="#drawable/icon"/>
</selector>
Then simply set android:textColor="#color/your_new_xml_file" for TextViewand use your drawable files for your header ImageView.
If you need to change View background, not only header icon and text color, set such xml file with defined colors as a View background.
OnSelectLevelChanged() method in my presenter class is empty (without this, like in tutorial above) :
holder.view.setAlpha(mUnselectedAlpha + holder.getSelectLevel() *
(1.0f - mUnselectedAlpha));
I would like to change the expanded and default icon of my Expandable List View.
I did some research how to do this and found this question.
I think I did it just as it is described there.
I have 2 icons, the first is called defaulticon.png and the second one expandedicon.png. Bother are located in the drawable folder (not in drawable-hdpi).
My list view declaration looks like this:
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="#drawable/groupindicator">
</ExpandableListView>
The groupindicator is an xml file also located in the drawable folder and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/defaulticon" android:state_empty="true"/>
<item android:drawable="#drawable/expandedicon" android:state_expanded="true"/>
</selector>
However the icons do not change. The default icon from the list view disappears and instead there is no icon.
Are there any restrictions with the resources (how big the resources can me (both are 160x160), where they have to be located,...).
I am testing the app on my S3 running Android 4.1.2.
Cheers
Change your icon size to 32 X 32, put your icon in drawable-mdpi and then try this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/expandedicon" android:state_expanded="true"/>
<item android:drawable="#drawable/defaulticon"/>
</selector>
First of all, resize your image to 24*24 and then copy it to the drawable-mdpi folder in Android. After that, copy and paste this code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/abc"android:state_empty="true"></item>
<item android:drawable="#drawable/downarrow" ></item>
</selector>
I'm developing an android application that contain many custom buttons. Do I need to make an .xml file for each one or there is a way of putting all of them in one .xml file?
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/father2" ></item>
<item android:drawable="#drawable/father" ></item>
Can I use this code for multiple custom buttons in one xml file?
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/brother1" android:duration="200"/>
<item android:drawable="#drawable/brother2" android:duration="200"/>
<item android:drawable="#drawable/brother3" android:duration="200"/>
<item android:drawable="#drawable/brother4" android:duration="200"/>
<item android:drawable="#drawable/brother5" android:duration="200"/>
And I also have an animated list; can I use multiple animated lists in one xml file?
It depends what part of your button is custom. If only the image or text is custom, you could put them inside of res/styles.xml and then theme the Buttons that you create inside of your XML within other layouts using those themes.
<LinearLayout ... >
<!--stuff-->
<Button style="#style/customButtonStyle1" ... other attributes />
</LinearLayout>
If they have different states (e.g., pressed, selected, unselected) you can use a <selector> resource to assign values (images, text) to the different states. A quick google shows this tutorial for selectors.
I have many custom buttons (ToggleButton) in my app and want to apply different styles for each button. I created a selector for all the buttons and I currently change only the drawable for the button, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected" />
<item
android:drawable="#drawable/button_gradient" />
</selector>
When I try to change the style the same way:
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected"
style="#style/button_checked />
It does not work, I have tried to change the drawable in the style instead (and just stated the style in the selector), I've also tried to create a separate selector but nothing seems to work.
Any ideas?
Right now it's not possible to do this.