Selector does not work for a button - android

Consider:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/btn_led_on" ></item>
<item android:state_selected="true" android:drawable="#drawable/btn_led_on" ></item>
<item android:drawable="#drawable/btn_led_off"></item>
</selector>
Button in layout
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mybutton"
/>
want to change background on click, but it doesn't work.

Try:
android:state_pressed="true"
Something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_led_on" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_led_off" android:state_pressed="false"/>
</selector>
Edit
For your requirement use this
boolean isButtonClicked=false;
Button button = (Button) findViewById(R.id.main_button);
button.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
#Override
public void onClick(View v) {
if (v.getId() == R.id.main_button) {
isButtonClicked = !isButtonClicked; // toggle the boolean flag
v.setBackgroundResource(isButtonClicked ? R.drawable.btn_led_on : R.drawable.btn_led_off);
}
}
});

Your selector should be like this:
<?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>

Related

Android how to Change Default background color in Button click?

I need to show different color on button after click because user need to know button is
Click.
I don't understand how to do this?
Give me suggestion. button click shoud be programatically, No Need to create XML for this
//XML file saved at res/drawable/button_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
in JAVA
am create like this
Button Settings_Button = new Button(this) ;
Settings_Button.setClickable(true);
//Settings_Button.setBackgroundResource(R.drawable.selector);
Settings_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent newIntent = new Intent(activity.getBaseContext(), ProfileSettingActivity.class);
// activity.startActivity(newIntent);
}
});
BUt this is not working
EDIT : How to change Background color when click button in programatically.
try this way,hope this will help you...
button_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/button_pressed" android:state_pressed="true"></item>
<item android:drawable="#color/button_pressed" android:state_focused="true"></item>
<item android:drawable="#color/button_default" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#color/button_pressed" android:state_enabled="false"></item>
</selector>
color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="button_pressed">#ffff0000</color>
<color name="button_default">#ff0000ff</color>
</resources>
ACTIVITY code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.button_selector);
btn.setText("Custom Button");
addContentView(btn,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
Create selector drawable according all state.
Below is sample example.
btn_green_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/btn_green_unselected" />
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/btn_green_selected" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/btn_green_selected" />
<!-- Pressed state -->
<item
android:state_pressed="true"
android:drawable="#drawable/btn_green_selected" />
</selector>
btn_green_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00B2B2"/>
</shape>
btn_green_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#8ED3CD"/>
</shape>
now applied btn_green_selector.xml in coding.
Settings_Button.setBackgroundResource(R.drawable.btn_green_selector);
you can also use Images instead of Shape.
Try Like this,
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="changeColor" />
</LinearLayout>
ChangeBackgroundActivity.java
public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
Button blueButton;
LinearLayout myLO;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLO=(LinearLayout)findViewById(R.id.myLayout);
blueButton=(Button)findViewById(R.id.button1);
blueButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myLO.setBackgroundColor(Color.BLUE);
}
});
}
}
You can use this line in buttons onclicklistener,button.setBackgroundColor(Color.WHITE);

Change the state of button to pressed

What I am trying to achieve: When the button is pressed for the first time, it should get highlighted/ activated / pressed. On the second click on the button it should get unactivated/ not pressed.
and later I want to check that if the button isPressed, do something.
What I tried:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.day_button:
if (v.isPressed() == true) {
v.setPressed(false);
} else if (v.isPressed() == false) {
v.setPressed(true);
}
return true;
I tried this with day_but.isPressed == true also.
you can try with this way change the background state of button when click set the images as a background
Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});
YOu can use toggle button. For more info http://developer.android.com/guide/topics/ui/controls/togglebutton.html
<ToggleButton
android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"/>
or there is another approach you can follow. you can apply style in your button.
<Button
android:id="#+id/button1"
style="#button_style/<StyleName>"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Create a button_style.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>
You can also define a Selector for your Button to customize highlighting,so you create a xml file then :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_enabled="true"
android:state_focused="false" android:drawable="#drawable/enable_notpressed" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="#drawable/enable_pressed" />
<item android:state_pressed="false" android:state_enabled="false"
android:state_focused="false" android:drawable="#drawable/disabled" />
</selector>
then assign it to your button as background param.
use selector like:
make a new xml file in drawable folder and paste this code.and change values accordingly.
<padding
android:left="10dp"
android:top="2dp"
android:right="10dp"
android:bottom="2dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#003040"
android:endColor="#003040"
android:angle="180" />
<corners
android:radius="8dp" />
<padding
android:left="10dp"
android:top="2dp"
android:right="10dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
!!happy coding!!
XML CODE:
<ToggleButton
android:id="#+id/btspeaker"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_marginRight="20dp"
android:background="#drawable/bgspeaker"
android:button="#null"
android:textOff=""
android:textOn="" />
In the drawable:
bgspeaker.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/speaker_btn_select" android:state_checked="true"/>
<item android:drawable="#drawable/speaker_btn" android:state_checked="false"/>
<item android:drawable="#drawable/speaker_btn"></item>
</selector>
In the activity:
if (v.getId() == R.id.btspeaker) {
if (btspeaker.isChecked()) {
Toast.makeText(context, "Pressed/Selected",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,"UnSelected",
Toast.LENGTH_LONG).show();
}
}

Setting background programmatically overrides drawable

I have a ListView and I have a Drawable for each item in the ListView to highlight each row when it's selected/pressed. I also have a custom adapter where I'm programatically setting the background color of each row (I want to have alternating background colors). This is a new feature I added and before adding the code the rows would highlight blue, but after they do not highlight. Not sure how to fix it. Here is what I have:
ListView item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:background="#drawable/app_selector"
>
<TextView
android:id="#+id/text"
style="#style/ListingTitle"
android:layout_alignParentTop="true"
android:paddingBottom="2dip"
/>
</RelativeLayout>
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="#color/light_blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
</selector>
Fragment
public class ListingFragment extends SherlockListFragment
{
public void onActivityCreated(final Bundle icicle)
{
ListView lv = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
}
#Override
public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {
}
}
}
/* ADAPTER */
private class CustomAdapter extends BaseAdapter
{
#Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, parent, false);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
if (position % 2 == 0)
convertView.setBackgroundColor(Color.GRAY);
else
convertView.setBackgroundColor(Color.TRANSPARENT);
return(convertView);
}
}
}
Expanding on dum's answer, you don't need to do all that work in code.
Drawable A
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/light_blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
</selector>
Drawable B
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/light_blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="#color/light_blue" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/gray" />
</selector>
In your Adapter:
if (position % 2 == 0) {
convertView.setBackgroundResource(R.drawable.A);
} else {
convertView.setBackgroundResource(R.drawable.B);
}
instead of changing the drawable with plain color, first create a new drawable programmatically and set the states just like in your xml and then set colors for each state. then try replacing your background with your programmatically created drwable.

android button setPressed after onClick

yesterday I noticed the possibility to integrate Fragments in older API Levels through the Compatibility package, but thats not really essential for the question. :)
I have a Button with an OnClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
button.setPressed(true);
}
});
Because of the actual clicking, it is shown as pressed and after releasing the click, the button state is not pressed and stays that way.
Is there a simple way that keeps the button state pressed after releasing?
First thing I can think of would be some sort of timer, but that seems unreasonable.
Just to note this is because Android is changing the setPressed both before and after your onClickEvent, so changing it yourself has no effect. Another way to get around this is to use the onTouchEvent.
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if (event.getAction() == MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if (event.getAction() != MotionEvent.ACTION_UP) return false;
doSomething();
button.setPressed(true);
return true;
}
});
Use ToggleButton instead of Button.
<ToggleButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/topping_selector"
android:checked="false"
android:textOff="Topping2"
android:textOn="Topping2" />
topping_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/btn_topping_on" />
<item android:state_checked="false"
android:drawable="#drawable/btn_topping_off" />
</selector>
You can keep the button states in xml file under drawable folder, then used as background for button.
For example:
android:background="#drawable/buttonstate"
buttonstate.xml
<?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/back_focused" />
<item android:drawable="#drawable/back" /> <!-- default -->
</selector>
Better solution:
In xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true">
<bitmap android:src="#drawable/image_selected"/>
</item>
<item>
<bitmap android:src="#drawable/image_not_selected"/>
</item>
</selector>
And in java:
#Override
public void onClick(View v) {
v.setActivated(!v.isActivated());
}
You can use android.os.Handler class. Ugly, but works also:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
new Handler().post(new Runnable() {
#Override
public void run() {
button.setPressed(true);
}
});
}
});
Another solution is to extend Button and override setPressed(boolean pressed) method so you can handle platform calls from the onClickEvent using a flag, for example changing the boolean pressed parameter depending on your needs.
public class MyButton extends Button {
boolean isSelected = false; //this is your flag
#Override
public void setPressed(boolean pressed) {
if(isSelected) {
//here you change the parameter so it stays pressed
super.setPressed(true);
return;
}
super.setPressed(pressed);
}
public void setIsSelected(boolean selected) {
this.isSelected = selected;
}
public MyButton(Context context) {
super(context);
}
}
This is the solution I used, It also works on android 7.0 at the moment.
YourActivity.java
public void onStandbyStart(String message) {
startStandbyBtn.setActivated(true);
}
public void onBackOnline(String message) {
startStandbyBtn.setActivated(false);
}
YourActivityLayout
<Button
...
style="#style/generic_btn_style"
... />
values/styles.xml
<style name="generic_btn_style" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:background">#drawable/generic_btn</item>
<item name="android:textColor">#color/selector_white_black</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
drawable/generic_btn.xml
This selector chooses the button background. I use the pressed as the activated.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/generic_btn_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/generic_btn_pressed" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="#drawable/generic_btn_pressed" android:state_activated="true" />
<item android:drawable="#drawable/generic_btn_focused" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="#drawable/generic_btn_enabled" android:state_enabled="true" />
</selector>
color/selector_black_white
Here I set the text color. In my case, I need to pick the textcolor black when pressed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fff" android:state_pressed="true" /> <!-- pressed -->
<item android:color="#fff" android:state_activated="true" /> <!-- pressed -->
<item android:color="#000" /> <!-- default -->
</selector>

Android how to make button text bold when pressed or focussed

I want to change the text inside a button to be bold when the button is highlighted or pressed. I currently use a xml file to define the button and use the XML to change how it looks when pressed but I would like to do this without using an image.
<?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/reset_hover" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/reset_hover" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/reset_hover" />
<item android:drawable="#drawable/reset" />
</selector>
I tried using something like the following, but it doesn't seem to ever get called.
final Button btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(this);
btn_reset.setOn(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){btn_reset.setTypeface(null, Typeface.BOLD);}
else{btn_reset.setTypeface(null, Typeface.NORMAL);}
}
});
You could try putting the bold code inside the click event for the button:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Set bold on click
button.setTypeface(null, Typeface.BOLD);
}
});
Attention! See update below
You could create your own style.xml in which you define the text style. In your selector you can reference to the style.
style.xml
<style name="myStyle">
<item name="android:textSize">9px</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:textColor">#fff</item>
<item name="android:textStyle">bold</item>
</style>
And in your selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
style="#style/myStyle" /> </selector>
Update 2020: My answer is more than 10 years old. It doesn´t work anymore!
Styles are not allowed in selectors. Reference
And to make the text bold, use this code:
btn_reset.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn_reset.setTypeface(Typeface.DEFAULT_BOLD);
break;
// When the user releases the Button
case MotionEvent.ACTION_UP:
btn_reset.setTypeface(Typeface.DEFAULT);
break;
}
return false;
}
});

Categories

Resources