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);
}
});
Related
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"))
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 am stucked in a problem developing an android App.
When the user clicks in a Button, I need to change the background colour of this button. But I need to do in a way that doesn't affects the style, mainly the shapes.
Okay as you have said without changing its style, i think here's what you want,
1.you might be interested in color filters like this
Button btn = (Button) findViewById(R.id.button1);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
You use different values according to your required colour.If you want to know the constant values of colours, you can refer the documents.
2.you can programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
For example for a red shaded button
btn.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
For a green shaded button
btn.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
And so on.What it actually does is, it works by multiplying the current button colour value by your colour value.
3.You can also use an online tool like this Android Button Maker to customize your button and use android:background="#drawable/custom_btn" in your layout(inside the <Button> tag) to define the customized button.
Now there are many more ways too to achieve what you want but i think these are some easy and quick fixes you can use.Hope this helps.
Try:
Button myButton = (Button) findViewById(R.id.myButton1);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myButton.setBackgroundColor(color.Green);
}
});
Try this:
Button btn = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btn.setBackgroundResource(color.lightblue);
}
});
The easiest way of changing a buttons background is using a state list drawable. This is a xml file where different drawables for the background of the button for different states are defined. An Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
<item android:drawable="#drawabel/normal"/> // Drawable in normal state
<item android:drawabel="#drawable/pressed"/ android:state_pressed="true"> //Drawabel when pressed
</selector>
Just use android:background="#drawable/background.xml" in the buttons xml or setBackground(R.drawable.background). The framework will do the work for you.
I want to be able to see what items that have been selected in my list. Before turning ListView into ListFragment, the selected items had a blue background color (Holo Dark), but now I can only see the blue color while pressing the items - then it disappears. I do not have a problem with the items getting "checked", because they are. You just can't see it.
Here's what I do in onActivityCreated():
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemsCanFocus(false);
getListView().setMultiChoiceModeListener(new LongPress());
LongPress() is a private class implementing ListView.MultiChoiceModeListener. It opens up a contextual action bar when holding finger on an item. The user can then select more items, and this is where the problem occurs. You can not see which items are checked or not (I want them to be highlighted). Why did it work before extending ListFragment? I tried to create a custom selector in my .xml-files, that didn't seem to work either. I would prefer using the custom one, if there's any.
Here's another method in my ListFragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_listview, container, false);
}
fragment_listview.xml isn't doing anything special apart from changing the background color and the color of the divider.
What should I do?
I tried this, but it didn't work:
In my list_item.xml:
android:background="#drawable/selector"
selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/item_checked" />
</selector>
item_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http//schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
</shape>
I was having the exact same issue.
I found a solution in this post: Highlight custom listview item when long click
The trick:
Set in the layout file of your list's row (in the top level component, usually a LinearLayout or RelativeLayout):
android:background="?android:attr/activatedBackgroundIndicator"
I cannot find the details right now, but you have to provide a statelist drawable for the listview-items that provides a separate drawable for the checked state.