I am using a selector working fine in all activities as you can see its XML below.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- focused -->
<item
android:drawable="#drawable/active_date_horizontal_5" /> <!-- default -->
Except one activity, it does not work ridiculously. You can see three screenshots below which the selector works fine in the first two screenshots, and does not work in the last screen shot.
1)
2)
3)
These are customized GridViews which filled with customized BaseAdapter. Cells in the GridViews have an Xml Layout which its background is the selector. Any ideas?
It seems I solved my own problem.
You can see the Xml Layout which I am applied the selector below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#drawable/calendar_cell_selector" >
<TextView
android:id="#+id/txtWeeklyEventName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imgCornerWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtWeeklyEventName"
android:layout_alignRight="#+id/txtWeeklyEventName"
android:src="#drawable/corner_orange"
android:visibility="visible" />
</RelativeLayout>
You can see the background of the RelativeLayout is the selector, but the TextView fills upon the whole layout so Android cannot handle the selector.
Thus, I removed the background from the RelativeLayout and set it for the TextView, it totally works.
Thanks to me!
Related
I am trying to change color of my card view when someone click on it. So far I have tried using this code below:
//drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="#color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="#color/button_focused"/> <!-- focused -->
<item android:drawable="#color/button_default"/> <!-- default -->
</selector>
This is my selector code this works perfect when I use this in button code is below:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector"
android:text="Click Me"
/>
I am trying to do this inside card view I can't see anything inside my card view I am using selector like this:
<android.support.v7.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_width="170dp"
android:layout_height="150dp"
android:layout_margin="4dp"
android:onClick="economy"
android:background="#drawable/selector"
>
</android.support.v7.widget.CardView>
My card is clickable my question is can I do this inside card view
There is a limitation with Cardview on changing the background using selector.If You still want to go ahead with that. You can apply selector on foreground.
<android.support.v7.widget.CardView
android:foreground="#drawable/selector" // change
android:clickable="true"
android:layout_width="170dp"
android:layout_height="150dp"
android:layout_margin="4dp"
android:onClick="economy"
>
</android.support.v7.widget.CardView>
And add alpha to the color(like : #55FFFFFF) which you are applying on the selector so that the content of the card will be visible. This is not ideal but depends on your output expectation.
I'd like to get this effect in android, I tried setting the background of the layout but without success, it would be like putting a mask on the elements of layout. An example of these is when you click a button on google play, which look like this:
when you click it puts a mask on blue button.
Could someone help me?
Thank you.
I did the following, I created the layout with image and text.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APP" />
</LinearLayout>
and create framelayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_default"
android:clickable="true">
<include
android:layout_gravity="center_vertical"
layout="#layout/test1" />
</FrameLayout>
but only the background is changed, the images and text are not placed the mask. Am I doing something wrong?
The first thing that comes to my mind to implement this kind of affect is:
1. Create a layout(Linear/Relative) with the Icon and the text (android icon and APPS text in your example.)
2. Set it to wrap-content for both dimensions.
3. Place this layout inside FrameLayout.
4. Inside this FrameLayout add a button and set it to match-parent, for this button create a selector like this and apply it as it background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/botton_shape_pressed" />
<!-- focused -->
<item android:state_focused="true"
android:drawable="#drawable/botton_shape_selected" />
<!-- default -->
<item android:drawable="#drawable/botton_shape_released" />
5. For the pressed texture create a semi-transparent blue texture. for the other two states apply a full transparent texture. this will result in the mask affect you looking for.
I realize this has been asked multiple times, but my image is changing size when it is pressed (which is should be the same size, but a different image). I've previously tried setting the size of the button in java, but it didn't work either. Here is my code.
XML 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="#drawable/testbuttonpressed" /> <!-- pressed -->
<item android:drawable="#drawable/testbutton" /> <!-- default -->
</selector>
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backtest1">
<Button
android:id="#+id/start_button"
android:layout_width="225px"
android:layout_height="55px"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="#string/start"
android:background="#drawable/buttonsxml"
/>
</LinearLayout>
It turns out my pictures were a pixel off...
My imagebutton code wont respond to "fill_parent" and "wrap_content".
Instead, it just shows the middle of the image (because it is a pretty big image).
I tried setting specific values for the image but it still didn't work! Can somebody help me?
My button.xml in the res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">\
<item android:state_pressed="true"
android:drawable="#drawable/largishbutton" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/largerbutton2" /> <!-- focused -->
<item android:drawable="#drawable/largerbutton" /> <!-- default -->
</selector>
My main.xml in the res/layout folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white">
<ImageButton
android:src="#drawable/button"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
/>
</RelativeLayout>
I tried using a LinearLayout, but the app wouldnt run, and I tried replacing the imagebutton with an imageview, but then the button didn't work.
If your image is bigger than your button, you will need to use something like android:scaleType or setMaxHeight() to have it resize to fit.
Also, I recommend that you have all images in your <selector> be the same size, at least until you get things working the way you wish.
I've scoured the interwebs and found many posts regarding how to change the colors of a list view using a list selector. However it does not seem to work for me. So my question would be what am I doing wrong?
When I use the below files I get a list where all item backgrounds are initially blue. (I expected white)
When I move the focus up and down the text just changes to a dark grey and the item backgrounds are still blue. (This is when I would expect a single row to be blue with the rest white)
When I click on a row the background of the row I clicked on turns black and all other rows turned green. (I expected to have the row I clicked turn green and the rest be white)
Here is my main layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#drawable/item_selector"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty"
/>
</LinearLayout>
Here is my list item file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textStyle="bold"
android:padding="7dip"
android:textSize="18sp"
android:layout_toRightOf="#id/checkbox"
/>
</RelativeLayout>
Here is my colors file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff00</color>
<color name="green">#f0f0</color>
<color name="blue">#f00f</color>
<color name="white">#ffff</color>
</resources>
Here is my selector file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#color/green" />
<item
android:state_focused="true"
android:drawable="#color/blue" />
<item
android:drawable="#color/white" />
</selector>
Hopefully it's something dumb and simple that I'm doing wrong.
Thanks in advance.
I think that android:listSelector="#drawable/item_selector" is applied only on the root of the view not at all his subchild.
Try to add #drawable/item_selector as a backgroud of your RelativeLayout with android:background="#drawable/item_selector" and put a transparent layout to the listSelector:
android:listSelector="#android:color/transparent"
Actually if you want to do it programatically without the use of resources, you can set your element color. For example, to set text color #F123456 in a TextView:
objTextView.setTextColor(color.parseColor("#F12345"));
This is handy if you want to dynamically set a color in a list view that is user selectable, perhaps stored in a database where you simply cant use a static resource file for color lists.