Change Button Image on Button click - android

Selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_call_active_big" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#drawable/btn_call_normal_big"/>
</selector>
And on button background
<ImageButton
android:id="#+id/btnPhone"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/button_background"
android:src="#drawable/btn_call_normal_big" />
Please advice on above . where i am doing wrong

you have to use the selector in place of the png file. Change
android:src="#drawable/btn_call_normal_big"
with
android:src="#drawable/nome_of_selector"

boolean isPressed=false
button.setOnClickListener(buttonListener);
OnClickListener buttonListener= new OnClickListener() {
#Override
public void onClick(View v) {
if(isPressed){
button.setBackgroundResource(R.drawable.icon1);
}else{
button.setBackgroundResource(R.drawable.icon2);
}
isPressed=!isPressed;
}
};
Hope it will help you.
Also check below link
http://www.mkyong.com/android/android-imagebutton-selector-example/
this also might help you

Use this:
<ImageButton
android:id="#+id/btnPhone"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:src="#drawable/selector" />

Remove src attribute and change the background attribute value to your selector file name.
<ImageButton
android:id="#+id/btnPhone"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/Selector"
/>

Related

How do I remove this rectangular of image button

There is a default rectangular background in my Image Button. I want to remove this
This is related codes:
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:src="#drawable/off1"/>
Just try this
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:background="#drawable/off1"/>
just change your android:src to android:background
You will want to set the background of the imageButton to be something invisible. I do this by making the following XML drawable and setting it to the ImageButton's background. This will let the button still show a color when it is clicked, but blank otherwise.
blank_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/some_color" />
<item android:drawable="#android:color/transparent" />
</selector>
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:src="#drawable/off1"
android:background="#drawable/blank_selector" />
best solution is
android:background="#null"

Android: How to change the color of a button?

I'm using Eclipse ADT and I need to know how to change the default color of a form widgets button from the default gray to other color, of course using the xml.
Or you can use 9 patch images.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="#drawable/patchdisable" android:state_enabled="false"/>
And in Xml
<Button
android:id="#+id/button_register"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/patch"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="REGISTER"
android:textColor="#ffffff"
android:textStyle="bold" />
In your xml, you can set the background color by using android:background="#FF0000"
<Button
android:id="#+id/loadimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image"
android:background="#FFFF00"/>
you can set any background color of your choice...:)
Using code in you activity class:
Button btn = (Button)findViewById(R.id.button);
btn.setBackgroundColor(Color.BLUE);
btn.setBackgroundColor(Color.rgb(0, 0, 255));
btn.setBackgroundColor(Color.parseColor("blue"));
Doing it straight from your XML:
Add this property to your Button in your XML file:
android:background="#800080"
#800080 is purple.
Here is answer my friend
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/grey"/>

Making buttons that look like tabs in android

I want this kind of look. They don't seem like tabs so I believe they are buttons.
I am able to acheive the similar type with toggle button by keeping a selector in drawable.
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/custom_selector"
android:textOn=" Button1 Button2"
android:textOff=" Button1 Button2" />
But they have only two values and by clicking on the selected tab gets the other selected and the present one unselected. But I want exactly like tab. So can someone please help me achieve it? Thanks in advance.
You have to create selector for all button and use RadioGroup with selector background and null button..
Follow #Andru answer for create Selector..
Below is RadioGroup code.
<RadioGroup
android:id="#+id/rdogrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#drawable/btn1Selector"
android:button="#null"
android:checked="true"
android:gravity="center" />
<RadioButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#drawable/btn2Selector"
android:button="#null"
android:gravity="center" />
<RadioButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#drawable/btn3Selector"
android:button="#null"
android:gravity="center" />
<RadioButton
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#drawable/btn4Selector"
android:button="#null"
android:gravity="center" />
</RadioGroup>
here is sample code for btn1Selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn1_selected" android:state_checked="true" />
<item android:drawable="#drawable/btn1_normal" android:state_checked="false"/>
</selector>
you can do something like this, instead of toggle button use normal buttons.
if "Data" button clicked do like this
data(View v)
{
databtn.setBackgroundResource(R.drawable.image1);
w_chartbtn.setBackgroundResource(R.drawable.image2);
H_chartbtn.setBackgroundResource(R.drawable.image2);
}
if "H-chart" button clicked
H_chart(View v)
{
databtn.setBackgroundResource(R.drawable.image2);
w_chartbtn.setBackgroundResource(R.drawable.image2);
H_chartbtn.setBackgroundResource(R.drawable.image1);
}
Use simple button instead of toogle button. And set background like this:
I am giving example for 1 button:
android:background="#drawable/data_button_select_state"
And add a xml file in your 'drawable' folder named data_button_select_state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/data_image_selected_state"
android:state_selected="true" />
<item android:drawable="#drawable/data_image_selected_state"
android:state_pressed="true" />
<item android:drawable="#drawable/data_image_without_selected" />
</selector>
Now add code in java file like this:
when data button clicked:
data_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
data_button.setActivated(true);
H_chart_button.setActivated(false);
w_chart_button.setActivated(false);
hc_chart_button.setActivated(false);
}
});
change other button like this as well. This will might help you...

No event received with onClickListener when ImageView has flag clickable=true

I have RelativeLayout with android:clickable="true" to change image color from grey to white when button stay pressed (otherwise if android:clickable="false" it doesn't work):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/contacts"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true" <!-- here clickable = true -->
android:contentDescription="#string/content_description_contacts"
android:scaleType="fitXY"
android:src="#drawable/contacts" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignBottom="#id/image"
android:paddingBottom="10dp"
android:textColor="#drawable/text_color"
android:text="#string/button_contacts"
android:textSize="12sp" />
</RelativeLayout>
and seems like:
My contacts Selector seems:
<?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/contacts_over" />
<item android:state_selected="true"
android:drawable="#drawable/contacts_selected" />
<item
android:drawable="#drawable/contacts_default" />
</selector>
As you can see I have 3 images: by default, selected and pressed.
According to documentation, after setting android:clickable="true" to ImageView I stopped to get onClickListener event.
contacts = (RelativeLayout) findViewById(R.id.contacts);
contacts.setOnClickListener(this);
So I have
or set clickable="false" and "state_pressed" doesn't work
or set clickable="true" and onClickListener doesn't work (no event received)
What should I do, any workaround?
Thanks,
Try to set onClickListener to your ImageView instead of RelativeLayout.
And this may interest you too:
How to pass the onClick event to its parent on Android?
Work well, if you set Listner to your Imageview instead Layout.
Try using ImageButton widget instead of ImageView.

Adding functionality to ToggleButton,

I am making a graphic intensive application on Android. I have to make a button for enabling and disabling sound of the application, which is pretty common amongst apps. I actually cannot find the correct way to do it. I have managed to switch the image through xml files but am still lost about how to add a functionality to those.
code is below ..
Main Layout
<Button android:id="#+id/InfoButton" android:background="#drawable/infoiconlow" android:layout_gravity="top|left" android:layout_height="40dp" android:layout_width="30dp" android:layout_marginTop="2dp" android:layout_marginLeft="125dp"></Button>
<Button android:layout_gravity="top|right" android:layout_width="wrap_content" android:id="#+id/ForwardButton" android:layout_height="wrap_content" android:background="#drawable/forwardbuttonlow" android:layout_marginRight="10dp" android:layout_marginTop="-3dp"></Button>
<ImageView android:src="#drawable/selectthescenetitle" android:id="#+id/imageView3" android:layout_gravity="top|center" android:layout_marginLeft="15dp" android:layout_marginTop="-3dp" android:layout_width="219dp" android:layout_height="wrap_content"></ImageView>
<ToggleButton android:id="#+id/MusicButton" android:background="#drawable/shufflebutton" android:textOn="" android:textOff="" android:layout_gravity="top|left" android:layout_width="30dp" android:layout_height="40dp" android:layout_marginTop="2dp" android:layout_marginLeft="84dp" android:clickable="true">
</ToggleButton>
sufflebutton.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shufflebuttonimage" />
</layer-list>
shufflebuttonimage.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/musiconiconlow" android:state_checked="false" />
<item android:drawable="#drawable/musicofficonlow"/>
</selector>
The image toggles but if i go back and forth, the state resumes its default, and i am unable to add a functionality to the toggle e.g. enabling and disabling sound. Can anyone please help me out in solving this problems, its driving me crazy ..
Thanks :)
I usually implement some code like this:
private void initialise(final Context context)
{
final ToggleButton music = (ToggleButton)findViewById(R.id.MusicButton);
music.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
music.setChecked(true);
// Do other stuff like play the music.
}
});
}
For full functionality you will probably have to checked the button's checked state before doing anything.

Categories

Resources