Pressed <Button> Selector

Selector for pressed button

I think you are looking for the :active pseudo-class...

#btn { border: solid 5px red; padding: 5px; }#btn:hover { border-color: green; }#btn:active { border-color: blue; }
<button id="btn" />

Pressed button selector

You can do this if you use an <a> tag instead of a button. I know it's not exactly what you asked for, but it might give you some other options if you cannot find a solution to this:

Borrowing from a demo from another answer here I produced this:

a {  display: block;  font-size: 18px;  border: 2px solid gray;  border-radius: 100px;  width: 100px;  height: 100px;  text-align: center;  line-height: 100px;}
a:active { font-size: 18px; border: 2px solid green; border-radius: 100px; width: 100px; height: 100px;}
a:target { font-size: 18px; border: 2px solid red; border-radius: 100px; width: 100px; height: 100px;}
<a id="btn" href="#btn">Demo</a>

How to style a clicked button in CSS

This button will appear yellow initially. On hover it will turn orange. When you click it, it will turn red. I used :hover and :focus to adapt the style.
(The :active selector is usually used of links (i.e. <a> tags))

button{  background-color:yellow;}
button:hover{background-color:orange;}
button:focus{background-color:red;}
a { color: orange;}
a.button{ color:green; text-decoration: none;}
a:visited { color: purple;}
a:active { color: blue;}
<button>Hover and Click!</button><br><br>
<a href="#">Hello</a><br><br><a class="button" href="#">Bye</a>

Button selector pressed_state doesn't work

Try either:

  • adding these 2 properties to your Button in xml:

android:clickable="true"
android:focusable="true"

  • or adding View.OnClickListener to your Button programatically in Activity

android button selector

You just need to set selector of button in your layout file.

<Button
android:id="@+id/button1"
android:background="@drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />

and done.

Edit

Following is button_effect.xml file in drawable directory

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

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.

Pressed button css

It's much easier to use hidden radio inputs with labels. I added and modified the jQuery, although you don't need it for the effects. See this POST

$(function() {  $('.btn').on('click', function(event) {    $(this).addClass('active');    $('.btn').not(this).removeClass('active');  });});
.tablist input[type="radio"] {  display: none;}.tablist label {  display: inline-block;  background-color: #ddd;  color: grey;  padding: 3px 6px;  font-family: Arial;  font-size: 16px;  border: 1px inset grey;  border-radius: 6px;  cursor: pointer;}.tablist input[type="radio"]:checked + label {  background-color: transparent;  color: blue;  border: 1px inset blue;}fieldset {  width: -moz-fit-content;  width: -webkit-fit-content;  width: -fit-content;  border-radius: 6px;}.btn {  text-decoration: none;  /* When you integrate this code, change none to auto */  pointer-events: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><fieldset class="tablist">  <input type="radio" id="r1" name="rad" data-toggle="tab" checked>  <label for="r1" class="tag">    <a href="#" class="btn active" data-toggle="tab">Sports</a>  </label>
<input type="radio" id="r2" name="rad" data-toggle="tab"> <label for="r2" class="tag"> <a href="#" class="btn" data-toggle="tab">Sante</a> </label>
<input type="radio" id="r3" name="rad"> <label for="r3" class="tag"> <a href="#" class="btn" data-toggle="tab">Claus</a> </label></fieldset>

Selector not working on click button effect

I believe it's because of your selector and how it's set up. The default item in the selector needs to be the last one. Try

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true"
android:drawable="@color/colorPrimaryDark"/>

<!-- default -->
<item android:drawable="@color/colorAccent"/>
</selector>


Related Topics



Leave a reply



Submit