2 attributes on 1 view android layout
2 attributes on 1 view android layout
I am trying to set a selectable background on a linear layout with a background color. I know the usual way is android:background="?android:attr/selectableItemBackground" but i already have another code in background. Here is the snippet of the code.
<LinearLayout
android:id="@+id/number"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:background="@color/category_colors"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
The question is how do i set selectable background, there?
2 Answers
2
Try this code snipped on for size:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
the android foreground attribute is relatively newly added XML attribute, but it does not work for API 22 and below! If this is the case then looks like we are going to have to stack attributes in a custom XML file,but don't worry, it is easier than it sounds!
1)In your project view go to res/drawable folder
2)right click the Drawable folder itself and select new>>>drawable resource file
3)Enter a file name my_custom_button.xml(the root doesn't really matter because you will replace it with the below code)
4)Click on the XML text tab (as opposed to the design view) if you aren't already there
5)Select all text and basically replace with the following:
(creating a custom color border is basically the same steps).
Feel free to change and play with the colors or to replace the gradient (mixture of colors) to a custom color of your own!
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/category_colors">
<item android:id="@android:id/ripple">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/colorPrimaryLight"
android:startColor="@color/colorPrimary"
android:type="linear" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
</ripple>
HOPE THIS HELPS!!!
I forgot to say you must include these two attributes android:clickable="true" and android:focusable="true" I updated my answer to include a few more possible solutions
– Paris B. G.
Sep 5 '18 at 11:19
You should background selector XML file like below code and apply your initial colour along with press and pressed colour.
Then you would have to create the res/drawable/bg_selector.xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="@color/color_056DAE" /> <!-- pressed -->
<item android:state_focused="true"
android:color="@color/color_056DAE" /> <!-- focused -->
<item android:color="@color/color_333333" /> <!-- default -->
</selector>
then you have to apply this file to the background of your layout in XML.
android:background="@drawable/bg_selector"
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
did not work...
– Tinovimba Mawoyo
Sep 5 '18 at 7:35