Changing Color of Button Text and State

change button text color when pressed

See the section called State List in this bit of documentation...Drawable Resources.

You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.

EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.

Changing color of button text and state

To change color of text

button.titleLabel.textColor = UIColor.grayColor()

To change state, on button press add following -

button.enabled = true

IBAction method should be like -

@IBAction func buttonTapped(sender : UIButton!) {
sender.enabled = false
}

How to change the background and text color within button using button onClick in React JS

You can use States to control color dynamically.

import React, { Component } from 'react';



class Page extends Component {
constructor(props){
super(props);
this.state={color:"black"}
}
render() {

return (
<div>

<div style={{ background: "#f0f0f0f" }}>
<h3 style={{ color: this.state.color }}>Heading</h3>
<p style={{ color: '#706c61' }}> This is a text...</p>
</div>

<div>
<button onClick={()=>this.setState({color:"white"})}> White </button>
<button onClick={()=>this.setState({color:"purple"})}> Purple </button>
<button onClick={()=>this.setState({color:"red"})}> Red </button>
<button onClick={()=>this.setState({color:"green"})}> Green </button>
</div>
</div>
);
}
}

export default Page;

Sandbox - Link

Android customized button; changing text color

Create a stateful color for your button, just like you did for background, for example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />

<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />

<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />

<!-- Default color -->
<item android:color="#ffffff" />

</selector>

Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:

android:textColor="@drawable/button_text_color"

how to change one button text color to red and rest of button text color to black after click in React

selected index should be stored in parent component:

// Parent component
export default function App() {
const [clickedIndex, setClickedIndex] = React.useState(null);
const fake = [1, 2, 3, 4, 5];

const click = (index) => {
console.log("clicked " + index);
setClickedIndex(index);
};

return (
<div className="App">
{fake.map((value, index) => {
return (
<Circles
key={index}
index={index}
clickedIndex={clickedIndex}
onClick={() => click(index)}
></Circles>
);
})}
</div>
);
}
// Child component
export default function Circle(props) {
const { index, clickedIndex, onClick } = props;
const isMeClicked = index === clickedIndex;
return (
<div className="App">
<button
onClick={onClick}
style={{ color: isMeClicked ? "red" : "black" }}
>
Circle{index}
</button>
</div>
);
}

How to change button text color and background color while button is pressed

There are some issues in your Synth file:

  1. It's not closed with </synth>

  2. If you don't set opaque="true" in it, by default your JButton will be transparent (i.e. your background won't be visible).

  3. I'm not entirely sure why, but for the foreground font color to change on state change you need to use TEXT_FOREGROUND instead of FOREGROUND key.

I changed some colors from your file as they weren't too visible, but your file should look something like:

<synth>
<style id="basicStyle">
<font name="Verdana" size="16" />
<state>
<color value="#eeeeee" type="BACKGROUND" />
<color value="#000000" type="FOREGROUND" />
</state>

</style>
<bind style="basicStyle" type="region" key=".*" />

<style id="button_style">
<opaque value="true" />
<insets top="4" left="4" right="4" bottom="4" />
<state>
<color value="blue" type="TEXT_FOREGROUND" />
<color value="#ffffff" type="BACKGROUND" />
</state>
<state value="PRESSED">
<color value="white" type="TEXT_FOREGROUND" />
<color value="#aaaaaa" type="BACKGROUND" />
</state>
</style>
<bind style="button_style" type="region" key="button" />
</synth>

Sample Image Sample Image



Related Topics



Leave a reply



Submit