I'm attempting to override the default ToggleButton appearance. Here's the XML that defines the ToggleButton:
<ToggleButton android:id="#+id/FollowAndCenterButton"
android:layout_width="30px"
android:layout_height="30px"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginLeft="5px"
android:layout_marginTop="5px" android:background="#drawable/locate_me"/>
Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:
centeredOnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (centeredOnLocation.isChecked()) {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
} else {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
}
}
});
Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/locate_me" /> <!-- default -->
<item android:state_checked="true"
android:drawable="#drawable/locate_me_on" /> <!-- pressed -->
<item android:state_checked="false"
android:drawable="#drawable/locate_me" /> <!-- unchecked -->
But this does not work; reading the ToggleButton API (http://developer.android.com/reference/android/widget/ToggleButton.html), it appears that the only inherited xml attributes are
XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.
There does not seem to be the android:state_checked attribute, despite the class having the method isChecked() and setChecked().
So, is there a way to do what I want in XML, or am I stuck with my messy workaround?
Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:
<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
</selector>
Related
I am setting custom drawable to the checkbox button,
But only android:state_checked="true" and android:state_checked="false" seem to work.
Other states aren't working.
I am unable to set a custom drawable on pressed state.
This is the selector I am using:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/radio_btn_selected" />
<item android:state_checked="false" android:drawable="#drawable/radio_btn_normal" />
<item android:state_pressed="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:state_focused="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:drawable="#drawable/radio_btn_normal"/> <!-- default -->
</selector>
This is how I am setting it to radio button:
radioButton.setButtonDrawable(R.drawable.radio_btn_selectors);
android:state_pressed="true" and android:state_focused="true" are not working because of the ordering in which you declared various states. Whenever you declare a drawable like this, system will goes from top to bottom and if a state is matched, it doesn't look for the other ones and applies the changes based on the very first selection. I think here android:state_checked="false" condition might be executing instead of android:state_pressed="true" and android:state_focused="true". So move android:state_checked="false" to the bottom and then try.
I'm wondering if this is even possible but hopefully someone will be able to confirm.
I've created a simple custom button layout in XML to handle the focused/pressed and dormant states. See code at bottom. This works fine when I use it to create a new button. However, I would like the user to be able to change the button colour via a colour picker if they don't like the default. However, the only way I know to change the button background colour programmatically is to use
mybutton.setBackgroundColor(someothercolor);
but if I do this it overwrites all the XML layout code and I lose the colour change when the button is pressed. I guess this is by design as I'm essentially overwriting the entire background style but what I really want to do is to allow the user to change the button colour when its not pressed to something custom but keep the style and layout of the other states the button could be in (i.e. what happens when its pressed).
Any ideas anyone?
Thank you in advance.
Nat
<?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="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Maybe you can consider creating a ColorStateList programmatically, as described here: How do I create ColorStateList programmatically?
You can try this:
1. remove the default color <item android:drawable="#color/originalbuttoncolor" />
2.Then:
`StateListDrawable ret = (StateListDrawable) res.getDrawable(R.drawable.btn_selector);
ret.addState(new int[] {}, new ColorDrawable(your_desire_color));
mybutton.setBackgroundDrawable(ret);`
You can make multiple files of your selector-list, each one contain different color as the default color, and link those files to the color picker, so you save your logic of selector.
For example:
<?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="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
And:
<?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="#color/yellowbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Edit: if you want to take color from user, this may work, if the selector state will overrided by this code:
ColorDrawable cd = new ColorDrawable(); // initialize it from the color picker;
StateListDrawable states = (StateListDrawable) mybutton.getBackground();
states.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, cd); // the minus means false value
mybutton.setBackground(states);
So I'm programmatically creating new buttons and adding them to a LinearLayout, however I want to initialize these buttons with a predefined style. I've spent some time searching for a solution and trying out answers, but I still can't seem to get it to work.
When I add a new button to the layout, it should look like the buttons (near the top) in this picture.
I've tried creating an xml file in res/values/ and initializing a button with new Button(context, null, R.style.ChoiceButton), but it doesn't work resulting in this happening.
I've also tried the workaround of creating a new layout xml for the button and using (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but that also didn't work, resulting in this (two buttons to show lack of margin).
res/values/choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Snippet from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit long to paste in here.
Is there just something I'm missing? Is this even possible?
Ok #kin3tik, I found an old application I made with some custom button.. see what it looks like :
there is my xml for one button :
<Button
android:id="#+id/num1"
android:layout_width="110dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="#string/num1"
android:textSize="20sp" />
I created file .xml in my drawable folder custombutton.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/bleuperso"/> // you can put #XXXXXX for the color you want
<corners android:radius="4dp"/>
And I just put the style in java :
Bfrancois.setBackgroundResource(R.drawable.custombutton);
With this you should be able to find yourself ;)
This question already has answers here:
Android: disabling highlight on listView click
(15 answers)
Closed 6 years ago.
I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).
Specify android:listSelector="#android:color/transparent" in your ListView XML.
Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.
I have done this way:
By adding two properties of ListView.
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
Your ListView should looks like below:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent">
</ListView>
Done
try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));
The highlight effect is a style on the listSelector. You can override the listSelector style.
This is a example with a listview : Android: disabling highlight on listView click
So I'm wondering how one could implement to an action bar with a logo button, a different image per state.(pressed, released etc).
I know how to do this for a normal button, but I'm not sure if it's possible with the actionbars logo button?
And if it's not, how could one implement am action bar that supports this? externals libs?
Thank you.
Create a drawable xml file in res/drawable such as res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal" />
</selector>
where button_pressed and button_normalare just regular png files in one of your drawable directories (e.g/ drawables-hdpi)
Then in your menu.xml or in your code you can refer to #drawable/button_selector or R.drawable.button_selector and the image will change automatically when the button is pressed.
If you need to change the ActionBar button background you need to override the theme in your res/values/style.xml.
<style name="YourTheme.Sherlock" parent="#style/Theme.Sherlock">
<item name="actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
<item name="android:actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
</style>
Both lines matter: one for the SherlockActionBar and one for the standard ActionBar
Then set the theme to your activity in the manifest:
<activity
android:name=".yourActivity"
android:theme="#style/YourTheme.Sherlock" >
</activity>
And here is actionbar_item_background_selector.xml to be placed in res/drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:drawable="#drawable/abs__list_focused_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
replace the background with the drawable of your choice where android:state_pressed="true"
It's a lot to do for just a background but that is how the ActionBar works.
you can use a selector to pick different image.
Android ImageButton with disabled UI feel
may be help
I think you are looking for this:
final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
This automatically makes the logo you have set look like its being pressed when its clicked. No need to have different images to show state change. Thats a pretty easy way. Though if you are using ActionBarSherlock this won't work, I think. Strictly 4.0 I believe. Check out the doc on ActionBar.