button textcolor not changed - android

button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button3.setBackgroundResource(R.color.buttonDarkSelected);
button4.setBackgroundResource(R.color.buttonMediumUnselected);
button3.setTextColor(R.color.yellow);
}
});
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button4.setBackgroundResource(R.color.buttonDarkSelected);
button3.setBackgroundResource(R.color.buttonMediumUnselected);
button4.setTextColor(R.color.yellow);
}
});
}
I am trying to change Button's background and textcolor clicking in that buttons. the background is fine working fine but textcolor always black (instead I want it be yellow) what's my problem here.

Try this:
button4.setTextColor(getApplication().getResources().getColor(R.color.yellow));
This will work to change the text color to yellow

You can see this :
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
With state list, you can choose differents color for button with differents state.
regards,

define a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
save this as xml file (for example btn_text_color.xml)in your ../res/drawable/
Link it to your button like:
android:textColor="#drawable/btn_text_color"
For more details on the subject, see the link that #Kentino posted.

Related

Permanently change resource of ImageButton

I have 3 ImageButtons. I have been trying to change the resource on image button onClick programmatically like when an imagebutton is pressed change the image on it until some other button is not pressed but its not changing. I have used selector for the source:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/favorites02" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/favorites02" />
<item android:state_hovered="true" android:drawable="#drawable/favorites01" />
<item android:drawable="#drawable/favorites00" /> <!-- default -->
</selector>
And in java in OnClicklistener I am changing resource like this:
btnFavorite.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isFavchecked = true;
isAllChecked = false;
isRecentChecked = false;
btnFavorite.setImageResource(R.drawable.favorites02);
btnRecent.setImageResource(R.drawable.tile_recent_style);
btnAll.setImageResource(R.drawable.tile_music_style);
}
});
but it only shows what is declared for the first time.

Change colour of buttons when one is selected; Android development

So I am making an app with 3 buttons. Currently I have the following .xml file that makes them have rounded off edges and be the colour red. Now I want to to make them so if one is selected then that button turns green while the other two remain, or turn back to red. How could I go about doing this? Do I have to make another .xml file?
Here is my drawable .xml file for the buttons:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!--red colour.-->
<solid android:color="#FF0000"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<stroke android:width="2px" android:color="#000000"/>
</shape>
Use a selector as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- green state -->
<item
android:drawable="#drawable/button_selected"
android:state_selected="true"></item>
<!-- green state -->
<item
android:drawable="#drawable/button_pressed"
android:state_pressed="true"></item>
<!-- red state -->
<item
android:drawable="#drawable/button_disabled"></item>
</selector>
Then, you should call this selector like:
<Button
...
android:background="#drawable/my_selector" />
And create each drawable.xml (as your example for the red button) for every state: button_selected, button_pressed and button_disabled.
You can also remain the state by using onTouchListener like:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// change the color
return true;
case MotionEvent.ACTION_UP:
// intitial color
return true;
default:
return false;
}
}
});
However, it's better to use a Selector and a background, this use less resource.
UPDATE:
You can use a setBackgroundResource method to remain and change the background state of clicked button as follows:
// 1st clicklistener method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.green_drawable);
button2.setBackgroundResource(R.drawable.selector_drawable);
}
}
// 2nd clicklistener method
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.green_drawable);
button1.setBackgroundResource(R.drawable.selector_drawable);
}
}
Not tested but it should work.

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>

Make an Android button change background on click through XML

Is there a way to specify an alternative background image/color for a Button in the XML file that is going to be applied onClick, or do I have to do a Button.setBackground() in the onClickListener?
To change the image by using code:
public void onClick(View v) {
if(v.id == R.id.button_id) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
Or, using an XML file:
<?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>
In OnClick, just add this code:
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
In the latest version of the SDK, you would use the setBackgroundResource method.
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setBackgroundResource(R.drawable.ImageResource);
}
}
public void methodOnClick(View view){
Button.setBackgroundResource(R.drawable.nameImage);
}
i recommend use button inside LinearLayout for adjust to size of Linear.
Try:
public void onclick(View v){
ImageView activity= (ImageView) findViewById(R.id.imageview1);
button1.setImageResource(R.drawable.buttonpressed);}
I used this to change the background for my button
button.setBackground(getResources().getDrawable(R.drawable.primary_button));
"button" is the variable holding my Button, and the image am setting in the background is primary_button

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