How can I display images on buttons that are attached to TilePane - javafx-8

-JavaFX
I created an array of buttons and attached them to tilePane. Now, I want to display an image on each button. I need some sort of an array of images that has the same size as the array buttons so that I can allocate an image for each button and display it. Meaning I need every button to display its own image.
I am coding in JavaFX. I'm struggling to create an array of images so that they can be displayed on the buttons. Please note that I want to display images on each button #not display images when I click the button.
public class Main extends Application {
private static Image[] deck = new Image[35];
private static ImageView[] imageView = new ImageView[35];
#Override
public void start(Stage LoginWindow) {
Button[] btn = new Button[35];
Controller control = new Controller();
for (int s = 0; s < 35; s++) {
btn[s] = new Button();
btn[s].setPrefSize(262, 200);
btn[s].setOnAction(e -> control.display());
btn[s].setStyle("-fx-border-color: #10fffa; -fx-border-width: 5px;");
}
TilePane pane = new TilePane();
pane.setPadding(new Insets(20, 10, 20, 10));
pane.setHgap(10);
pane.setVgap(10);
pane.setAlignment(Pos.CENTER);
pane.setMaxSize(500, 700);
pane.getChildren().addAll(btn);
Scene scene = new Scene(pane, 500, 700);
LoginWindow.setFullScreen(true);
LoginWindow.setScene(scene);
LoginWindow.show();
}
public void add() {
for (int i = 0; i < deck.length; i++) {
deck[i] = new Image(i + ".png");
imageView[i].setImage(deck[i]);
}
}
}
When I run the code it should display images on every button. Images can differ. No action Listeners. Just buttons with images on a TilePane.

Related

How to define gravity of a Dialog Fragment?

I want my dialog to stick to the start but it always comes in the center.
i will give you example==>
Following is the code -->Onclick image -->Dialog Appears==>
Dialog dialog; //your dialog declaration
//
//
//
oncreate(){
imageView = (ImageView) findViewById(R.id.customProfileGridImg);
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.START |Gravity.TOP; //change direction as per your requirement
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.show();
}
});
}

How to make background of modal popup darker and non transparent in unity?

I have a modal pop up in which I show a list of stories title and a button to open a browser that points the link of the stories, but the pop up is now transparent I want the background to appear darker
Need to modify my modal pop up without a transparent background or make the background color darker, or how can I change the GUI modals opaque or can I set opaque value for this GUI?
please check my modal pop up code here:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// example:
// HFTDialog.MessageBox("error", "Sorry but you're S.O.L", () => { Application.Quit() });
public class HFTDialog : MonoBehaviour {
Rect m_windowRect;
Action m_action;
string m_title;
string m_msg;
string[] m_myNewsTitleColl;
string[] m_myColl;
GUIStyle labelStyle, labelStyleShadow, buttonStyle, sliderStyle, sliderThumbStyle;
// The position on of the scrolling viewport
public Vector2 scrollPosition = Vector2.zero;
private GUIStyle guiStyle; //create a new variable
static public void MessageBox(string[] myNewsTitleColl, string[] myColl,string title, string msg, Action action)
{
GameObject go = new GameObject("HFTDialog");
HFTDialog dlg = go.AddComponent<HFTDialog>();
dlg.Init(myNewsTitleColl,myColl,title, msg, action);
}
static public void CloseMessageBox(GameObject go)
{
Destroy(go.gameObject);
}
void Init( string[] myNewsTitleColl,string[] myColl,string title, string msg, Action action)
{
m_title = title;
m_msg = msg;
m_action = action;
m_myColl=myColl;
m_myNewsTitleColl=myNewsTitleColl;
}
void OnGUI()
{
const int maxWidth = 640;
const int maxHeight = 480;//480;
int width = Mathf.Min(maxWidth, Screen.width - 20);
int height = Mathf.Min(maxHeight, Screen.height - 20);
m_windowRect = new Rect(
(Screen.width - width) / 2,
(Screen.height - height) / 2,
width,
height);
GUI.color = Color.white;
GUI.backgroundColor = Color.yellow;
m_windowRect = GUI.Window(0, m_windowRect, WindowFunc, m_title);
}
void WindowFunc(int windowID)
{
const int border = 10;
const int width = 50;
const int height = 25;
const int spacing = 10;
guiStyle = new GUIStyle();
//guiStyle.fontSize = 14; //change the font size
guiStyle.fontStyle = FontStyle.Bold;
guiStyle.normal.textColor = Color.white;
int myNewsTitlePositionY=border + spacing;//Initial position
if(m_myNewsTitleColl.Length==0){
Rect l = new Rect(border, myNewsTitlePositionY,m_windowRect.width - border * 2,m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, "Currently No News Available ,Please try later..",guiStyle);//This is the news title label
}
else{
for(var i = 0; i < m_myNewsTitleColl.Length; i++)
{
var NewsTitle = m_myNewsTitleColl[i];
Rect l = new Rect(border, myNewsTitlePositionY,m_windowRect.width - border * 2,m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, NewsTitle,guiStyle);//This is the news title label
myNewsTitlePositionY=myNewsTitlePositionY+33;//next y position of the news title label
}
}
int myY=-15;
for(var i = 0; i < m_myColl.Length; i++)
{
var item = m_myColl[i];
// work with link item here to make a button
myY=myY+33; //Y position of button
Rect btn = new Rect(550,myY, width+25, height);
//Rect btn = new Rect(510,myY, width+25, height); //when scroll enable -
if (GUI.Button(btn, "Open Link"))
{
//open browser pointing the url
Application.OpenURL(item);
}
Debug.Log ("from string array value is :"+item);
}
//End Scrollview
// GUI.EndScrollView();
Rect b = new Rect(
m_windowRect.width - width - border,
m_windowRect.height - height - border,
width,
height);
if (GUI.Button(b, "close"))
{
Destroy(this.gameObject);
// m_action();
}
}
}

How to make button image change with on click anywhere on screen?

I am very new to Unity and I have trouble understanding on click possibilities with my UI buttons etc.. In my project, I have a button which changes its image whenever I click on it and the way I made it is: I created this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class OnClickScript : MonoBehaviour
{
public static int spritenumber=1;
public Button mybutton;
public Sprite square;
public Sprite circle;
public Sprite triangle;
public int counter = 1;
void Start()
{
mybutton = GetComponent<Button>();
}
public void changeButton()
{
if (PAUSESCRIPTE.isPaused == false)
{
counter++;
if (counter == 1)
{
mybutton.image.overrideSprite = square;
spritenumber = 1;
soundmanagerscript.PlaySound("buttonpress");
}
if (counter == 2)
{
mybutton.image.overrideSprite = circle;
spritenumber = 2;
soundmanagerscript.PlaySound("buttonpress");
}
if (counter == 3)
{
mybutton.image.overrideSprite = triangle;
spritenumber = 3;
counter = counter - 3;
soundmanagerscript.PlaySound("buttonpress");
}
}
}
}
I attached this script to my button, selected sprites which I want to use and I added on On Click() function to the button.
Afterwards I decided that I no longer want to change the sprite of my button by clicking on the button itself but rather by clicking anywhere on the screen. I tried using Input.GetMouseButtonDown(0) but got no results since I used it incorrectly. Any help on, how to achieve what I want, is very welcome. Thanks in advance.
Here's how I'd do it:
using UnityEngine.UI;
using UnityEngine;
public class ButtonScript : MonoBehaviour
{
// Public objects
public Sprite[] sprites;
//Private objects/variables
private Image buttonImage;
private int spriteIndex;
void Start()
{
buttonImage = GetComponent<Image>(); // Get the image component of your button
spriteIndex = 0; // Set the index to 0
buttonImage.sprite = sprites[spriteIndex]; // Set the image to the first image in your sprite array
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
spriteIndex++; // Increment the index
if (spriteIndex > sprites.Length - 1) // Ensure that you stay within the array size
{
spriteIndex = 0;
}
buttonImage.sprite = sprites[spriteIndex]; // Set the image
}
}
}
However, you need to change the image settings to:
Image Type: simple
Use Sprite Mesh: true
(maybe) Preserve aspect: true
As an answer to your comment above, I will suggest you to have a button in the background and your pause button above. Then, replace your Update() method with an event OnClick() as you did before.
Your pause button will catch the left click and your background button will not.
Be sure to tick Interactable in the Button component.
Then you can safly remove the Image component of your background button, if you don't need it.
What it means is you can create (or modify) a class for your pause button
using UnityEngine.EventSystems;
public class PauseButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public bool IsMouseOver = false;
public void OnPointerEnter(PointerEventData eventData)
{
IsMouseOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
IsMouseOver = false;
}
// --
// Your OnClick method
// --
}
Note: the using eventSystems.
Now you can use this bool value within a code like amengelbrecht send you.
I'll will copy and paste his code from his answer, and add the bool value based on the previous class above.
public class BackgroundButtonScript : MonoBehaviour
{
// Public objects
public Sprite[] sprites;
public PauseButton pauseButton;
//Private variables
private Image buttonImage;
private int spriteIndex;
private void Start()
{
buttonImage = GetComponent<Image>(); // Get the image component of your button
spriteIndex = 0; // Set the index to 0
buttonImage.sprite = sprites[spriteIndex]; // Set the image to the first image in your sprite array
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && !pauseButton.IsMouseOver)
{
spriteIndex++; // Increment the index
if (spriteIndex > sprites.Length - 1) // Ensure that you stay within the array size
{
spriteIndex = 0;
}
buttonImage.sprite = sprites[spriteIndex]; // Set the image
}
}
}
This will allow you to click anywhere on your screen except if the mouse is over your pause button. In this case, OnClick from the pause button will be called.
Be careful
If your game is is pause and the user left click anywhere but the pause button, depends on how your pause works, the Update() method of BackgroundButtonScript will be called and can lead to if (Input.GetMouseButtonDown(0) && !pauseButton.IsMouseOver).

how to add flyout text to flyout in button programatically

Im a generating this button and i would like to set text in the flyout
var button = new Button();
button.Height = height;
button.Width = width;
button.Margin = new Thickness(left, top, 0, 0);
button.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red);
button.BorderThickness = new Thickness(2);
button.Background = new SolidColorBrush(Windows.UI.Colors.Transparent);
button.Flyout = new Flyout();
how do i set content of button.Flyout?
how do i set content of button.Flyout?
You could pass button instance to the follow method.
public void AddFlyoutToBtn(Button TargetBtn)
{
TargetBtn.Flyout = new Flyout()
{
Content = new StackPanel
{
Children =
{
new TextBlock
{
Text = "All items will be removed. Do you want to continue?",
Margin = new Thickness(10, 10, 0, 0)
},
new Button
{
Content = "Yes, empty my cart"
}
}
}
};
}

Change the background color of cell in table layout for showing selected and unselected on cell (text click event)clickevent in android

Help me to change background color on text click event on each cell when i click other cell previous selected cell should be unselect means only one cell which is clicked should change the background color.In my code how many cell i clicked,all clicked cell are changing background.I Want single cell should change the color at a time .
where allValues is array list and i am creating 5 column table.
TableLayout tl = (TableLayout) findViewById(R.id.tlparent);
for (int j = 0, k = 0; j < allValues.size() / 5; j++)
{
final TableRow tableRow = new TableRow(this);
for (int y = 1; y <= 5; y++)
{
TextView tv = new TextView(this);
tv.setText(allValues.get(k));
tv.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
String text = tv.getText().toString();
v.setBackgroundColor(Color.GRAY);
}
});
tableRow.addView(tv);
k++;
}
tl.removeView(tableRow);
tl.addView(TABROW);
}
I found the answer of the question. first create the global text view with "null" value.where cell_shape is nothing just a border of text view.
Like public static TextView temp = null; then replace the text view click listener in above code only by the following.
ptv.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (temp != null)
{
temp.setBackgroundColor(Color.GREEN);
temp.setBackgroundResource(R.drawable.cell_shape);
temp = null;
}
if (temp == null) {
TextView ptv = (TextView) v;
String mdnStr =ptv.getText().toString().trim();
v.setBackgroundColor(Color.GRAY);
if (mdnStr.length()== 10) {
//do some thing whatever you want...
}
temp = ptv;
}
}
});