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);
}
});
Related
I'm making an android app, and I'm using Spinners in Dialog Mode, defined as follows :
<Spinner android:id="#+id/new_order_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SpinnerTheme"
style="#style/UnderlinedSpinner"
android:layout_below="#+id/new_order_hint_address"
android:prompt="#string/myString"
android:spinnerMode="dialog"
android:layout_marginBottom="5dp" />
where the styles are
<style name="SpinnerTheme">
<item name="colorControlNormal"> #color/colorPrimaryDark </item>
<item name="android:textColorSecondary"> #color/colorPrimaryDark </item>
<item name="android:colorControlHighlight"> #color/colorPrimaryDark </item>
<item name="android:colorControlActivated"> #color/colorPrimaryDark </item>
<item name="android:minHeight"> 35dp </item>
<item name="android:showDividers"> middle </item>
<item name="android:divider"> #color/colorAccent </item>
<item name="android:dividerHeight"> 0.5dp </item>
</style>
<style name="SpinnerLabel" parent="TextAppearance.Design.Hint">
<item name="android:paddingLeft">#dimen/input_label_horizontal_spacing</item>
<item name="android:paddingRight">#dimen/input_label_horizontal_spacing</item>
<item name="android:textSize"> 14sp </item>
<item name="android:textColor">#color/hintText</item>
</style>
<style name="UnderlinedSpinner" parent="Base.Widget.AppCompat.Spinner.Underlined"/>
In API <23, everything works fine, but in API 23, the prompt is not shown. I've tried setting in from the java code, I've read all the similar questions and answers and have had no luck whatsoever.
Thanks in advance
Ok, after a lot more of trial and error, I found out that the issue was in fact that the color of the prompt text was the same as the color of the background of the prompt (both white).
The reason behind it relays on the "textColorPrimary", which I'm not setting in the style of the spinner, but I did set in the style of the Tabhost where it is located. It looks like it inherited this property.
Therefore, the solution is just adding that parameter to the style, with the color desired.
The only thing I'm not sure is why this only happens in APIs > 23, and not the others.
I like to show alert like this:
And when I click the first settings icon, show an alert like this one:
To achive that you have to group the subitems and embed in a one menu item.
Here is an example you can refer.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/id_group1"
android:icon="#drawable/v3_actionbar_setting"
android:orderInCategory="120"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:id="#+id/subitem1"
android:icon="#drawable/actionbar_settings"
android:title="subitem 1"/>
<item
android:id="#+id/subitem2
android:icon="#drawable/image2"
android:title="subitem 2">
</item>
</menu>
</item>
<item
android:id="#+id/group2"
android:icon="#drawable/group2"
android:orderInCategory="90"
android:showAsAction="always"
android:title="group 2">
<menu>
<item
android:id="#+id/subitem21"
android:icon="#drawable/subitem1"
android:title="subitem1"/>
<item
android:id="#+id/subitem22"
android:icon="#drawable/sub2"
android:title="sub2"/>
</menu>
</item>
Hope this helps :)
Take a good look at this: Android Menus
You'll possibly find all you need.
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);
}
}
});
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.
I'd like to change the background for an image button. Basically the image I have looks great on a transparent background and a bit lousy when there are a bunch of them all with a non-transparent background.
This should be easy - just change the android:background on the button to a transparent color (via a drawable):
click_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"><color android:color="#FF008080" />
</item>
<item android:state_pressed="true"><color android:color="#FF008080" />
</item>
<item><color android:color="#00000000" />
</item>
</selector>
Then the button
<ImageButton
android:id="#+id/players"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#drawable/click_background"
android:contentDescription="#string/players"
android:src="#drawable/speaker_tile" />
The problem is that I actually want to retain the state_focussed and state_pressed versions of the background (which I presume is derived from the theme). I would like to have the background appear when the button is pressed or selected (and appear with the exact same colour/drawable) that would be used by the theme normally when a button is pressed would be helpful.
I was wondering if there is a way to do one of the following, but have so far been unable to find anything on either:
Define a selector that in some what inherits from another (similar to the way a theme can inherit from another).
Create an entirely new selector but have it's XML reference the color/drawable of the theme's state_focussed and state_pressed used for an image button. Edit: It looks like this option is out. I would have needed attributes, but you can't reference them from a drawable. See here
I'd like to do this in Declarative XML rather than Programmatic Java if possible.
You can copy the drawables used by the Android OS into your project and use them in your state-list drawable. You can find the images in {android-sdk-directory}/platforms/android-##/data/res/drawable[-*dpi]
EDIT:
If you go to {android-sdk-directory}/platforms/android-##/data/res/values, you'll find the themes.xml and styles.xml files used by Android. Using them you can figure out which drawables you'll be looking for.
For example, the default theme on newer versions of Android is Theme.Holo. This theme has a default style for ImageButtons declared like so:
<item name="imageButtonStyle">#android:style/Widget.Holo.ImageButton</item>
In styles.xml, this style is defined as follows:
<style name="Widget.Holo.ImageButton" parent="Widget.ImageButton">
<item name="android:background">#android:drawable/btn_default_holo_dark</item>
</style>
Thankfully the background attribute is right there defined in plain sight. Sometimes it's inherited from a parent style and you have to find that instead. In any case, here's the drawable (found in the /drawable directory since it's xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
So those are the drawables Android uses for the background of a standard ImageButton in the default (holo dark) theme.