Highlighting the selected cell in grid layout in unity - unity3d

I am using a panel as grid lay out and a button prefab in that lay out.I can able to attach event for the button but i couldn't able to figure out how to highlight that button when user clicks on that button.Is there any way to change background color of that clone button when user clicks on that?

you can use onClick to call a method when the button is clicked.
public Button btn;
void Start () {
btn.onClick.AddListener(ChangeColor);
}
or just use the editor, OnClick list, just note that for this, the method you want to call should be public
if you just want to highlight the button which is currently clicked and when the user clicks on another button the other button gets highlighted and this button gets back to its normal color, then that option is simply doable in the editor, just set the highlight color of the button in the editor
but if you want the buttons to change their colors after you click on them and that change is permanent then
you can just change the color of the image property of the button, by accessing its Image property and set its color to something you want.
void changeColor(){
pButton.GetComponent<Image>().color = yourColor;
}
if you dont need to change the color of the image which is used in the button, but you want to change the Button normalcolor, first you keep your previous colors, then just set the new color to the normalcolor
I also think if you want to use this method it is better to change the highlightedColor too, because after the button is pressed it, after it goes to pressed color for a second, it will enter the highlighted state until you click on another button, so I guess it is better if you change both of the colors
public void ChangeColor()
{
ColorBlock colorBlock = btn.colors;
colorBlock.normalColor = Color.blue;
colorBlock.highlightedColor = Color.blue;
btn.colors = colorBlock;
}

Related

Color of a button changes without a reason

I have two views. After clicking on the button in the first view, a popup window appears (second view). In it, if a user does some action, the popup disappears, and the user is once again in the first view and the button is of a different color (which is great!). However, when I touch down on it and move the finger away (so I won't click it) it changes back to its initial color. The same happens when an alert appears in the window - the button changes back to its initial color. Any help?
I changed the color of the button through delegate and line:
cell.btn2.titleLabel?.textColor = UIColor.gray
Use cell.btn2.tintColor = UIColor.gray to change the tint color, rather than directly affecting the titleLabel's textColor.

Changing buttons background color while the button is being clicked

While the button is pressed, I want the buttons background to be blue, and after the click event has occurred (and I have loaded/shown what needs to be done), I want the background color to go back to its default color.
If I do
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//Some code to change my background color of the button
button.setBackgroundSearchAgainButton("0066CC");
}
});
This permanently changes my buttons background color to blue. How do then change it back to default since there is no OffClick event.
I was wondering if I could use onBlue event, but I dont know how to use it since it doesnt fall under button.addClickHandler()
You could use CSS styles. For example let's say, you have a style like:
.blue {
background: #0066cc;
}
When the button is clicked you just add this style:
button.addStyleName("blue");
You can do this in the ClickEvent or the MouseDownEvent.
After you have done everything you wanted, you can just remove the style:
button.removeStyleName("blue");
And btw onClick doesn't mean that the click is now in ON state. This means this method will be called ON a click event, so it doesn't make sense to look for an offClick method.

Want to see the image only in iPhone

I have a button connected to an action.
When I set the background image on that button, the result is not visually appealing. I want just to see the image not the button but I want the ibaction functionality of that button.
Please help
You need a button with type UIButtonTypeCustom (this will stop the default button appearance from drawing). You can do this in interface builder or in code (although you can't change it once the button is initialized).

Clear background color of delete button

I have background image for every cell of table view, when i swipe delete button appears, the problem is that a square of white color(default cell color) appears behind the delete button, i want to get rid of that white square, so how to clear background color of delete button?
If u have create button dynamically then use react round type button. if u have same problem then u choose custom type button. If u have drag and drop then unchecked opaque quality of button in view section.

Clear button color on text field

How do you change the clear button color of a UITextField?
You want to use rightView and rightViewMode. Use a button and add an image that is a red clear button. Then add your target and perform your logic.
rightViewRectForBounds will also return the size of the button for the rightView.