Listview: Add arrows as dividers - android

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>

Related

Android app - basic icon button without text

How do I make a basic icon button without text or background? For example, just a simple vector image from #drawables that, when clicked, shows that circular response thing around it. I don't want the icon to have any background color to it. Just an icon that can be clicked, that's literally it.
I can only figure out how to do it by creating a menu and setting the icon as an item with app:showAsAction="always". It seems like there must be a better way to do this.
Edit: Here's an example of what I want to achieve. It's very basic. Just a clickable icon with responsive feedback when touched. https://material-ui.com/components/buttons/#icon-buttons
It's exactly the same as creating the following. I just thought there was likely an easier way to do this without having to create and load a menu just for one single icon:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mycompany.myapp.MainActivity">
<item
android:id="#+id/action_contacts"
android:icon="#drawable/ic_contacts_24"
android:title="#string/action_contacts"
app:iconTint="#android:color/white"
app:showAsAction="always" />
</menu>
First create a vector drawable and then add to the ImageView like
this:
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
app:srcCompat="#drawable/ic_delete" />
You can use a MaterialButton, ImageButton and even an ImageView for this. Here's an example of how you'd do it with an ImageView...
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:src="#drawable/your_icon" />
All you need to do is set the selectableItemBackground, this will use your theme's primary color (or maybe the secondary color, I can't remember).
Now if you want a different color for ripple effect, for example a grey ripple, then you can create the drawable yourself...
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#dddddd">
<item android:id="#android:id/mask">
<color android:color="#dddddd" />
</item>
</ripple>
and set the view background below..
android:background="?attr/selectableItemBackground"
it'll work with any view provided the view is clickable
Yes, you can use ImageView if you like, but if you want to generate the complete image (including frame/borders) yourself you don't have to go that far. You could practically use plain View (except that you'd have to supply android:layout_width/height explicitly). Just use Button and have the android:background specify a selector which will select between images.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/clear1" android:state_pressed="true"/>
<item android:drawable="#drawable/clear0"/>
</selector>

Listview dividers color with HoloEveryWhere

I'm using HoloEveryWhere to get support of the Holo themes on android 2.x and I want to change the default color of my ListView dividers.
I did this :
<ListView
android:id="#+id/listRecherche"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:layout_height="wrap_content" >
</ListView>
It works well on android 4.x but in 2.x, what I get is no more dividers and instead a #e5e5e5 background on the whole ListView.
I've thought about a height problem since I know that changing the dividers color resets the dividers height. This is why I've set heights at the end... but no effect.
Use a drawable instead of an RGB color Just put a file named divider.xml in res/drawable/ so you can access it as R.drawable.divider; if you can access it that way, then you can use android:divider="#drawable/divider" in the XML for ListView.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffcdcdcd"
android:endColor="#ffcdcdcd"
android:angle="270.0" />
And in styles.xml for listview item
<item name="android:divider">#drawable/divider</item>
<item name="android:dividerHeight">1px</item>

Custom scrollbar 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)

How to make background stretch in Android layout

So what I am trying to do is display background for list row element. I've created row layout and applied style :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/product1" >
I've set style as:
<style name="product1">
<item name="android:background">#drawable/product1</item>
</style>
and product1.xml as :
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/prod1"
android:antialias="true"
android:dither="true"
android:filter="false"
android:gravity="right"
android:scaleType="fitXY" />
where prod1.9.png is a nine patch set to be scalable top and left (so I want to have my image in right lower corner)
But the problem I am facing now is that icon is displayed in the corner while 9patch is not scaled left and up.
If instead of using style I put android:background=#drawable/prod1 then it scales but every row get ridiculously big. Any ideas?
I haven't verified this with your code, but the first thing that comes to mind is (assuming using 'android:background' instead of the style), instead of this on the row layout:
android:layout_height="fill_parent"
set a fixed height:
android:layout_height="30dp"
It's not as flexible, but it might suit your needs.

seekbar customization: progress and secondaryProgress property not stretch pictures default

I customize my seekbar like below:
<SeekBar android:id="#+id/progress_seekbar" android:background="#drawable/play_progress_bg"
android:layout_centerHorizontal="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginBottom="-9dp"
android:layout_above="#id/playwidget" android:progressDrawable="#drawable/progress_seekbar_style"
android:thumb="#drawable/thumb_progress" />
in my progress_seekbar_style.xml file, I specified it like this:
<?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="#drawable/play_progress_01" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/play_progress_02" />
<item android:id="#android:id/progress" android:drawable="#drawable/play_progress_03"/>
</layer-list>
I want to use this seekbar customization for different screen size; but when it display on the large screen like 600x1024, the seekbar will repeat the picture which I set in "#android:id/background,secondaryProgress/progress " , not stretch my pictures. and I need it automatically stretch my picture to fill parent width. that is what I want, but it just repeat the picture.
can you help? thanks.
Try using nine-patch images. Nine-patch is stretched if you use it for background (even for progress or seek bar).
For testing just rename your image from img.png to img.9.png

Categories

Resources