I have a button background that change drawable if the state is pressed or not. I'm able to change text color and background like below but I would like to change text size. How can I do it .XML side?
drawable/button_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_pressed" android:state_selected="true" />
<item android:drawable="#drawable/toggle_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/toggle_unpressed" />
</selector>
res/color/src_text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false" android:color="#color/blue2"/>
<item android:state_pressed="true" android:color="#color/color_concrete"/>
<item android:state_pressed="false" android:state_selected="true"
android:color="#color/color_concrete"/>
<item android:color="#color/blue2" />
</selector>
button:
<Button
android:id="#+id/playButton"
android:layout_width="200dp"
android:layout_height="70dp"
android:background="#drawable/button_selector"
android:onClick="#{(v)->mainViewModel.playButtonClick(v)}"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="#string/play"
android:textColor="#color/src_text"
android:textSize="40sp"
android:visibility="#{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>
You can create subclass of Button and override onTouch method
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// button pressed, change textSize
return true;
case MotionEvent.ACTION_UP:
// button released, revert textSize
return true;
}
return false;
}
Since you're using databinding you can probably create a BindingAdapter like following:
#JvmStatic
#BindingAdapter("textSize")
fun bindTextSize(button: Button, size: Int) {
if(button.isPressed())
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
Also keep a MutableLiveData field in the viewmodel (size) and change the value according to your need.
<Button
textSize="#{mainViewModel.size}
android:id="#+id/playButton"
android:layout_width="200dp"
android:layout_height="70dp"
android:background="#drawable/button_selector"
android:onClick="#{(v)->mainViewModel.playButtonClick(v)}"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="#string/play"
android:textColor="#color/src_text"
android:textSize="40sp"
android:visibility="#{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>
Related
i would like to change the background color in ontouchListener, i can change textColor but the background color remains unchanged: here is my code:
Button usd=findViewById(R.id.button_divide);
Button cdf=findViewById(R.id.button_multiply);
usd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("currency","USD");
editor.apply();
usd.setBackgroundColor(Color.parseColor("#515de1"));
usd.setTextColor(Color.WHITE);
cdf.setBackgroundColor(Color.WHITE);
cdf.setTextColor(Color.parseColor("#515de1"));
return false;
}
});
I's better to use the selector instead of code
<Button
android:id="#+id/myButton"
android:background="#drawable/selector_my_button"
android:layout_width="100dp"
android:layout_height="36dp"
android:text="My Button" />
and the selector_my_button.xml is like this
<?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/white"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="#color/blue"/> <!-- focused -->
<item android:drawable="#color/black"/> <!-- default -->
</selector>
Originally I had my button connected to a method through the XML like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_button_green"
android:onClick="start_process"
android:text="#string/button_send"
android:textSize="50sp" />
Then I decided to add an onTouchListener so that I could handle ACTION_UP events:
private OnTouchListener myListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//start recording
System.out.println("DOWN");
start_process();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("UP");
}
return true;
}
};
The listener works great, but I've noticed that the states for my button have disappeared. The button had different colors for if it had been pressed, but this no longer happens now that the button is interacting through the listener. Here is what is defined in #drawable/custom_button_green:
<?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/btn_default_pressed" />
<item android:state_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_green" />
<item android:state_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_green" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
Can anyone help me figure out why the button no longer follows these states?
Thanks!
I think the onTouchmethod of your listener should return false, otherwise you just override the "normal" behaviour of updating drawable on touch events.
I am using ListView and for every row I have row_item.xml and I inflate that in code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/chk"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtChoice"
android:textColor="#FF0000"
android:text="TEST"
android:layout_toRightOf="#id/chk"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
How to change that checkBox use another custom_1 image when is checked and another custom_2 image when is unchecked ?
Drawable customdrawablecheckbox.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/unchecked_drawable" />
<item android:state_checked="true" android:drawable="#drawable/checked_drawable" />
<item android:drawable="#drawable/unchecked_drawable" /> <!-- default state -->
</selector>
yourcheckbox xml:
<CheckBox
android:id="#+id/chk"
android:button="#drawable/customdrawablecheckbox"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
checkbox is a button, so you can provide your own drawable with check uncheck state and it as checkbox background. For instance
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/yourdrawable1" />
<item android:state_checked="true" android:drawable="#drawable/yourdrawable2" />
<item android:drawable="#drawable/yourdrawable1" /> <!-- default -->
</selector>
and put this in a file.xml in your drawable folder. In your checkbox:
<CheckBox
android:button="#drawable/file"
android:id="#+id/chk"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Its pretty easy :)
First you need to create a CustomCheckBox class which will extend CheckBox and override the onDraw(Canvas canvas) method:
public class CustomCheckBox extends CheckBox {
private final Drawable buttonDrawable;
public CustomCheckBox(Context context, AttributeSet set) {
super(context, set);
buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box);
try {
setButtonDrawable(android.R.color.transparent);
} catch (Exception e) {
// DO NOTHING
}
setPadding(10, 5, 50, 5);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
buttonDrawable.setState(getDrawableState());
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
if (buttonDrawable != null) {
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int buttonWidth = buttonDrawable.getIntrinsicWidth();
int buttonLeft = getWidth() - buttonWidth - 5;
buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
buttonDrawable.draw(canvas);
}
}
}
Also create your selector named custom_check_box in your drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_check_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_check_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_check_on" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_check_off" />
<item android:state_checked="false" android:drawable="#drawable/btn_check_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_check_on" />
</selector>
And use your custom icons/imgs in the XML above for all three states (focused/pressed/default)
Use the custom component in your XML like this :
<*package + class path*.CustomCheckBox // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder
android:text="#string/text"
android:checked="false" android:layout_width="fill_parent"
android:id="#+id/myCheckbox" android:layout_height="wrap_content"/>
and java :
private CustomCheckBox mCheckbox;
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox);
It works because I used it both ways :) And with some tweaks it works for RadioButtons too the same way. Happy coding!
You can use selector in xml, which is used to change the image of checkbox dynamically according to its checked state.
For example:
<item android:drawable="#drawable/ic_linkedin" android:state_checked="true" />
<item android:drawable="#drawable/ic_linkedin_disabled" android:state_checked="false" />
In the following file, if the checkbox is checked, it will set the ic_linkedin icon and if the checkbox is unchecked, it will set the ic_linkedin_disabled icon.
I use a XML file to display a button with 2 states (normal, pressed). How can I find out when the button is pressed? Is there an event like onClick --> onPressed or how can I find it out?
Here is the XML file I use:
<?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/buttonbluepressed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/buttonbluepressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/buttonbluepressed" />
<item android:drawable="#drawable/buttonblue"/>
</selector>
And here is how I use it:
<Button
android:id="#+id/button_choose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/choose_button"
android:onClick="onButtonClicked"
android:text="#string/button_choose" />
you can use setOnTouchListener as:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});
I am using ListView and for every row I have row_item.xml and I inflate that in code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/chk"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtChoice"
android:textColor="#FF0000"
android:text="TEST"
android:layout_toRightOf="#id/chk"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
How to change that checkBox use another custom_1 image when is checked and another custom_2 image when is unchecked ?
Drawable customdrawablecheckbox.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/unchecked_drawable" />
<item android:state_checked="true" android:drawable="#drawable/checked_drawable" />
<item android:drawable="#drawable/unchecked_drawable" /> <!-- default state -->
</selector>
yourcheckbox xml:
<CheckBox
android:id="#+id/chk"
android:button="#drawable/customdrawablecheckbox"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
checkbox is a button, so you can provide your own drawable with check uncheck state and it as checkbox background. For instance
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/yourdrawable1" />
<item android:state_checked="true" android:drawable="#drawable/yourdrawable2" />
<item android:drawable="#drawable/yourdrawable1" /> <!-- default -->
</selector>
and put this in a file.xml in your drawable folder. In your checkbox:
<CheckBox
android:button="#drawable/file"
android:id="#+id/chk"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Its pretty easy :)
First you need to create a CustomCheckBox class which will extend CheckBox and override the onDraw(Canvas canvas) method:
public class CustomCheckBox extends CheckBox {
private final Drawable buttonDrawable;
public CustomCheckBox(Context context, AttributeSet set) {
super(context, set);
buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box);
try {
setButtonDrawable(android.R.color.transparent);
} catch (Exception e) {
// DO NOTHING
}
setPadding(10, 5, 50, 5);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
buttonDrawable.setState(getDrawableState());
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
if (buttonDrawable != null) {
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int buttonWidth = buttonDrawable.getIntrinsicWidth();
int buttonLeft = getWidth() - buttonWidth - 5;
buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
buttonDrawable.draw(canvas);
}
}
}
Also create your selector named custom_check_box in your drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_check_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_check_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_check_on" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_check_off" />
<item android:state_checked="false" android:drawable="#drawable/btn_check_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_check_on" />
</selector>
And use your custom icons/imgs in the XML above for all three states (focused/pressed/default)
Use the custom component in your XML like this :
<*package + class path*.CustomCheckBox // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder
android:text="#string/text"
android:checked="false" android:layout_width="fill_parent"
android:id="#+id/myCheckbox" android:layout_height="wrap_content"/>
and java :
private CustomCheckBox mCheckbox;
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox);
It works because I used it both ways :) And with some tweaks it works for RadioButtons too the same way. Happy coding!
You can use selector in xml, which is used to change the image of checkbox dynamically according to its checked state.
For example:
<item android:drawable="#drawable/ic_linkedin" android:state_checked="true" />
<item android:drawable="#drawable/ic_linkedin_disabled" android:state_checked="false" />
In the following file, if the checkbox is checked, it will set the ic_linkedin icon and if the checkbox is unchecked, it will set the ic_linkedin_disabled icon.