Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a button when is pressed?

Tags:

android

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.

<Button
    android:id="@+id/btn1"
    android:background="@drawable/roundedbutton"
    android:textColor="#ffffff"
    android:textStyle="italic"
    android:layout_width="225sp"
    android:layout_marginTop="23sp"
    android:layout_height="38sp"
    android:layout_alignLeft="@+id/btn2"
    android:layout_below="@+id/textView1"
    android:text="Button" />

roundedbutton.xml

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" android:padding="10dp">   
<solid android:color="#848482"/> <!-- this one is ths color of the Rounded Button -->

<corners
android:bottomRightRadius="6.5dp"
android:bottomLeftRadius="6.5dp"
android:topLeftRadius="6.5dp"
android:topRightRadius="6.5dp"/>
</shape>
like image 843
Jo Ke Avatar asked Jul 31 '13 12:07

Jo Ke


People also ask

How do you highlight a button when pressed?

A simple solution using a color filter similar to the one of Vishwas and with a more complete onTouchListener solution description as the one of Raghunandan: stackoverflow.com/a/14278790/891479 It creates an OnTouchListener modifying the color filter on touch. – L. G.

How do I keep a clicked navigation button highlighted?

If the button has to stay highlighted even if you're not holding the click button, use a:focus { background-color: blue; //or others } Instead, if you want the button to be highlighted only when you are holding them clicked use a:active { background-color: yellow; } hope this helps, good luck with your html.

How do I highlight a selected button in Swift?

In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.


2 Answers

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 :

<!-- Background drawable for bordered standalone items that need focus/pressed states. -->
<attr name="selectableItemBackground" format="reference" />

in your xml. For instance :

<ImageButton
   android:id="@+id/btn_help"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_help"
   android:background="?android:selectableItemBackground"/>
like image 121
Damien Praca Avatar answered Sep 17 '22 22:09

Damien Praca


If you want to do it programmatically then you can also try one of following two methods:

btn1.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));

or this way:

btn1.getBackground().setColorFilter(0xFFAA4400,PorterDuff.Mode.MULTIPLY);

just put this code in your onCreate method of activity and it will do. You can change the color codes according to your choice.

like image 45
DroidDev Avatar answered Sep 17 '22 22:09

DroidDev