In my app, I am using a custom button with selector states. The unpressed and focused states are working just fine, though the pressed state won't work. Any ideas? The on click listener is implemented in a fragment if that means anything.
Here is my selector code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/participants_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/participants_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/participants_unpressed" />
</selector>
And here is my java code:
Button participantsSelector = new Button(getActivity());
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(300,300);
participantsSelector.setBackgroundResource(R.drawable.selector_participants);
participantsSelector.setLayoutParams(lp);
participantsSelector.setClickable(true);
participantsSelector.setOnClickListener(this);
participantsGrid.addView(participantsSelector);
Button are only pressed when the user has the finger on them. You can use ToggleButton if you want two distinct states.
For your drawable :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
With your XML and the Button you already have, I wonder... what if you programmatically toggle state in the listener?
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(button.isSelected()) {
button.setSelected(false);
} else {
button.isSelected(true);
}
}
});
Related
I am trying to make a favourite item in the menu that can display a hollow heart when unchecked and a solid one when checked
However the selector approach is not working, I managed to make it work in code with a boolean value that is toggled and a item.setIcon() method. But I want to understand why my selector approach didn't work.
here's what I tried:
I tried to make a checkable attribute in the item xml with no luck
also tried to set the setChecked directly from code checked the debugger the item.isChecked() is returning true as intended but the icon doesn't change
tried to set the checked state from inside the xml to true to see if it will change icon still didn't work
The code is showing that the checking state is changing as intended but the selector is not responding by changing the icon accordingly the Question is why is that so and how to solve it.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_contact"
android:title="#string/action_contact"
android:orderInCategory="10"
app:showAsAction="never" />
<item
android:id="#+id/action_order"
android:icon="#drawable/ic_shopping_cart"
android:title="#string/action_order"
app:showAsAction="always" />
<item
android:id="#+id/action_status"
android:title="#string/action_status"
android:icon="#drawable/ic_status_info"
app:showAsAction="always" />
<item
android:id="#+id/action_favourites"
android:title="#string/action_favourites"
android:icon="#drawable/favourite_toggle_image"
android:checkable="true"
app:showAsAction="always" />
</menu>
favourite_toggle_image.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_favourite_info"
android:state_checked="true" />
<item
android:drawable="#drawable/ic_favourite_unchecked_info"
android:state_checked="false" />
</selector>
MainActivity.java
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_order:
displayToast("Order");
break;
case R.id.action_status:
displayToast("Status");
break;
case R.id.action_favourites:
lastState = !lastState;
//if (lastState) item.setIcon(R.drawable.ic_favourite_info);
//else if (!lastState) item.setIcon(R.drawable.ic_favourite_unchecked_info);
item.setChecked(lastState);
displayToast("Favourites");
break;
case R.id.action_contact:
displayToast("Contact");
break;
case R.id.action_settings:
displayToast("Settings");
break;
}
return super.onOptionsItemSelected(item);
}
I currently have a sound button,and I would like to change its background every time when it's selected and clicked(I'm developing on a pair of android glasses, so when the button is selected it's not pressed, thus two different states).
I have used the xml file to change button background when selected so far:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music"
android:state_selected="true" />
<item android:drawable="#drawable/no_music"
android:state_focused="true" />
<item android:drawable="#drawable/music" />
</selector>
And together in the onClick method of the button, I set the button background according to the state:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setBackgroundResource(R.drawable.no_music);
MusicManager.release()
}else{
music.setBackgroundResource(R.drawable.music);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
When I click the button, each time it would change its background. But when I select it, it would only change background once. Is there any method that I can use to make selected state the same as the pressed one?
Thank you very much.
Do this in your code:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setPressed(true);
MusicManager.release()
}else{
music.setPressed(false);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
Also in your xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music" android:state_selected="true"/>
<item android:drawable="#drawable/no_music" android:state_pressed="true"/>
<item android:drawable="#drawable/music"/>
</selector>
you could make an drawable resource as resource for the button:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
<item android:drawable="#drawable/musicon" android:state_checked="false"/>
<item android:drawable="#drawable/musicoff" android:state_checked="true" />
<item android:drawable="#drawable/default" />
something like this should work. good luck.
Set your button pressed property to true, then verify if your button background.xml file has selected item.
OK, when I use setBackground on a button, the button no longer shows any feedback when its clicked. How can I fix this. I just want the button to get darker or some type of feedback so the user knows it was clicked.
Hope this makes sense!
Create this drawable and bind it to button background
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/YOURIMAGE" />
</selector>
Apply it to button
android:background="#drawable/button"
You need to create a selector inside the XML and have it linked to state_pressed = "true". Inside of the item, you can specify the shape of the object. I've linked to the Android developer site so you can see other options available as well.
<item android:state_pressed="true">
<shape>
<solid android:color="#73E5E4D7" />
</shape>
</item>
<item>
<shape>
<solid android:color="#E6E5E4D7" />
</shape>
</item>
Android Developer State List
hope this works well. first assign your button into a button type reference in your activity class.
ex: newBtn=(Button) findViewById(R.id.yourButton);
after that set an listener to that button.
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setBackgroundColor(int colorCode);
}
});
There is the folliwing problem: I've made some Android app and there are 3 menu items (1 dropdown and 2 simple items):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#android:drawable/ic_menu_sort_by_size"
android:showAsAction="ifRoom"
android:orderInCategory="100"
android:title="СSort">
<menu>
<item
android:id="#+id/girls_action_all"
android:title="All"/>
<item
android:id="#+id/girls_action_best"
android:title="Best"/>
<item
android:id="#+id/girls_action_new_people"
android:title="New users today"/>
<item
android:id="#+id/girls_action_50_plus"
android:title="Top 50"/>
<item
android:id="#+id/girls_action_100_plus"
android:title="Top 100"/>
</menu>
</item>
<item
android:id="#+id/girls_action_home"
android:icon="#drawable/ic_menu_home"
android:orderInCategory="101"
android:showAsAction="ifRoom"
android:title="My page"/>
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="ifRoom"
android:title="Search"/>
</menu>
But one item ("Search") doesn't appear on the Action Bar if I use some devices; it appears in the Menu (after click by "Menu" button). I don't like it. Is there any way to fix it?
Try to implement
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="always"
android:title="Search"/>
Change android:showAsAction="ifRoom" to android:showAsAction="always" for the search menu item.
android:showAsAction="ifRoom" will do as it is told - it will display the menu in the action bar if there is enough room; if not, it will overflow to the menu. Currently, all your menu items have this flag set and will compete for the limited space.
android:showAsAction="always" will force the item to be displayed in the action bar. This may cause them to overlap with other UI items if there is not enough room.
Or set android:showAsAction="never" for some of the other items that you don't want in the action bar.
i want to change the green background of a button when this is touched without using OnTouchListener. i have this situation: i put some butons in a layer, then the layer in another layer, and the last layer in onother layer. when i touch the button the background is changing(i m using OnTouchListener right now) but if i drag the finger outside of the button and then get it out of the screen the background of the button remains the image from the state when it s touch(otherwise if i click the button and the the finnger off the button it is k the background is changing)
1. Prepare 3 images for button states, and put it into resource/drawable folder.
2. create a new XML file in res/drawable/ folder, in whatever name you want, in this case, we just give a name as my_button.xml. This file defined which button state is belong to which image.
Now, you can refer to this button via this Id : #drawable/my_button.
File : res/drawable/my_button.xml
create xml file using the button image like this with my_button.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
add a normal button, and attach the background image to above “my_button” via
android:background:#drawable/my_button
The selector will be as below
<?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/btn_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/btn_focused" /> <!-- focused -->
<item android:drawable="#drawable/btn_default" /> <!-- default -->
</selector>