JavaFx bind Integer to Text - text

I want text containing integer "expense" will be changed, when "expense" changes itself. But when "expense" variable changes the text continues to display 0. As far as I know it must be solved by binding? How?
int expense=0;
Text expenseAmmount = new Text(String.valueOf(NumberFormat.getIntegerInstance().format(expense)) + " $");
TextFlow moneyStatus = new TextFlow();
TextFlow incomeStatus = new TextFlow();
TextFlow expenseStatus = new TextFlow();
incomeStatus.getChildren().addAll(incomeLabel,incomeAmmount);
expenseStatus.getChildren().addAll(expenseLabel,expenseAmmount);
moneyStatus.getChildren().addAll(moneyLabel,moneyAmmount);

I have found it by myself :
IntegerProperty property = new SimpleIntegerProperty(expense);
expenseAmmount.textProperty().bind(property.asString());

IntegerProperty property = new SimpleIntegerProperty(expense);
expenseAmmount.textProperty().bind(property, new NumberStringConverter());
expenseAmmount.textProperty().bind(property.asString()); was not working for me

Related

Android Spannable dismiss after editing

I am implementing mentions as in twitter #username. But mentions can't be typed, only selected from MultiAutoCompleteTextView.
I am creating spannable with blue text color on treminateToken
Spannable sp = new SpannableString(text + " ");
sp.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
But the problem is that user can edit this text.
How to make this snap non editable? To delete whole span when user press backspace?
Or at least change color back to black if it have been edited.
Try this
Pass your string to this below method using key listener of your editext
public void displayText(String s)
{
String ss[]=s.split(",");
for(int i=0;i<ss.length;i++)
{
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createContactTextView(ss[i]);
BitmapDrawable bd = (BitmapDrawable) GlobalMethods.convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
sb.append(ss[i] + ",");
sb.setSpan(new ImageSpan(bd), sb.length()-(ss[i].length()+1), sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
contact.setText(sb);//your edit text here
}
}
set text style
public TextView createContactTextView(String text)
{
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextSize(25);
tv.setBackgroundResource(R.drawable.design);
return tv;
}
Output:
if the user try to delete the text it delete the whole text

Align textView next to another one programatically

I am programmatically creating a text view and trying to place another textView right next to the first one. But I am unable to do it.
Here is the code I have written,
//1st textview
TextView itemText = new TextView(context);
itemText.setText(mItemText);
Typeface itemFont = Typeface.createFromAsset(context.getAssets(), "fonts/" + "Roboto" + ".ttf");
itemText.setTypeface(itemFont,Typeface.BOLD);
itemText.setPadding(0, padding, 0, 0);
itemText.setId(10);
RelativeLayout.LayoutParams itemTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
itemText.setTextSize(font_size);
itemText.setLayoutParams(itemTextParams);
//2nd text view
TextView seperator = new TextView(context);
seperator.setText(mSeperator);
seperator.setPadding(0,padding,0,0);
seperator.setTypeface(null,Typeface.BOLD);
RelativeLayout.LayoutParams seperatorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
seperatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId());
seperatorParams.addRule(RelativeLayout.CENTER_VERTICAL);
seperator.setLayoutParams(seperatorParams);
seperatorParams.addRule(RelativeLayout.CENTER_HORIZONTAL) works, but when I use seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId()) , the text is not shown.
Can anyone point out where I am going wrong? Or is there any other way to do this?
The width of itemTextParams is set to MATCH_PARENT, so there's no space to put anything to the right of it. Change it to WRAP_CONTENT or define a width value.

How to assign a string id to UI elements.

I'm developing an app on android and I am generating UI elements in a loop. But I need these elements to have an id with letters and numbers, for example "rl1" or "rl2". I was trying to use the method RelativeLayout.setId() but, that method only accepts int. Is there a way I can set an ID as I want without being limited to numbers?
Thanks.
Here is the code I am trying to make work.
for (int i=1; i < 10; i++)
{
//gets the frameview where the elements will be created.
String LinearLayoutId = "frameview1";
int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
LinearLayout linearLayout = (LinearLayout)findViewById(resID);
//creates the RelativeLayout that will hold the ImageIcon and the TextView
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,40 );
rl.setLayoutParams(lp);
rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));
//creates the image icon within the layout at the left side
ImageView image = new ImageView(this);
lp = new RelativeLayout.LayoutParams(
40,RelativeLayout.LayoutParams.MATCH_PARENT );
image.setLayoutParams(lp);
String imageicon = "icon_"+i;
resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
image.setImageDrawable(getResources().getDrawable(resID)); //sets the icon
rl.addView(image); //adds the ImageView to the relative layout
//creates the TextView within the layout with a 40 margin to the left
TextView tv = new TextView(this);
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
lp.setMargins(40, 0, 0, 0);
tv.setLayoutParams(lp);
String textViewID = "tv"+i;
resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
tv.setText(getResources().getString(resID));
tv.setTextColor(Color.BLACK);
tv.setTextSize(25);
rl.addView(tv);//adds the TextView to the relative layout
rl.setOnClickListener(mAddListener);
linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}
and then I have the OnCLickListener like this...
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v){
Intent intent;
Bundle bundle;
String id = getResources().getResourceEntryName(v.getId());
id = id.replaceAll("\\D+","");
int value = Integer.parseInt(id);
intent = new Intent(ERS.this, ShowInfo.class);
bundle = new Bundle();
bundle.putInt("key", value);
System.out.println(v.getId());
intent.putExtras(bundle);
startActivity(intent);
}
};
I have tried to set up numeric IDs, but then when I Look for them with:
String id = getResources().getResourceEntryName(v.getId());
It can't find them.
I had all of this in an xml file to begin with, but it was really long because there are about forty items in the list, and it was complicated for me to go and change a letter for example in all of them. I came up with this idea to generate them at runtime in a for loop. I am testing in the meantime with ten, but I can't get it to work.
If I am doing something incorrect, then pardon me, but I am new to this.
You may still find it easier to go back to XML layouts and use the R class to generate meaningful IDs. Although as you haven't included the original xml file you refer to at the end of the question, so I can only guess at the problem you had with it. It does seem to fit the bill though, and would allow you to create something along the lines of:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/hellotextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi there"/>
The android:id="#+id/hellotextview" generates an id that can be used elsewhere in your project. In your java code you could access that specific TextView with something similar to:
TextView helloText = (TextView) findViewById(R.id.hellotextview);
The R.id.hellotextview is a int automatically generated when the project is built (in gen/R.java), but as you get to pick the name you can assign them something relevant to you and your project. So instead of trying to use strings values such as "rl1" and "rl2" that you mentioned, you could use R.id.rl1 and R.id.rl2.
As well as individual UI elements, you can also use the same technique for strings (in res/values/strings.xml), and other resources stored under the project's res/ folder, such as icons, media files, etc. In the case of strings you would access them getString(R.string.some_name_given_by_you);
See Accessing Resources at the Android Developers site for more info.
Why dont you try using SharedPreferences as an alternative in case you want to access the elements which you give some ID elsewhere in some other activity.

J2ME LWUIT - Error showing outside screen labels on scroll

I'm working on a J2ME application using LWUIT.
I need to show 10 values with a Label each one.
Something like this:
Label1 Value1
Label2 Value2
Label3 Value3
Label4 Value4
............
LabelN ValueN
I'm using 1 Container for each "row" and one big Container for each "row container"
My problem is with the "rows" outside the screen. The last 4 pairs of Label+value are not showing when I use the scroll
I don't know why.
Can someone solve this?
This is my code:
this.setLayout(new BorderLayout());
PromotionMonitorDTO promotionMonitorDTO = Cloud.getPromotionMonitor();
Utils utils = new Utils();
Font f = Font.getBitmapFont("movSmall");
Container cellContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container rowContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
//FIRST PAIR///////////////////////
String stringValue = utils.calendarToShorString(promotionMonitorDTO.getLastUpdate());
Label valor = new Label(LanguageManager.getText("LastUpdate"));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
valor = new Label(LanguageManager.getText(stringValue));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
cellContainer.addComponent(rowContainer);
rowContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
//SECOND PAIR///////////////////////
stringValue = String.valueOf(promotionMonitorDTO.getInitialTarget());
valor = new Label(LanguageManager.getText("InitialTarget"));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
valor = new Label(LanguageManager.getText(stringValue));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
cellContainer.addComponent(rowContainer);
////////8 MORE PAIRS////////////////////
this.addComponent(BorderLayout.NORTH, cellContainer);
Finally I solved.
I removed the second Container, I changed the Layout of the form to BoxLayout(BoxLayout.Y_AXIS) and I added all the "row containers" to the form,
With those changes, I have the same Graphic funcionality and the scroll is working.
I have to remove the question? o leave it for others?
Maybe you must "freeze" the Form and make it not scrollable. Set the Form not scrollable using Form.setScrollable(false) and make the center/north Container of your BorderLayoutscrollable. Try to do that.

How to align RIGHT the content of TextArea in LWUIT?

I want to align the text in a TextArea to the right. I tried the following code:
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(RIGHT);
form.addComponent(textArea);
The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:
So how to align the content to the RIGHT ?
It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(TextArea.LEFT);
form.addComponent(textArea);
Setting it to LEFT makes the displayed text RIGHT aligned !
Or by removing the textArea.setRTL(true) which is mirroring the display
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setAlignment(TextArea.RIGHT);
form.addComponent(textArea);
For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}
And drawTextArea method in DefaultLookAndFeel is as follows:
int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
// remaining code is here in initial source
}
g.drawString(displayText, x, y);
Unfortunately TextArea.RIGHT value is 3
But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)
Meanwhile TextArea.Left value is 1
That's why it matched the value in the switch and was aligned to RIGHT
BTW, if you set
textArea.setAlignment(Component.RIGHT);
it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !
You only have to write 'TextArea.RIGHT' instead of 'RIGHT'
textArea.setAlignment(TextArea.RIGHT);
You can use the following line:
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Resources