I have a Widget for my Android App. On this Widget there is a button. The user can freely choose the color of the button. So I also have to set the colors for the different button states dynamically.
Normally, I would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorButtonRedTransparentPressed"
android:state_pressed="true" />
<item android:drawable="#color/colorButtonRedTransparentPressed"
android:state_focused="true" />
<item android:drawable="#color/colorButtonRedTransparentNotPressed" />
</selector>
As mentioned, I have to do this dynamically because the user is free to choose the color:
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] {}, new ColorDrawable(Color.parseColor(customButton.getColorHex())));
drawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor("#0000")));
The challenge:
The button is on a Widget. So i cannot just simply call:
myButton.setBackground(drawable);
I need to call something like this:
remoteView.setInt(R.id.button_fixedvalue, "setBackground", drawable)
But the function expects an integer and I don't have one.
Does anyone have a different approach?
Option 1
If there is a limited number of colors that the user can choose from, you could create a selector resource for each color option and then use the `setInt()` method to assign the resource ID that corresponds to the selected color.
Option 2
If by "freely choose" you mean that the user can select any possible color - or at least from a very large number - option 1 won't work of course.
Depending on what your drawable exactly looks like, you could try this instead:
Create only a single selector resource.
Use an ImageView for your button.
Apply the color as a tint to the ImageView using the ImageView.setColorFilter() method.
That would look something like this:
remoteView.setInt(R.id.button_id, "setColorFilter", Color.parseColor("#0000"))
Related
I have a radio group with 4 radio buttons. I use this code :
int radioButtonID = radioGroup.getCheckedRadioButtonId();
View checkedRadioButton = radioGroup.findViewById(radioButtonID);
int idx = radioGroup.indexOfChild(checkedRadioButton);
To retrieve the index of the checked radio button.
The problem is that i want to change the text color of the checked radio button, while i dont know the specific radio button each time. so: checkedRadioButton.setTextColor(color);
shows me an error that i need to 'Add a qualifier', which basicaly is showing me that i should use a specific radio button to call that method, like:
radioButtonAns1.setTextColor(color);
I would like if someone would also explain why i have that error and if there is a solution on that. The only method i can call is .setBackgroundColor() which looks ugly.
Thanks in advance!
You can use the setTextColor method if you cast the View that you found to RadioButton:
RadioButton checkedRadioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
checkedRadioButton.setTextColor(color);
Well it is kinda of straight forward, the error is basically saying hey you trying to change the color of the group containing the groups of radio button.Please choose which one you want first then change the it. To remedy this declare a Boolean array,then set a on click listener to detect the position of the clicked radio button then set then set Boolean value to true in the array.
An XML alternative would be:
set the TextColor as a #Color selector.
<RadioButton
android:id="#+id/radio_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio 1"
android:textColor="#color/radiobuttonstate"/>
The color selector would be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#000000"/>
<item android:state_checked="true" android:color="#ffffff"/>
<item android:color="#00f"/>
</selector>
I'm willing to create a android quiz app. I've done all things and my app is completed but I'd like to make the quiz option buttons to change its color when it is pressed by user in order to show whether the answer is correct or wrong.
I'd like to show red color button on clicking wrong option and green on correct option .please help me with a simple code that I can embedd in my presaved app java files.
You can try this:
someButton.setBackgroundColor(Color.RED); // Wrong option
someButton.setBackgroundColor(Color.GREEN); // Correct option
Refer to View#setBackgroundColor(int) and Color.
A Button is a sub-class of View.
You can use the combination of android:state_selected and the android:state_enabled properties of the StateListDrawable to achieve the effect through xml.
Your button background can be defined as a drawable below
<selector>
<item android:drawable="#drawable/default_button_background" />
<item android:state_selected="true" android:state_enabled="true" android:drawable="#drawable/correct_answer_background" />
<item android:state_selected="true" android:state_enabled="false" android:drawable="#drawable/wrong_answer_background"
</selector>
and in your code. On Click of the button add this code
boolean isAnswerCorrect = //your logic to check if answer is correct or not
clickedButton.setEnabled(isAnswerCorrect);
In the following approach, you would need to create two image files for backgrounds on the button to represent correct or false.
Create a folder called Drawable under res and place these two image files in there, then call them as seen below like, R.drawable.filename.
buttonCheckAnswerObject = (Button)findViewById(R.id.buttonCheckAnswer);
buttonCheckAnswerObject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
boolean userAnswer;//check if correct
if (userAnswer){
v.setBackgroundResource(R.drawable.button_correct_answer_color);
}
else {
v.setBackgroundResource(R.drawable.button_false_answer_color);
}
}
});
enter code here
I'm trying to figure out how to do this thing for an android app.
So I made an array of buttons
Button btn[][] = new Button[10][10];
How do I make it so that once I click a button that it, for instance, turns a different color?
I have had trouble making it because I can create the array, and it looks nice, but how do I assign different functions for individual buttons? Are buttons in the array already labeled and can I use individual ones? Thanks.
Any time you would like to make a view change a different color based on the user actions, you should use a state list drawable
Here is a very simple example of a state list drawable which you would use to trigger only off whether the user had pressed the view or not.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#color/brown" />
<item android:state_pressed="true" android:color="#color/brown_selected" />
</selector>
You would then set this on the view with the following attribute to the view in xml
android:background="#drawable/background"
This though, will only be changed while the user is pressing the button. If you would like it to permenantly change color, use a on click listener. For example if you would like to change the background color to white:
button.setOnClickListener(new View.OnClickListener() {
/**
* Handle a user clicking on the view v
* #param v the view the user clicked on. In this case the button
*/
#Override public void onClick(View v) {
// Set the background color to white
v.setBackgroundColor(Color.WHITE);
}
});
Can anyone tell me how to get background color of selected item from ListView in android?
Here I an using ListView with multiple item select mode and I just want to set background color of selected items. If user unchecked the option I want to restore it's background color to black.
I can set the background color of selected items but I am not getting the current background color of selected item.
Please help me.
I am using Android 4.0.3 and editor Eclipse
for this you should maintain a boolean variable for each item initially with false and when you click on any item you should make the variable to true.
and in getView() of your custom adapter you check whether it is true or false and do the needfull.
before trying this have a xml file in drawable folder and set it as background if this doesn't work then do the above must work..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- touch down -->
<item
android:drawable="#drawable/list_selector_pressed"
android:state_pressed="true"/>
<!-- selected -->
<item
android:drawable="#drawable/list_selector_selected"
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
Refer this LINK LINK
I am using a custom Group indicator as shown in the code below. Now if I leave the default I can use setBounds perfectly, but when using my custom image which is the same dimensions and setup as the same .9 patch file it is always half off screen. No matter what values I use for setBounds()
Drawable plus = (Drawable) getResources().getDrawable(R.drawable.expander_group);
getExpandableListView().setGroupIndicator(plus);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
getExpandableListView().setIndicatorBounds(0,0);
XML expander_group
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_expanded="true"
android:drawable="#drawable/minus" />
<item
android:drawable="#drawable/plus" />
</selector>
EDIT:
Interestingly. I have copied the exact xml and image files from the standard Android files. And when I use the above method to define it the exact same thing happens! So it is not the xml or image files that are causing the issue I think.