StaggeredGridView change long click colour - android

I want to change the background colour of my StaggeredGridView item when pressed, but currently I am getting an ugly gingerbread orange by default, as shown
here. I tried setting the gridview items background as a selector, but when I did that, if I click one item, all the items' background colours are changed.
<!-- My selector -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_blue_light"
android:state_pressed="true" />
<item android:drawable="#android:color/transparent" />
</selector>
And this was in my StaggeredGridView, but it didn't help:
<!-- In StaggeredGridView -->
android:listSelector="#drawable/selector"
By the way, I am using this StaggeredGridView Library. Thanks in advance!

at the implementation of staggeredGridViews onClick event:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for(int i=0;i<mGridView.getChildCount();i++) {
mGridView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.yellow));
// set all items to yellow
}
mGridView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.blue));
//set the selected items color to blue
}
hope this helps

The solution was to stay away from any #color or #drawable that refers
to a color inside listSelector. I created two 3x3 pixel .png files.
Each saved with the gamma layer. In my case it's two of the same color
each mixed down in Gimp with a different transparency on the color
layer. So when you select an item you get an overlay with 25% color,
and when you press it you get a png with 50% color. I put them in my
drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/bg_list_item_highlighted" /> <!-- #drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/bg_list_item_pressed" /> <!-- #drawable/tab_press -->
</selector>
then
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"

you should change a line in that library and than make your own selector
1) in the library there is a class called: StaggeredGridView
there is a method in that class called: useDefaultSelector()
inside that comment the setSelector line and add this line:
setSelector(getResources().getDrawable(R.drawable.selector));
2)make an xml file inside drawable folder in the library as follow: selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/yourColor1" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/yourColor2" />
<item android:state_focused="true" android:drawable="#color/yourColor3" />
</selector>

Related

Style EditText underline color android

I am creating a application that has couple of edittext's that chang color when the edittext has focus or not. When the view has the focus the color should be blue, when normal it should have white color, something like this:
I've tried creating two nine patches and setting the background according to this xml but seems its not working. Can anyone tell me how can I achieve this ?
Xml selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_focused="true"
android:drawable="#drawable/spelling_blue" />
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spelling_white" />
<item android:drawable="#drawable/spelling_white" />
</selector>
Thanks
I don't think you need to use android:state_window_focused.
Try this:
<?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/spelling_blue" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/spelling_blue" /> <!-- focused -->
<item
android:drawable="#drawable/spelling_white" /> <!-- default -->
</selector>

Android:Styling ListView

I want to put some style in the ListView in such a way that when an item of ListView is hovered, the color of that list item changes. I dont have any idea about how to achieve this.
Please suggest some other ideas about styling the ListView as well.
Regards,
Use selector to define what should happen when you focus the item, select the item and press the item, I doubt you cannot hover over a view, since Android devices use touch screen interface and not a pointing interface like a mouse..
This should help you:
Create a XML file in drawable folder named listselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/blue" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/blue" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/green" /> <!-- pressed -->
<item android:drawable="#color/white" /> <!-- default -->
</selector>
Then set the background of your TextView which you use in ListView to this like
android:background = "#drawable/listselector",
that should do it.

change button text color when pressed

I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?
Yes, you can do it like that:
layout/main_layout.xml:
.....
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bonjour !"
android:textColor="#color/button_text_color"
/>
.....
color/button_text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#c0c0c0" android:state_pressed="true"/>
<item android:color="#ffffff"/>
</selector>
See the section called State List in this bit of documentation...Drawable Resources.
You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.
EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.
I like the solution proposed by Konstantin Burov in the other issue: Android customized button; changing text color
You can actually manage more states than just pressed and normal. But it should solve the problem.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Then you can use that selector drawable in your button changing the text color attribute like below. Note that the selector in the example below is named "button_text_color"
android:textColor="#drawable/button_text_color"
Using the same drawable approach you can also solve the background color of the button. Just remember that in the selector instead of using the "android:color" attribute you need to use the "android:drawable" attribute like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Default color -->
<item android:drawable="#ffffff" />
</selector>
And then in the button itself do, note that this time the selector name is "button_background"
android:background="#drawable/button_background"
You have to do it in your code. Try this:
mBtn = ((Button) findViewById( R.id.button1 ));
mBtn.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
mBtn.setTextColor( Color.RED );
}
});
Declare:
private Button mBtn;
You must set #drawable xml resource in textColor attributte
Here is example: Android customized button; changing text color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#FFFFFF" />
<item
android:state_pressed="true"
android:color="#000000" />
</selector>

question about change the colours of a button when clicked

My purpose is to change the color of a button when click and my codes are
<?xml version="1.0" encoding="utf-8"?>
selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
</selector>
It works but if i make a small change like below :
xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
<!-- When selected, use grey -->
item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
</selector>
It does not work anymore....
I need some help...Any comments are welcomed here.Thanks
I think you need to mention stats in selector like pressed or focused and change image accordingly.
Here i have attached sample selector file,have a look and try accordingly.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:drawable="#drawable/estimator_new" />
</selector>
All The Best....

Android highlight ImageButton onclick

Is there a way to highlight an ImageButton when it's pressed?
You can define a drawable via XML and use the selector, like below, to use different (i.e. highlighted) images for different button states:
i.e. res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_catlocfilter" android:state_pressed="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_pressed="true" />
<item android:drawable="#drawable/bg_catlocfilter" android:state_focused="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_focused="true" />
</selector>
Use this resource then for the ImageButton view.

Categories

Resources