List selection does not disappear after the second tap - android

I have a ListView or PreferenceView with some items (5 for ex.)
and each element has android:background value
android:background="#drawable/red_selector_list"
red_selector_list:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/red_gradient" />
<item android:state_focused="true" android:drawable="#drawable/red_gradient" />
<item android:state_pressed="true" android:drawable="#drawable/red_gradient" />
<item android:drawable="#drawable/gray_gradient_list" />
</selector>
red_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#990000"
android:centerColor="#b00000"
android:endColor="#ff0000"
android:type="linear" />
<corners android:radius="0dp"/>
</shape>
gray_gradient_list:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:angle="90" android:startColor="#ebebeb"
android:endColor="#ffffff" android:type="linear" />
<corners android:radius="0dp"/>
</shape>
Steps to reproduce this issue:
List item
Touch and hold any item
Touch and hold any other item using other finger
Remove finger which keeps option from step 2
Continue keep your finger from step 3 on the screen and swipe down until the
end of screen
Highlighting always remains after selecting two items at the same time and should be disappeared. This issue cannot be reproduced with items without the android:background value. Could it be an Android platform issue?

Related

Clicked Animation is gone after design with <shape>

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.

Listview Item with both foreground and background together

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);
}

Adding selector to listview item

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>

Android : Image button or button Highlighted with Effect when Pressed

Here when i pressed on these left and right arrow button at that time I want to see these type of effects on button. These same thing happens in Iphone/IOS by default.
Can i make this type of effect?
Here i mentioned the pic what exactly i want.
Here i used this xml files but didnt got success.
button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<gradient android:angle="180" android:centerColor="#657377" android:endColor="#AFFFFFFF" android:startColor="#FFFFFFFF" android:type="linear"/>
<corners android:radius="10dp" />
</shape></item>
</selector>
EDIT
I used android:background="#drawable/button_pressed.xml" line but i did not got which i want Exactly.
Tried :
I used this xml file as per Piyush's Answer but i didnt get success and i am getting this effect.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/transparent" />
</shape>
</item>
<item>
<shape
android:innerRadiusRatio="4"
android:shape="ring"
android:thicknessRatio="9"
android:useLevel="false" >
<gradient
android:endColor="#00000000"
android:gradientRadius="250"
android:startColor="#ffffffff"
android:type="radial" />
</shape>
</item>
</layer-list>
I agreed top and bottom part cuts because i have limited space in layout for this button. that we will think later but why its not taking effect like shadow and alpha and all that same like which i mentioned?
OUTPUT :
Please help me If any body has idea about this.
Thanks in Advance.
The shape has to be radially drawn with fading from white to black outwards.
Try this:
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffffff"
android:endColor="#00000000"
android:gradientRadius="100"
android:type="radial"/>
</shape>
Also, you cannot directly set this as a background to your button. You will have to create a selector drawable file in which you specify different backgrounds based on the state of the button. In this case, you need this background to be set only when button is pressed. The selector will look something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_pressed"/>
<item android:state_selected="true" android:drawable="#drawable/button_pressed"/>
<item android:state_focussed="true" android:drawable="#drawable/button_pressed"/>
<item android:drawable="#android:color/transparent" />
</selector>
PS: It is not recommended to replicate iOS design when designing an Android app. For Android design guidelines, please refer: http://developer.android.com/design/building-blocks/buttons.html
can try this regarding for circular shape, can set your colors accordingly.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/transparent" />
</shape>
</item>
<item>
<shape
android:shape="ring"
android:useLevel="false" >
<gradient
android:endColor="#00000000"
android:gradientRadius="200"
android:startColor="#ffffffff"
android:type="radial" />
</shape>
</item>

How to change the color of the color button when pressed in Android?

I have some buttons which I set its background color as red, green and blue separately. When I press the button, click event is generated, but there is no change in gui for the user to know the button is pressed. Android button’s default background grayish color changes to orange and come back to grayish color after releasing the pressed state. How to implement this on colored button?
That is implemented via a StateListDrawable, represented by selector in XML. Reference: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Here is an example of a drawable that will be white by default, black when pressed:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:color/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
As mentioned by K-Ballo you can use StateListDrawable to implement a view with various different graphics depending on state. In your case Button is the view where two states are Button pressed and button not pressed.
We need to create a buttonselector.xml file in the drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_not_pressed" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/button_pressed" android:state_pressed="true"/>
</selector>
Create two separate xml files for the state of the button
button_not_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
</shape>
button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000"
android:centerColor="#FF0000"
android:endColor="#FF0000"
android:angle="270" />
</shape>
You will notice two html color codes #FF0000 & #FFFFFF which represent background color of the button according to the state.
In you main.xml where you set the style of your custom button
<Button
android:id="#+id/customButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="test"
android:textColor="#000000" />
In you activity class add the following two lines
Button customButton = (Button) findViewById(R.id.customButton);
customButton.setBackground(getResources().getDrawable(R.drawable.buttonselector));
Hope it helps
Try out this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<solid
android:color="#1E669B" />
<stroke
android:width="2dp"
android:color="#1B5E91" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1E669B"
android:endColor="#1E669B"
android:angle="270" />
<stroke
android:width="4dp"
android:color="#1B5E91" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>

Categories

Resources