- android button selector
- 6 Answers 6
- How to highlight a button when is pressed?
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Buttons
- Responding to Click Events
- Kotlin
- Using an OnClickListener
- Kotlin
- Styling Your Button
- Borderless button
- Custom background
- Android ImageButton selector example
- 1. Add Images to Resources
- 2. Add Selector for different button states
- 3. Add Button
- 4. Code Code
- 5. Demo
- Download Source Code
- References
- Comments
- How to set background of selected/unselected Button in the XML file
- 4 Answers 4
android button selector
This is a button selector such that when normal it appears red, when pressed it appears grey.
I would like to ask how could the code be further directly modified such that when PRESSED the text size and color could also change? Many thanks!
6 Answers 6
You just need to set selector of button in your layout file.
Following is button_effect.xml file in drawable directory
In this, you can see that there are 3 drawables, you just need to place this button_effect style to your button , as i wrote above. You just need to replace selector_xml_name with button_effect .
You can’t achieve text size change with a state list drawable. To change text color and text size do this:
Text color
To change the text color, you can create color state list resource. It will be a separate resource located in res/color/ directory. In layout xml you have to set it as the value for android:textColor attribute. The color selector will then contain something like this:
Text size
You can’t change the size of the text simply with resources. There’s no «dimen selector». You have to do it in code. And there is no straightforward solution.
Probably the easiest solution might be utilizing View.onTouchListener() and handle the up and down events accordingly. Use something like this:
A different solution might be to extend the view and override the setPressed(Boolean) method. The method is internally called when the change of the pressed state happens. Then change the size of the text accordingly in the method call (don’t forget to call the super).
Источник
How to highlight a button when is pressed?
I’m making an Andorid quiz and I want to highlight a button when it’s clicked but when the user lets go of the button that it turns in it original colour. You see I’ve set the background of the button so the buttons can be rounded. I’ve set that in drawable.
6 Answers 6
You can use OnTouchListener or you can use a selector.
You can use a selector also. Borders and rounded rectangle. Customize the same.
bkg.xml in drawable folder
normal.xml in drawable folder
pressed.xml in drawable folder
Now set the background fro your button in xml
use a selector like this and set your buttons background to the drawable.
If you want to do it programmatically then you can also try one of following two methods:
just put this code in your onCreate method of activity and it will do. You can change the color codes according to your choice.
If you don’t want to create 2 drawables with a selector xml, or 2 shapes or even don’t want to bother doing it programmatically with a color filter, you can use the Android built-in highlight by ussing the selectableItemBackground attibute :
in your xml. For instance :
Source Code
selector.xml
normalpressed.xml
pressed.xml
Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Buttons
A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.
Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:
- With text, using the Button class:
- With an icon, using the ImageButton class:
- With text and an icon, using the Button class with the android:drawableLeft attribute:
Key classes are the following:
Responding to Click Events
When the user clicks a button, the Button object receives an on-click event.
To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here’s a layout with a button using android:onClick :
Within the Activity that hosts this layout, the following method handles the click event:
Kotlin
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
- Be public
- Return void
- Define a View as its only parameter (this will be the View that was clicked)
Using an OnClickListener
You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) . For example:
Kotlin
Styling Your Button
The appearance of your button (background image and font) may vary from one device to another, because devices by different manufacturers often have different default styles for input controls.
You can control exactly how your controls are styled using a theme that you apply to your entire application. For instance, to ensure that all devices running Android 4.0 and higher use the Holo theme in your app, declare android:theme=»@android:style/Theme.Holo» in your manifest’s element. Also read the blog post, Holo Everywhere for information about using the Holo theme while supporting older devices.
To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource. Alternatively, you can apply a style for the button, which works in a manner similar to HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, see Styles and Themes.
Borderless button
One design that can be useful is a «borderless» button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.
To create a borderless button, apply the borderlessButtonStyle style to the button. For example:
Custom background
If you want to truly redefine the appearance of your button, you can specify a custom background. Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button’s current state.
You can define the state list in an XML file that defines three different images or colors to use for the different button states.
To create a state list drawable for your button background:
- Create three bitmaps for the button background that represent the default, pressed, and focused button states.
To ensure that your images fit buttons of various sizes, create the bitmaps as Nine-patch bitmaps.
Источник
Android ImageButton selector example
By mkyong | Last updated: August 29, 2012
Viewed: 287,123 (+122 pv/w)
In last Android tutorial, you use “ ImageButton ” to display a “ Button ” with a customized background image easily. However, you can do more than that just a simple image, Android allow you to change the button’s image depends on different states like button is focused or button is pressed.
This example is referenced from this Android custom button article, with minor changes.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1. Add Images to Resources
Prepare 3 images for button states, and put it into “resource/drawable” folder.
- button_normal_green.png – Default image button.
- button_focused_orange.png – Display when button is focused, for example, when phone’s keypad is move (focus) on this button.
- button_pressed_yellow.png – Display when button is pressed.
2. Add Selector for different button states
Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “ new_button.xml “. This file defined which button state is belong to which image.
Now, you can refer to this button via this Id : @drawable/new_button .
3. Add Button
Open “res/layout/main.xml” file, add a normal button, and attach the background image to above “new_button” via “ android:background=»@drawable/new_button ”
4. Code Code
A normal button with a simple click listener.
5. Demo
Run the application.
1. Result, default button. (button_normal_green.png)
2. Button is focused. (button_focused_orange.png)
3. Button is pressed. (button_pressed_yellow.png)
Download Source Code
References
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.
Comments
This tutorial still can be use. AWESOME !!
Thanks for explaining this so well, It really helps me in understanding basic android concepts of xml.
Thanks for sharing this is Really helped me, ur explanation is very clear and u added the downloadable file thats add the advantage to rectify errors more accurately рџ™‚ Good Work Keep Going
Nice, but I have a question about it.
I would like to have a kind of state on this image button.
I now have a toggle button like this.
private OnClickListener powerButtonListener = new OnClickListener() <
public void onClick(View arg0) <
ToggleButton powerButton = (ToggleButton) arg0;
if (D) Log.d(TAG, “powerButtonListener” + powerButton.isChecked());
searchAndConnect();
if (powerButton.isChecked()) <
powerOn = 1;
sendColorToArm();
> else <
powerOn = 0;
sendColorToArm();
if (mBTSerialService != null) mBTSerialService.stop();
>
>
>;
(My image’s are in a xml file called on_btnaction.xml)
in mainActivity.java.
public class mainActivity extends Activity <
ImageButton on_btnaction;
@Override
public void onCreate(Bundle savedInstanceState) <
on_btnaction = (ImageButton) findViewById(R.id.on_btnaction);
on_btnaction.setOnClickListener(on_btnactionListener);
>
private OnClickListener on_btnactionListener = new OnClickListener() <
public void onClick(View arg0) <
ImageButton on_btnaction = (ImageButton) arg0;
if (D) Log.d(TAG, “on_btnactionListener” + on_btnaction.getDrawableState());
searchAndConnect();
if (on_btnaction.getDrawableState(1)) <
powerOn = 1;
sendColorToArm();
> else <
powerOn = 0;
sendColorToArm();
if (mBTSerialService != null) mBTSerialService.stop();
>
>
>;
Well, what do you think and what do I have to do to make the ImageButon like the ToggleButton?
Thanks in advance.
Источник
How to set background of selected/unselected Button in the XML file
I have custom buttons that are supposed to have different backgrounds depending if they are selected or not selected. I want to know if there is a way to state this in the XML file. I have a button for Celsius and a button for Fahrenheit. I want it to work where if one is selected, it stays «pressed» and unable to be clicked, while the other button can be pressed.
The Celsius button is defaulted to selected. I try working on it like this in my code, but it just seems to messy:
4 Answers 4
Be aware that there are still 4 states for that
You define them in a selector like this
Then define it in your button like this
Edit
You could actually just use 1 button for your use. Alternatively you can use 2 radio buttons
This is used to change color of button on pressed or focused write this code in your drawable folder
To change background image:
Or, using an XML file:
In OnClick, just add this code:
Yes, you can select a Drawable based on the state of the View that is rendering it.
This is the exact purpose of a Selector type of drawable. See examples and guide here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Basically, each item of a selector defines which states have which values. It also defines which drawable represents this set of values.
Next, you can set state of a View from the code, eg.
This is great in practice because you’re separating UI settings from the model/controller. Maintaining large sets of drawables comes easier when the code isn’t responsible for changing the UI of your application in a direct way.
An example of my working selector is:
This example renders a button background depending on it’s state.
Источник