I have a ListView and its items have an arrow aligned to the end, like this:
The arrow is set in the background, which is a custom drawable:
background_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
<item
android:width="24dp"
android:height="24dp"
android:gravity="right|end|center_vertical"
android:right="#dimen/preferred_margin">
<bitmap android:src="#drawable/ic_chevron_right_black_24dp" />
</item>
</layer-list>
With API > 22 this is working great but 22 and below the background looks like this:
This is how I am setting the background:
listitem_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/listitem_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_listitem"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="#dimen/preferred_margin">
...
</RelativeLayout>
Is there any way to fix this without adding the arrow as an ImageView?
you can change you background_listitem.xml file like this i hope this is help you.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white"/>
<item>
<bitmap android:src="#drawable/ic_chevron_right_black_24dp"
android:gravity="right|end|center_vertical" />
</item>
</layer-list>
I tried your example but by making small change and it worked.
Instead of applying gravity to the <item> tag, I applied the tag to bitmap, and the result is clear to me.
My xml code is as follows:
<item>
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
<item >
<bitmap
android:src="#drawable/ic_launcher" android:gravity="center|right"/>
</item>
And in my layout I applied them to parent RelativeLayout as follows:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/my_background"
android:layout_height="match_parent">
Here are my results:
Android API 23
Android API 19
Without gravity attribute in Android API 19
Hope this helps you
Related
I have created custom drawable for AppCompatButton and it is working perfectly fine for API level 23 and above.
Here is custom drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="#android:integer/config_shortAnimTime" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#drawable/background_radio_checked" android:state_checked="true" />
<item android:drawable="#drawable/background_radio_unchecked" android:state_checked="false" />
</selector>
background_radio_checked -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-8dp"
android:right="-8dp"
android:top="-8dp">
<shape>
<solid android:color="#FFF1EEFF" />
<stroke
android:width="1dp"
android:color="#color/color_ask_question" />
</shape>
</item>
<item
android:bottom="#dimen/screen_margin_half"
android:drawable="#drawable/ic_add_filled_24dp"
android:gravity="start|center_vertical"
android:left="#dimen/screen_margin_half_plus_four"
android:right="#dimen/screen_margin_half"
android:top="#dimen/screen_margin_half" />
</layer-list>
background_radio_unchecked -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/color_employer_divider" />
<solid android:color="#F2F2F2" />
<corners android:topLeftRadius="#dimen/screen_margin_half_half_half" />
</shape>
</item>
<item
android:bottom="#dimen/screen_margin_half"
android:drawable="#drawable/ic_add_empty_24dp"
android:gravity="start|center_vertical"
android:left="#dimen/screen_margin_half_plus_four"
android:right="#dimen/screen_margin_half"
android:top="#dimen/screen_margin_half" />
</layer-list>
The drawables used are SVGs and have height width of 24dp.
How it looks on API 23+
But the same thing looks horrible on API level 21.
The image used in drawable stretches out to full width.
Tried removing animation but still the same results.
Not sure what wrong I'm doing or missing out.
Seems I'm using some property which is not available only from API 23 and above but not sure which one.
I'll answer by self, hopefully It'll help someone.
I was setting this drawable as background to my radiobutton and passing "#null" in button property.
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_radio_button_backgorund"
android:button="#null"
android:checked="true" />
This wasn't working, so I created two separate custom drawables for button and background.
For custom drawable for background I just used shape with border, background color, radius etc but NO ICON. And for custom drawable for Button, I used icon.
Assigning these two custom drawables in background and button makes things work in both, lollipop devices and above.
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_radio_background"
android:button="#drawable/custom_radio_button"
android:checked="true" />
-----------Custom drawable for background-----------
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="#android:integer/config_shortAnimTime" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#drawable/background_radio_checked_background" android:state_checked="true" />
<item android:drawable="#drawable/background_radio_unchecked_background" android:state_checked="false" />
</selector>
background_radio_checked_background -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-8dp"
android:right="-8dp"
android:top="-8dp">
<shape>
<solid android:color="#EBFFF7" />
<stroke
android:width="1dp"
android:color="#118F5D" />
</shape>
</item>
</layer-list>
background_radio_unchecked_background -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/color_employer_divider" />
<solid android:color="#F2F2F2" />
<corners android:topRightRadius="#dimen/screen_margin_half_half_half" />
</shape>
</item>
</layer-list>
-----------Custom drawable for button-----------
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="#android:integer/config_shortAnimTime" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#drawable/background_radio_checked_button" android:state_checked="true" />
<item android:drawable="#drawable/background_radio_unchecked_button" android:state_checked="false" />
</selector>
background_radio_checked_button -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="#dimen/screen_margin_half"
android:drawable="#drawable/ic_poll_filled_24dp"
android:gravity="start|center_vertical"
android:left="#dimen/screen_margin_half_plus_four"
android:right="#dimen/screen_margin_half"
android:top="#dimen/screen_margin_half" />
</layer-list>
background_radio_unchecked_button -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="#dimen/screen_margin_half"
android:drawable="#drawable/ic_poll_empty_24dp"
android:gravity="start|center_vertical"
android:left="#dimen/screen_margin_half_plus_four"
android:right="#dimen/screen_margin_half"
android:top="#dimen/screen_margin_half" />
</layer-list>
I'm still not sure why creating a single drawable which has both icon and border/background color, does not work with lollipop devices.
Suggestions, corrections on this solution are welcome.
I am new to Android programming. I want to add ripple effect to cardview. Actually I found the solution, however, after touching cardview element, it seems that the effect doesn't cover entire cardview. I mean that the effect fades out suddenly and doesn't reach the edges of the cardview.
The result which I'm getting is as follows:
While I want something like this one posted in this blog post.
This is the approach I have tried:
cardview_item.xml
<android.support.v7.widget.CardView
android:layout_width="match_parent"
style="#style/MyCardViewStyle"
android:layout_height="280dp"
android:id="#+id/appletCard"
app:cardBackgroundColor="#color/cardDefaultBg"
android:clickable="true"
android:foreground="#drawable/ripple_effect"
>
//Some TextViews here ...
</android.support.v7.widget.CardView>
And this is the ripple_effect.xml in the drawable-v21 package:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white"/>
</ripple>
If it is important, I use support library.
You should apply mask to your ripple. Try this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="#dimen/card_corner_radius" />
</shape>
</item>
</ripple>
You can use android default ripple effect with "?attr/selectableItemBackground"
<android.support.v7.widget.CardView
android:id="#+id/appletCard"
android:layout_width="match_parent"
android:layout_height="280dp"
style="#style/MyCardViewStyle"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="#color/cardDefaultBg">
</android.support.v7.widget.CardView>
Or create custom
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item android:state_pressed="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/backColor" />
</shape>
</item>
</selector>
If you want that effect reach the edges, use this library that provides you OnRippleCompleteListener, so you can wait until it finish effect and reach the edges
After checking the similar applications which are using ripple effect for their ui elements, I found out that the effect has changed over different versions of Android. The case I'm looking for is used in Lollipop Android devices. So as I ran the emulator on API 25 (Nogut), I didn't get the desired result.
Thank you all for ur responses.
I'm a new to Android Development. While working on Android Studio 2.2, I tried to create a circular Button. As read from here, I tried to create a new Drawable Resource File. But I was unable to change Root Element to Shape. There was no such options. Could anyone help?
There are two solutions.
Floating Action Button: https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
else:
You create a oval shape in xml.
and put that shape as background on button and make sure button have equal layout_height and layout_width.
Example:
oval_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="oval">
<solid android:color="#color/colorAccent"/>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/oval_shape"/>
</FrameLayout>
No option is needed here. Just use <shape>.
Check this example:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorPrimary" />
<corners android:radius="#dimen/btn_radius" />
</shape>
Use xml drawable like this:
Save the following contents as round_button.xml in drawable folder
<?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="#fa09ad"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#c20586"/>
</shape>
</item>
And set it as background of Button in xml like this:
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />
You are probably looking for the FloatingActionButton
It is available in the support-library.
The documation of the class.
On devices running Android API <= 22, my custom SeekBar looks as follows:
On devices running Android API >= 23, my custom SeekBar looks as follows:
Why does my SeekBar drawable look incorrect on devices API >= 23? Here is my XML
seekbar_progress.xml
<?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/seekbar_progress_bg" />
<item android:id="#android:id/progress">
<scale
android:drawable="#drawable/seekbar_progress_fill"
android:scaleWidth="100%" />
</item>
</layer-list>
seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size
android:width="#dimen/seekbar_thumb"
android:height="#dimen/seekbar_thumb" />
<solid android:color="#color/white" />
<stroke
android:width="#dimen/seekbar_thumb_border"
android:color="#color/gray" />
</shape>
</item>
</layer-list>
layout file
<SeekBar
android:thumb="#drawable/seekbar_thumb"
android:progressDrawable="#drawable/seekbar_progress"
android:splitTrack="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:progress="50" />
For reasons unknown, by playing with minHeight and maxHeight I was able to get the desired behavior
I need to set size for item in layer-list. For using as windowBackground in launch activity. I wrote this XML file and Android Studio displays it correctly. But in application the logo always full screen. How to make it?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:endColor="#000004"
android:gradientRadius="1000"
android:startColor="#1f3371"
android:type="radial" >
</gradient>
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/texture_5"
android:tileMode="repeat" />
</item>
<item android:drawable="#drawable/logo"
android:height="100dp"
android:width="77dp"
android:gravity="center"
>
</item>
</layer-list>
I found solution, but I expected better way.
Here is final code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:endColor="#000004"
android:gradientRadius="1000"
android:startColor="#1f3371"
android:type="radial" >
</gradient>
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/texture_5"
android:tileMode="repeat" />
</item>
<item android:drawable="#drawable/logo"
android:bottom="214dp"
android:top="214dp"
android:left="100dp"
android:right="100dp"
android:gravity="center"
android:tileMode="repeat"
>
</item>
</layer-list>
At least it works at xxhdpi and xhdpi without distortion.
Starting from API 23, it is possible to set width and height right within the "item" tag:
<item android:drawable="#drawable/logo" android:width="..." android:height="..." />
You can do the followin
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/blanco"/>
<item
android:width="200dp"
android:height="200dp"
android:gravity="center">
<bitmap
android:gravity="fill"
android:src="#drawable/your_image"
android:mipMap="true"/>
</item>
</layer-list>
Just change width and height
I had the same problem and found the solution here.
I had to use my vector drawable as the source to a bitmap object:
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_logo_splash"/>
</item>