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"
}
}
}
};
}
Related
I am new to unity and am using unity 2020.3.26f1 . I am receiving an array of names from server. I want to make buttons dynamically using those names inside scroll view using c# script and assign on Click() that simply prints the name of button when its clicked. I just don't know how to create button dynamically. Following is the script I have attached to scroll View;
public string allNames;
void Start()
{
StartCoroutine(getNames());
}
IEnumerator getNames()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost:8080/names");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
allNames = www.downloadHandler.text;
allNames = allNames.Replace("[", "");
allNames = allNames.Replace("]", "");
allNames = allNames.Replace("\"","");
string[] separatedNames = allNames.Split(',');
for (int i = 0; i < separatedNames.Length; i++)
{
Debug.Log(separatedNames[i]);
//make buttons here and attach them to scroll View
}
}
}
I found a video that applied #ShafqatJamilKhan suggestion. Posting my code here for reference.
//content reference of scroll view
[SerializeField] Transform contentPanel;
[SerializeField] GameObject buttonPrefab;
void Start()
{
StartCoroutine(getNames());
}
IEnumerator getNames()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost:8080/names");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
string allNames = www.downloadHandler.text;
allNames = allNames.Replace("[", "");
allNames = allNames.Replace("]", "");
allNames = allNames.Replace("\"","");
// prefab button y position and yPosition should be the same.
float yPosition = 115;
string[] separatedNames = allNames.Split(',');
for (int i = 0; i < separatedNames.Length; i++)
{
GameObject button = (GameObject)Instantiate(buttonPrefab);
string name = separatedNames[i];
button.GetComponentInChildren<Text>().text = name;
button.GetComponent<Button>().onClick.AddListener(
// your function whatever you want to do
() => { customFunction(name); });
// applying y positions so the buttons dont overlap
button.transform.SetPositionAndRotation(new Vector3(0.0f, yPosition, 0.0f), new Quaternion(0.0f, 0.0f, 0.0f,0.0f));
button.transform.SetParent(contentPanel,false);
yPosition -= 35;
}
}
}
Make a nice button and save it as prefab.
Instantiate it or Pool it.
Add onClick events.
GameObject buttonPrefab;
void MyAwesomeCreator()
{
GameObject go = Instantiate(buttonPrefab);
var button = GetComponent<UnityEngine.UI.Button>();
button.onClick.AddListener(() => FooOnClick());
}
void FooOnClick()
{
Debug.Log("Ta-Da!");
}
Copied from Unity Forum
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();
}
}
}
I want to make a panel by script in unity and add my label and button to the panel
My existing Gui modal popup code is as shown below
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;
Rect ls = new Rect(
border,
border + spacing,
m_windowRect.width - border * 2,
m_windowRect.height - border * 2 - height - spacing);
//Start Scrollview test
scrollPosition = GUI.BeginScrollView(ls, scrollPosition, new Rect(0, 0, 600, 4800));//Increase the height if more scrolling is needed
guiStyle = new GUIStyle();
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 newsItemLink = m_myColl[i];
// work with news 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"))
{
Application.OpenURL(newsItemLink );//open browser pointing the url
}
Debug.Log ("from string array value is :"+newsItemLink );
}
//End Scrollview
GUI.EndScrollView();
//Close button
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();
}
}
}
Panel must be as shown in the picture (my blue color drawing is assumed as a panel - in which the label and button must be held together):
Also how can I make my modal popup background more darker ,is there a way to make it opaque or can I set opaque value for this Modal GUI?
-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.
I'm coding in Xamarin.iOs and I'd like to add a border for bottom of the UITextField.
So it's simple, I've googled it and (I personalized it to Xamarin.ios and) I have this code from this link.
UITextField _textField = new UITextField();
_textField.Placeholder = placeHolder;
_textField.AutocapitalizationType = autocap.Value;
_textField.AutocorrectionType = uitextAutocorrect.Value;
_textField.BorderStyle = borderstyle.Value;
_textField.SecureTextEntry = secureTextEntry.Value;
_textField.KeyboardType = uiKeyboardType.Value;
return _textField;
As you know I cannot put this part of code in constructor of my ViewController and also in ViewWillAppear. So I have to put it in ViewDidAppear, BUT it's too late, it means that when I run ViewController, it show the textField without borders, and just after a few milliseconds, the border will appear which is toooo late for me.
Any idea for this display problem?
EDIT:
So I ask my question in much more detail :
here is my complete code:
public class ChangePasswordViewController: UIViewController
{
//private MainViewModel _viewModel;
private UILabel _oldPasswordLabel;
private UITextField _oldPasswordTextField;
private UILabel _newPasswordLabel;
private UITextField _newPasswordTextField;
private UILabel _confirmPasswordLabel;
private UITextField _confirmPasswordTextField;
private User currentUser;
private UIButton _saveNewPassworBtn;
public ChangePasswordViewController(User user)
{
currentUser = user;
}
private void _saveNewPassworBtn_TouchUpInside(object sender, EventArgs e)
{
//do sth
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
//_viewModel = new MainViewModel();
Title = Texts.ChangePassword;
View.BackgroundColor = UIColor.White;
_oldPasswordLabel = InitUILabel(Texts.OldPasswordTxt, alignment: UITextAlignment.Right);
_oldPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_oldPasswordLabel.AdjustsFontSizeToFitWidth = true;
_oldPasswordTextField = InitUITextField(Texts.OldPassword, secureTextEntry: true);
this._oldPasswordTextField.ShouldReturn += (textField) => {
_newPasswordTextField.BecomeFirstResponder();
return true;
};
_oldPasswordTextField.BorderStyle = UITextBorderStyle.None;
_newPasswordLabel = InitUILabel(Texts.NewPasswordTxt, alignment: UITextAlignment.Right);
_newPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_newPasswordTextField = InitUITextField(Texts.NewPassword, secureTextEntry: true);
this._newPasswordTextField.ShouldReturn += (textField) => {
_confirmPasswordTextField.BecomeFirstResponder();
return true;
};
_newPasswordTextField.BorderStyle = UITextBorderStyle.None;
_confirmPasswordLabel = InitUILabel(Texts.ConfirmPasswordTxt, alignment: UITextAlignment.Right);
_confirmPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_confirmPasswordTextField = InitUITextField(Texts.NewPassword, secureTextEntry: true);
this._confirmPasswordTextField.ShouldReturn += (textField) => {
textField.ResignFirstResponder();
return true;
};
_confirmPasswordTextField.BorderStyle = UITextBorderStyle.None;
_saveNewPassworBtn = InitUIButton(Texts.Save, _saveNewPassworBtn_TouchUpInside, Colors.MainColor, UIColor.White);
View.AddSubviews(_oldPasswordLabel, _oldPasswordTextField, _newPasswordLabel, _newPasswordTextField,
_saveNewPassworBtn, _confirmPasswordLabel, _confirmPasswordTextField);
//
//constraints
var hMargin = 10;
var hMiniMargin = 5;
var vMargin = 10;
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
_oldPasswordLabel.AtTopOf(View, 100),
_oldPasswordLabel.AtLeftOf(View, hMargin),
_oldPasswordTextField.Below(_oldPasswordLabel, hMiniMargin),
_oldPasswordTextField.AtLeftOf(View, hMargin),
_oldPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_newPasswordLabel.Below(_oldPasswordTextField, vMargin),
_newPasswordLabel.AtLeftOf(View, hMargin),
_newPasswordTextField.Below(_newPasswordLabel, hMiniMargin),
_newPasswordTextField.AtLeftOf(View, hMargin),
_newPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_confirmPasswordLabel.Below(_newPasswordTextField, vMargin),
_confirmPasswordLabel.AtLeftOf(View, hMargin),
_confirmPasswordTextField.Below(_confirmPasswordLabel, hMiniMargin),
_confirmPasswordTextField.AtLeftOf(View, hMargin),
_confirmPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_saveNewPassworBtn.Below(_confirmPasswordTextField, vMargin * 3),
_saveNewPassworBtn.WithSameWidth(View).Minus(20),
_saveNewPassworBtn.AtLeftOf(View, 10)
);
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem(Texts.Cancel,UIBarButtonItemStyle.Plain, (sender, args) =>
{
this.NavigationController.SetNavigationBarHidden(false, false);
this.NavigationController.PushViewController(new ProfileViewController(), false);
}), true);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
}
In these lines:
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);
I put the border for my textfields BUT! If I put these line in ViewDidLoad or ViewWillAppear or ViewDidLayoutSubviews, it doesn't show the changes and if I put them in ViewDidAppear, it's shows perfectly with some milliseconds delay (after showing the content of my page!!!). Do you have any solution for me?
It caused by using Cirrious.FluentLayouts.
When we set the Constraints in ViewDidLoad, those control will not finish rendering before ViewDidAppear .
Print the frame of those textfields in ViewDidLoad, ViewWillAppear ,ViewDidAppear , you can find that only in ViewDidAppear the frame is not equal (0,0,0,0).
To solve the problem, you can add View.LayoutIfNeeded in ViewDidLoad to force update the view.
View.AddConstraints(
//xxx
);
View.LayoutIfNeeded(); //Add this
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);