Custom scrollbar Android - android

How I can make this scrollbar:

To change the thumb image you can simply create the following style and apply it to your ScrollView:
<style name="your_style_name">
<item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
<item name="android:scrollbarStyle">outsideOverlay</item>
<item name="android:scrollbars">vertical</item>
<item name="android:fadeScrollbars">false</item>
<item name="android:scrollbarThumbVertical">#drawable/scroller_thumb</item>
</style>
where scroller_thumb is your custom image for the scroller thumb.
also note the following attributes:
"android:fadeScrollbars" which is set to false to make the thumb image stay permanently.
"android:scrollbarStyle" which is set to outsideOverlay to make the thumb image drawn at the "Edge of the view and overlaid" as stated here: android:scrollbarStyle
Now, in order to put the thin line you want under the scroller, simply add an image view containing the line image, to the direct child of the ScrollView (RelativeLayout child as a direct child for the ScrollView will allow you to position the image on the right side of the view - so this is would have been my choice).
and that's it.

Setting android:scrollbarThumbVertical is not the best solution, it will stretch the thumb image according to the list size...
You'd better use android:fastScrollThumbDrawable
Here's an example:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="0dip"
android:scrollbarStyle="outsideInset"
android:fastScrollAlwaysVisible="true"
android:fastScrollEnabled="false"
android:scrollbars="vertical" />
Then on the styles.xml AppTheme you add
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:fastScrollThumbDrawable">#drawable/scroller_style</item>
</style>
and on the res/drawable folder you create the file: scroller_style.xml with the content
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/scroller_active" android:state_pressed="true"/>
<item android:drawable="#drawable/scroller"/>
</selector>
where scroller is your thumb image and scroller_active is your active thumb image (optional)

Related

Listview: Add arrows as dividers

I would like to add arrows as dividers between my listview items. I've gotten pretty far, but my arrow is stretched and there is no option to set the divider width. Here is what I have so far...
In my styles.xml:
<style name="dividedListStyle" parent="#android:style/Widget.ListView">
<item name="android:cacheColorHint">#android:color/transparent</item>
<item name="android:divider">#drawable/baseline_expand_more_black_24</item>
<item name="android:dividerHeight">50dp</item>
</style>
Then in my layout file:
<ListView
android:id="#+id/checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/menu"
android:layout_alignParentTop="true"
style="#style/dividedListStyle">
</ListView>
This is what the divider looks like:
This is what I need:
The ListView divider is stretched to full width of the ListView and your selected height (<item name="android:dividerHeight">50dp</item>). That's why your image is stretched.
To avoid it and keep the size/ratio unchanged, there are such ways:
Use 9-patch drawable. You may use a default Android 9-patch editor to convert your arrow to 9-patch (you still need some basic knowledge about how 9-patch works)
Make your arrow drawable a part of the drawable or a layer-list drawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/baseline_expand_more_black_24">
</bitmap>

How can I change default background color of a selected ListView item in Android?

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>

Android XML styles

I have the following style in my styles.xml that a toggle button is using. However, i want the toggle button to fill_parent but not stretch the image. How can i tell it to center this background image and not stretch it? I'm trying to increase the area the user can click on.
Toggle:
<ToggleButton
android:id="#+id/OtherToggle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/OtherToggle" />
Style:
<style name="OtherToggle">
<item name="android:textOn">""</item>
<item name="android:textOff">""</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:background">#drawable/other_toggle_bg</item>
</style>
I assume you mean that you do want to stretch the image, but you want to keep the aspect ratio unchanged?
If so, try adding this to your style:
<item name="android:scaleType">centerInside</item>
See here for the other possibilities if centerInside does not do what you want.
Maybe you can use layer-list as a background drawable for ToggleButton? Create the following xml file and use it in android:background attribute:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item>
<bitmap android:src="#drawable/other_toggle_bg"
android:gravity="center" />
</item>
</layer-list>

Changing height of buttons

I'm trying to change the height of a couple of buttons in my app.
I have tried setting #android:color/transparent as the android:background and I have also tried setting a layout_height to values like 16dp.
How could I give my buttons a smaller height?
Here is the style xml:
<style name="Theme.PMBAppTheme.TextButton">
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#drawable/text_button_text</item>
<item name="android:background">#ff3300</item>
<item name="android:padding">0dp</item>
<item name="android:height">20sp</item>
</style>
And the layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/register_btn_privacy"
android:text="#string/privacy_policy"
style="#style/Theme.PMBAppTheme.TextButton"
/>
text_button_text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="true" android:color="#color/muted_pink_over" />
<item android:state_focused="false" android:state_pressed="true" android:color="#color/muted_pink_over" />
<item android:color="#color/muted_pink" />
</selector>
set the height of the button to wrap_content:
android:layout_height="wrap_content"
That should set the height of the button to its minimum to display its text.
In Vapor API (a jQuery style framework for Android I just released) there is a method you can use called .size(int,int). This sets the size of your View depending on the type of container in which it sits. So something like:
$.Button(R.id.register_btn_privacy).size(int width, int height);
What's cool is, you can also chain calls to adjust the other properties of your View. For example:
$.Button(R.id.register_btn_privacy)
.size(50,100) // set size
.text("Privacy Policy") // set the text
.bgColor(Color.RED) // set the background color
.color(Color.WHITE); // set the text color
Check it out if you're interested at the Vapor API website, it's basically designed to make Android dev a lot easier and like using jQuery.
Remove this from your style
<item name="android:height">20sp</item>
So you end up with
<style name="Theme.PMBAppTheme.TextButton">
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#drawable/text_button_text</item>
<item name="android:background">#ff3300</item>
<item name="android:padding">0dp</item>
</style>
Button inherits from TextView and according to TextView android:height, this is probably setting the height exactly and causing it to ignore your layout_height.

Disable Android list view 'blue' background

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"
/>

Categories

Resources