Android highlight an imagebutton when clicked - android

I am using an ImageButton. But I don get the highlight when clicked. I googled and many suggested to use selector where another image is displayed. Is there any way around this. by using only one image and highlighting it or giving it a glow effect. so that the user knows that button has been clicked.

This is actually not very difficult to do. You don't even need to create 2 seperate .png files or anything like that. For instance, if you want to have a button which has a gradient, and then change it when the button is pressed:
Step 1:
Create default button gradient (drawable/default_button.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>
Step 2: Create default button pressed gradient (drawable/default_button_pressed.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>
Step 3: Create selector (drawable/default_button_selector.xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/default_button_pressed" />
<item android:drawable="#drawable/default_button" />
</selector>
Step 4 (optional): Create style for the button (values/style.xml):
<resources>
<style name="DefaultButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/default_button_selector</item>
</style>
</resources>
Step 5: use the button (layout/main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<button style="#style/DefaultButton" />
</RelativeLayout>
As you can see, it's not particularly difficult to do.

Without having to create multiple images (pressed, normal etc) and even don't have to create selector. Use setOnTouchListener rather than setOnClickListener. The below code will give you the grey overlay on the clicked item.
((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageButton view = (ImageButton ) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
// Your action here on button click
case MotionEvent.ACTION_CANCEL: {
ImageButton view = (ImageButton) v;
view.getBackground().clearColorFilter();
view.invalidate();
break;
}
}
return true;
}
});

You can simply use android:foreground for your View to achieve clickable effect:
android:foreground="?android:attr/selectableItemBackground"
For use with dark theme add also theme to your layout (to make clickable effect clear):
android:theme="#android:style/ThemeOverlay.Material.Dark"

In order not to have to set several drawable for each button I set the color filter attribute of the image button in an on touch listener.
See code here in another post:
https://stackoverflow.com/a/14278790/891479

I managed to do this!!
First you declare buttonStyle.xml in drawable folder.
<?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="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
<item android:drawable="#drawable/button_normal" />
</selector>
Then you create button_pressed.xml in drawable folder.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/semiTransparentGnfrBlueColor" />
<stroke android:width="10dp" android:color="#android:color/transparent" />
</shape>
After that, you create button_normal.xml in drawable folder.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="3"
android:shape="rectangle">
<solid android:color="#color/gnfrBlueColor"/>
<stroke android:width="10dp" android:color="#android:color/transparent" />
</shape>
You can use default colors but if you want to do it like me, you need to have a file named colors.xml in values folder and this values inside:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="gnfrBlueColor" type="color">#2A3748</item>
<item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item>
<integer-array name="androidcolors">
<item>#color/gnfrBlueColor</item>
<item>#color/semiTransparentGnfrBlueColor</item>
</integer-array>
</resources>
Finally you set this to your button or whatever:
savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);
NOTICE THAT I set a stroke with color transparent because when you set backgroundresource your button becomes bigger and with this trick it remains original.
This is the only way I could archieve this programmatically.
If you want to do this by XML:
<Button
android:text="ENTRAR"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/buttonstyle"
android:textColor="#color/white"
android:textSize="18sp"
android:id="#+id/button1" />

Related

Change Tint Image Button after pressed

I am trying to create an ImageButton able to change the icon color after pressing. I want to do that in my XML file. I have the following code:
...
<ImageButton
android:id="#+id/stop_button"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/stop_icon"
android:background="#drawable/stop_button"
android:tint="#color/ship_cove"
app:layout_constraintBottom_toTopOf="#id/bottom_icon_guideline"
app:layout_constraintLeft_toRightOf="#id/start_icon_guideline"
app:layout_constraintRight_toLeftOf="#id/end_icon_guideline"
app:layout_constraintTop_toBottomOf="#id/mediatop_icon_guideline" />
And here is my #drawable/stop_button.xml file content:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/cornflower_blue">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/oxford_blue" />
<corners android:radius="8dp" />
</shape>
</item>
</ripple>
As I stated before, my purpose is to change my stop_icon tint after pressing my stop_button, but I do not know how I can do that. Can anyone help me?
Note: if my button is not pressed, I want my stop_icon with the color #color/ship_cove. If it is, I want the color #color/mirage.
Instead of color create a color selector with a pressed state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="pressed_color" android:state_pressed="true" />
<item android:color="default_color" />
</selector>
And use this selector for the color of the icon. The rest should be taken by the system.

How can i make an "Gmail" like header buttons? (Pic attached)

Does any one knows how can i skin an android button to look like this:
a busy cat http://www.11sheep.com/temp/picForSO.jpg
Thanks in advance,
Lior
The following layout files will product a button with 5px radius, of course u input your own color and change the solid color to gradient to match ur screenshots, then on the button change the text colour to white or something.. I dont have time for examples now.. good luck though.
and lastly you have to apply them as background to your button like this
<Button
android:id="#+id/btnLoveThisOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Love me love u too!"
android:background="#drawable/button_background" <!-- Yes look at me -->
/>
button_background_normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/button_background_normal_color"/> <!-- Change this to your own colour -->
<corners android:radius="5px"/>
<stroke android:width="1dp" android:color="#20ffffff"/>
</shape>
button_background_pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/button_background_pressed_color"/> <!-- Change this to your own colour -->
<corners android:radius="5px"/>
</shape>
button_background.xml
<?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="#drawable/button_background_normal" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:drawable="#drawable/button_background_normal" />
</selector>
I know two ways to make it:
a Button or TextView with an image having that engraving rectangle, for stretch, use 9.patch :)
a Button or TextView with transparent background and a selector drawing the shape border
However, I don't know exactly how they did as shown in your picture.

how to change background image of button when clicked/focused?

I want to change the background image of a button when clicked or focused.
This is my code:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
Is this code right? Does it calls a button on its event?
you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="#drawable/your_imagename_while_pressed" />
<item android:drawable="#drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)
Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.
button_background_selector.xml
<?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="#drawable/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/your_hover_image"/>
<item android:drawable="#drawable/your_simple_image" />
</selector>
Now set the above file in button's background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/grey_text"
android:background="#drawable/button_background_selector"/>
Sorry this is wrong.
For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.
For example: button_selector.xml (define this file inside the drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
<!-- IF you want image instead of color then write
android:drawable="#drawable/your_image" inside the <item> tag -->
And apply it as:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/button_selector.xml" />
use this code
create xml file in drawable folder name:button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/buutton_pressed" />
<item
android:drawable="#drawable/button_image" />
</selector>
and in button xml file
android:background="#drawable/button"
To change the button background we can follow 2 methods
In the button OnClick, just add this code:
public void onClick(View v) {
if(v == buttonName) {
buttonName.setBackgroundDrawable
(getResources().getDrawable(R.drawable.imageName_selected));
}
}
2.Create button_background.xml in the drawable folder.(using xml)
res -> drawable -> button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/tabs_selected" /> <!-- selected-->
<item android:state_pressed="true"
android:drawable="#drawable/tabs_selected" /> <!-- pressed-->
<item android:drawable="#drawable/tabs_selected"/>
</selector>
Now set the above file in button's background file.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_background"/>
(or)
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
2nd method is better for setting the background fd button
You just need to set background and give previous.xml file in background of button in your layout file.
<Button
android:id="#+id/button1"
android:background="#drawable/previous"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
and done.Edit Following is previous.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/onclick" android:state_selected="true"></item>
<item android:drawable="#drawable/onclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/normal"></item>
You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#81ba73" />
<corners android:radius="6dp" />
</shape>
<ripple android:color="#c62828"/>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="#788e73" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#add8a3" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).
Create a file in drawable play_pause.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/pause" />
<item android:state_selected="false"
android:drawable="#drawable/play" />
<!-- default -->
</selector>
In xml file add this below code
<ImageView
android:id="#+id/iv_play"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#drawable/pause_button"
android:gravity="center"
android:scaleType="fitXY" />
In java file add this below code
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);
and also add this
iv_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iv_play.setSelected(!iv_play.isSelected());
if (iv_play.isSelected()) {
((GifDrawable) gif_1.getDrawable()).start();
((GifDrawable) gif_2.getDrawable()).start();
} else {
iv_play.setSelected(false);
((GifDrawable) gif_1.getDrawable()).stop();
((GifDrawable) gif_2.getDrawable()).stop();
}
}
});
use <androidx.appcompat.widget.AppCompatButton /> instead of 'Button'

How to change the background color of the toggle button on Android

I tried to change the background color of the toggle button using XML file as white color, but the toggle button is totally damaged. It looks like all the button was covered with white.
There is no indication of ON or OFF on the toggle button when I have changed the color of the toggle button to white. Is there is another way to change the background which will not damage the indication of the toggle button?
<ToggleButton android:id="#+id/togglebutton"
android:layout_width="100px"
android:layout_height="46px"
android:background="#ffffff"
android:layout_above ="#+id/save"
android:textOn="DAY"
android:textOff="NIGHT" />
This is how my XML code look for the toggle button.
Yes, there is a way to change the background as you wish, but you have to use a selector like this as background:
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/some_image" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/some_other_image" />
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/some_image1" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/other_image" />
</selector>
For #Drawable, etc. (you can use a color or make a gradient. Check this for more information about gradients.
Follow this way to make your ToogleButton have background color red when On and green when OFF
First, create tooglebutton_selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/togglebutton_on"
android:state_checked="true" />
<item android:drawable="#drawable/togglebutton_off"
android:state_checked="false"
/>
</selector>
Second, create togglebutton_on.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#ff0000" /> // red color
</shape>
Third, create togglebutton_off.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#00FF00" /> // green color
</shape>
Finally, in your ToggleButton
<ToggleButton
android:id="#+id/btnMon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector
/>
When you decompile your SystemUI.apk, you should go to the following file: SystemUI/res/values/colors.xml
Then change the following line:
#ff000000
#ffffffff
#80000000
#ffadc1d6
#ffffffff
#ffe6e6e6

How to Change color of Button in Android when Clicked?

I am working on Android Application. I want to have 4 buttons to be placed horizontally at the bottom of the screen. In these 4 buttons 2 buttons are having images on them. The border of the buttons should be black color and the border should be as thin as possible. When I click the button, I want the background of the button should be changed to blue color without the color of border to be changed and should be remained in that color for some time. How can I achieve this scenario in Android?
One approach is to create an XML file like this in drawable, called whatever.xml:
<?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="true"
android:drawable="#drawable/bgalt" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/bgalt" />
<item android:drawable="#drawable/bgnorm" />
</selector>
bgalt and bgnormare PNG images in drawable.
If you create the buttons programatically in your activity, you can set the background with:
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
If you set your buttons' style with an XML, you would do something like:
<Button
android:id="#+id/mybutton"
android:background="#drawable/watever" />
And finally a link to a tutorial.
Save this code in drawable folder with "bg_button.xml" and call "#drawable/bg_button" as background of button in your xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#004F81" />
<stroke
android:width="1dp"
android:color="#222222" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#89cbee"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4aa5d4" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Try This
final Button button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.RED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
button.setBackgroundColor(Color.BLUE);
}
return false;
}
});
Refer this,
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#00a3e2" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#5a97f5" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape></item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#478df9"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
</selector>
If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.
<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/pressed" /> <!--pressed -->
<item android:drawable="#drawable/normal" /> <!-- Normal -->
</selector>
<!-- Now this file should be in a drawable folder and use this
single line code in button code to get all the properties of this xml file -->
<Button
android:id="#+id/street_btn"
android:layout_width="wrap_content"
android:background="#drawable/layout_a" > <!-- your required code -->
</Button>
1-make 1 shape for Button
right click on drawable nd new drawable resource file .
change Root element to shape and make your shape.
enter image description here
2-now make 1 copy from your shape and change name and change solid color.
enter image description here
3-right click on drawable and new drawable resource file
just set root element to selector.
go to file and set "state_pressed"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="#drawable/YourShape1"/>
<item android:state_pressed="false" android:drawable="#drawable/YourShape2"/>
</selector>
4-the end go to xml layout and set your Button background "your selector"
(sorry for my english weak)
Try this......
First create an xml file named button_pressed.xml
These are it's contents.
<?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="#drawable/icon_1" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/icon_1_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/icon_1_press" />
<item android:drawable="#drawable/icon_1" />
</selector>
Noe try this on your button.
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName());
button.setImageResource(imgID);
button_pressed.xml should be in the drawable folder.
icon_1_press and icon_1 are two images for button press and normal focus.
you can try this code to solve your problem
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="#drawable/login" /> <!-- default -->
</selector>
write this code in your drawable make a new resource and name it what you want and then write the name of this drwable in the button same as we refer to image src in android
I am use this code (with ripple effect):
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#color/color_gray">
<item android:id="#android:id/mask">
<color android:color="#color/color_gray" />
</item></ripple>
public void onPressed(Button button, int drawable) {
if (!isPressed) {
button.setBackgroundResource(R.drawable.bg_circle);
isPressed = true;
} else {
button.setBackgroundResource(drawable);
isPressed = false;
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.circle1:
onPressed(circle1, R.drawable.bg_circle_gradient);
break;
case R.id.circle2:
onPressed(circle2, R.drawable.bg_circle2_gradient);
break;
case R.id.circle3:
onPressed(circle3, R.drawable.bg_circle_gradient3);
break;
case R.id.circle4:
onPressed(circle4, R.drawable.bg_circle4_gradient);
break;
case R.id.circle5:
onPressed(circle5, R.drawable.bg_circle5_gradient);
break;
case R.id.circle6:
onPressed(circle6, R.drawable.bg_circle_gradient);
break;
case R.id.circle7:
onPressed(circle7, R.drawable.bg_circle4_gradient);
break;
}
please try this, in this code i m trying to change the background of button on button click this works fine.
To deal properly for how long you want to have your button stay in your other color I would advise this version:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable));
break;
case MotionEvent.ACTION_UP:
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
((Activity) (getContext())).runOnUiThread(new Runnable() {
#Override
public void run() {
button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable));
}
});
}
}, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION);
break;
default:
}
return false;
}
});
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION indicates after how much time [ms] the button will reset to the previous state (however you might want to use some boolean to check that the button hadn't been used in between, depending on what you want to achieve...).
Even using some of the comments above this took way longer to work out that should be necessary. Hopefully this example helps someone else.
Create a radio_button.xml in the drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<!-- An element which allows two drawable items to be listed.-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/radio_button_checked" /> <!--pressed -->
<item android:drawable="#drawable/radio_button_unchecked" /> <!-- Normal -->
</selector>
Then in the xml for the fragment should look something like
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:button="#drawable/radio_button"
android:paddingLeft="10dp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#drawable/radio_button" />
</RadioGroup>
</LinearLayout>
</layout>
Just add 1 line in XML for the click item
android:background="?android:attr/selectableItemBackground"
hai the most easiest way is this:
add this code to mainactivity.java
public void start(View view) {
stop.setBackgroundResource(R.color.red);
start.setBackgroundResource(R.color.yellow);
}
public void stop(View view) {
stop.setBackgroundResource(R.color.yellow);
start.setBackgroundResource(R.color.red);
}
and then in your activity main
<button android:id="#+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click">
</button><button android:id="#+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click">
or follow along this tutorial

Categories

Resources