while using a ListView to show some data to the user, By default the ListView items have a "Clicked" Animation when the user clicks them [as shown in the picture]
Now, i wanted to design the items in the ListView and added android:background="#drawable/border"
to their XML layout definition
when drawable/border.xml is defined to be:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1.5dp" android:color="#000000" />
<solid android:color="#ffe5d226"/>
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="0dp" />
<corners android:radius="4dp"/>
</shape>
After doing this the ListView items changed to the shape and color that I wanted, However the "Clicked" animation was gone [I am Clicking on the "Wednesday" in the picture below]
I've been trying to solve this for a long time but with no success :\
How can I retrieve the "Clicked" Animation while keeping the Item designed?
A solution to my problem was found by creating in the #drawable folder:
1) "NOT Clicked" Background layout to the ListView item named not_clicked_layout.xml
like:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1.5dp" android:color="#000000" />
<solid android:color="#color/green"/>
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="0dp" />
<corners android:radius="4dp"/>
</shape>
2) "Clicked" Background layout to the ListView item, i.e. how it will look like the moment the user pushes it. named clicked_layout.xml
Like:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/gold"/>
<stroke android:width="1.5dp" android:color="#000000" />
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="0dp" />
<corners android:radius="4dp"/>
</shape>
3) The last and most important layout is the one that sticks the two previous steps by creating the last layout named: list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/not_clicked_layout" />
<item android:state_pressed="true"
android:drawable="#drawable/clicked_layout" />
</selector>
The above layout declares the "not_clicked_layout.xml" to be shown when the item is NOT pressed, and "clicked_layout" to be shown when the item is pressed.
4) All that is left to do is to add:
android:background="#drawable/list_selector"
to the layout of your ListView Item and the "Clicked" Animation will arise from the dead :)
Note: this Solution is relevant not only to ListView items, but to any GUI element you would like to create a "Pushed" Animation.
Related
I have a button that is clear with a white border, and when tapped it becomes light grey. My issue is this transition is instant and quite jarring to see. I want to learn how to smoothly flow from the one color to the other.
I have an XML file that has my button properties:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="250px" />
<padding android:left="100px" android:right="100px" />
<stroke android:color="#cecece" android:width="15px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="250px" />
<padding android:left="100px" android:right="100px" />
<stroke android:color="#color/colorWhite" android:width="15px" />
</shape>
</item>
Then the activity's button has the XML file as the background.
You can use animated-selector instead of plain selector. And control the animation through alpha property.
I am trying to set the foreground color of a listview item on multi select of items. But my problem is it is collapsing with the background attribute of listview item. How do I set the foreground to ?attr/activatedBackgroundIndicator without collision with background drawable selector. I have the background drawable set to selector_card_bg which displays card like item. Now, on selecting these items , I want the selected items to be highlighted as in the screenshot.
listview_item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/selector_card_bg"
android:foreground="?attr/activatedBackgroundIndicator"
android:padding="5dp" >
selector_card_bg:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/card_background_pressed" />
<item android:drawable="#drawable/card_background" />
</selector>
card_background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#4f334e"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
UPDATE:
Able to achieve by adding the code in getView of adapter. But getView is called so many times because of this. Any suggestions to improve this?
if(mSelectedItemsIds!=null && mSelectedItemsIds.get(position, false))
{
Log.d("tag","if triggered"+mSelectedItemsIds+"position is="+position);
view.setBackgroundColor(Color
.parseColor("#CCCCCC"));
}
else
{
Log.d("tag","else triggered"+mSelectedItemsIds+"position is="+position); // how to restrict this else loop from getting called always
view.setBackgroundResource(R.drawable.selector_card_bg);
}
I am using the following xml code to show smooth top white strip and bottom white strip on list item selection. The problem is that it shows gradient on list item selection.I think, I am missing something in xml,Please help me.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#android:color/white"
android:centerColor="#android:color/transparent"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
</selector>
<!-- If you simply want a background for your list item, use the following -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"><stroke android:color="#color/popup_border" android:width="1dp"/> <!-- For border/outline(Remove this line if you don't want border/outline)-->
<gradient android:startColor="#android:color/white" android:centerColor="#android:color/transparent" android:endColor="#android:color/white" android:angle="270" />
</shape>
</item>
</selector>
I am using a listview. Each row has a fancy layout with different press states, shapes and layers. In the adapter of my listview, I would like to set a particular color to these states according to the list row content. The particular color I would like to change in my adapter for each row is the on in the file "list_row_pressed.xml" (see below, almost at the end, on line android:color="#android:color/blue" )
The xml for each row is in an xml file "list_child.xml"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlLevel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#layout/list_rowbg" >
...
"list_rowbg.xml" is another xml file containing my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#layout/list_row_pressed" >
</item>
<item
android:state_pressed="false"
android:drawable="#layout/list_row_notpressed" >
</item>
"list_row_pressed.xml" is:
<?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/transparent" />
<corners
android:radius="3dp" />
</shape>
</item>
<!-- Top Shadow -->
<item android:top="#dimen/rowShadow">
<shape>
<solid
android:color="#android:color/blue" /> <-- I WANT TO SET THIS COLOR DYNAMICALLY
<corners android:radius="3dp" />
<stroke
android:width="0dp"
android:color="#color/grey" />
</shape>
</item>
So how can I change in my listview adapter the color "#android:color/blue" in my file "list_row_pressed.xml" ?
Thanks
How to add border around linear layout except at the bottom ?
LinearLayout needs to have border at left, top and right side but not at the bottom.
Create an XML file named border.xml in the drawable folder and put the following code in it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Then add a background to your linear layout like this:
android:background="#drawable/border"
EDIT :
This XML was tested with a galaxy s running GingerBread 2.3.3 and ran perfectly as shown in image below:
ALSO
tested with galaxy s 3 running JellyBean 4.1.2 and ran perfectly as shown in image below :
Finally its works perfectly with all APIs
EDIT 2 :
It can also be done using a stroke to keep the background as transparent while still keeping a border except at the bottom with the following code.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="0dp" android:right="0dp" android:top="0dp"
android:bottom="-10dp">
<shape android:shape="rectangle">
<stroke android:width="10dp" android:color="#B22222" />
</shape>
</item>
</layer-list>
hope this help .
Save this xml and add as a background for the linear layout....
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FF00FF00" />
<solid android:color="#ffffff" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="0dp" />
<corners android:radius="4dp" />
</shape>
Hope this helps! :)
Kenny is right, just want to clear some things out.
Create the file border.xml and put it in the folder res/drawable/
add the code
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FF00FF00" />
<solid android:color="#ffffff" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="0dp" />
<corners android:radius="4dp" />
</shape>
set back ground like android:background="#drawable/border" wherever you want the border
Mine first didn't work cause i put the border.xml in the wrong folder!
Here is a Github link to a lightweight and very easy to integrate library that enables you to play with borders as you want for any widget you want, simply based on a FrameLayout widget.
Here is a quick sample code for you to see how easy it is, but you will find more information on the link.
<com.khandelwal.library.view.BorderFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:leftBorderColor="#00F0F0"
app:leftBorderWidth="10dp"
app:topBorderColor="#F0F000"
app:topBorderWidth="15dp"
app:rightBorderColor="#F000F0"
app:rightBorderWidth="20dp"
app:bottomBorderColor="#000000"
app:bottomBorderWidth="25dp" >
</com.khandelwal.library.view.BorderFrameLayout>
So, if you don't want borders on bottom, delete the two lines about bottom in this custom widget, and that's done.
And no, I'm neither the author of this library nor one of his friend ;-)