i am programming a game with andengine and i am using Andengine Text to display the high score...
Thats the code:
StrokeFont mFont = FontFactory.createStrokeFromAsset(this.getFontManager(), mainFontTexture, this.getAssets(), "Roboto-BoldItalic.ttf", 100, true, Color.WHITE, 2, Color.BLACK);
mFont.load();
text_score_menu = new Text(25, 25, mFont, "Score: ",getVertexBufferObjectManager());
if(LC.Score>Constants.highScore){
prefs.edit().putInt("highScore",LC.Score).commit();
text_score_menu.setText("New High: " + LC.Score);
}else{
text_score_menu.setText("Score: " + LC.Score);
}
Problem is that when there is new high score i am getting a new exception exactly here:
text_score_menu.setText("New High: " + LC.Score);
but i dont have that problem when the score is not new highscore and score is display with
text_score_menu.setText("Score: " + LC.Score);
here is the error message:
FATAL EXCEPTION: UpdateThread
java.lang.ArrayIndexOutOfBoundsException: length=210; index=210
at org.andengine.entity.text.vbo.HighPerformanceTextVertexBufferObject.onUpdateVertices(HighPerformanceTextVertexBufferObject.java:121)
at org.andengine.entity.text.Text.onUpdateVertices(Text.java:335)
at org.andengine.entity.text.Text.setText(Text.java:223)
this is a common pitfall with text in AndEngine - when you instantiated the text_score_menu entity, you set the max length then. Try changing this
text_score_menu = new Text(25, 25, mFont, "Score: ",getVertexBufferObjectManager());
to
text_score_menu = new Text(25, 25, mFont, "New High: 123456789",getVertexBufferObjectManager());
that will establish a long enough text field - then be sure to "set" the proper text before showing the entity - (as you are doing in the code you presented)
Related
My NetBeans recently updated new CodenameOne plugin and it looks like the Button.setTextPosition(Label.LEFT); renders wrong position of the icon on Android and iOS, the icon always overlaps with the text. It still renders with the simulator correctly and the function still renders with Label.RIGHT, Label.TOP and Label.BOTTOM parameters correctly. Do you have any idea?
Thanks,
William
Shai, I think the problem is that the icon was scaled to font.getHeight() of the button.
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Test", new BoxLayout(BoxLayout.Y_AXIS));
Font font = new Button("Test").getUnselectedStyle().getFont();
Image t = pictures.getImage("Play.png").scaled(font.getHeight(), font.getHeight());
hi.add(b(Button.LEFT, " Left 123", t))
.add(b(Button.RIGHT, " Right ", t))
.add(b(Button.TOP, " Top ", t))
.add(b(Button.BOTTOM, " Bottom ", t));
hi.add(b(Button.LEFT, " Left ", t))
.add(b(Button.RIGHT, " Right ", t))
.add(b(Button.TOP, " Top ", t))
.add(b(Button.BOTTOM, " Bottom ", t));
hi.show();
}
private Button b(int pos, String txt, Image t) {
Button btn = new Button(txt, t);
btn.setTextPosition(pos);
return btn;
}
Here is the screenshots:
Simulator:
Nexus (android):
I see the issue there, its a regression that only occurs with smaller icons since the width of the icon is used instead of the width of the text only in the case of left aligned text which is more rare.
We'll fix this in the next server update.
Is it possible change size of temperature?
remoteViews.setTextColor(R.id.battery, Color.WHITE);
remoteViews.setTextViewText(R.id.battery, String.valueOf((int)batteryLevel + "%" + "|" + temperatura + "°C"));
Batterylevel and temperature are in the same textview. I want change size only of temperature. Actually is 50dp. I want 20dp.Hopw can i do it?
You can change the size using HTML code, but I don't think it is possible to specify detailed sizes in dp with it.
In your case I would use the tag <small> for the temperature:
remoteViews.setTextColor(R.id.battery, Color.WHITE);
String styledText = String.valueOf((int)batteryLevel) + "%" + "|" + "<small>" + temperatura + "°C</small>";
remoteViews.setTextViewText(R.id.battery, Html.fromHtml(styledText));
The list of supported tags is described here:
http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/
Running on my Samsung Galaxy Note, the below code logs 28.0 for each log statement. Am I doing something wrong?
label = new TextView(context);
Log.e("text size", "" + label.getTextSize());
label.setTextAppearance(context, android.R.attr.textAppearanceLarge);
Log.e("text size", "" + label.getTextSize());
label.setTextAppearance(context, android.R.attr.textAppearanceSmall);
Log.e("text size", "" + label.getTextSize());
Use the style class, not attr.
label.setTextAppearance(context, android.R.style.TextAppearance_Large);
This same point of confusion was reported here: TextView.setTextAppearance not working.
I need to display multiple lines of text in an Alert Dialog. If I use multiple setMessage() methods, only the last setMessage is displayed, as shown below.
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Statistics:");
alertDialog.setMessage("No. of attempts: " + counter);
alertDialog.setMessage("No. of wins: " + counterpos);
alertDialog.setMessage("No. of losses: " + counterneg);
Is there a way to create a new line for each of these in the dialog? Like using \n in System.print.out(); method.
Thanks!
You can do something like this
String alert1 = "No. of attempts: " + counter;
String alert2 = "No. of wins: " + counterpos;
String alert3 = "No. of losses: " + counterneg;
alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3);
You could just create one string of everything you want to show and add "\n" where you'd like the line breaks to be.
alertDialog.setMessage("No. of attempts: " + counter + "\n" +
"No. of wins: " + counterpos + "\n" +
"No. of losses: " + counterneg);
Or even better to use a StringBuilder:
StringBuilder sb = new StringBuilder();
sb.append("No. of attempts: " + counter);
sb.append("\n");
sb.append("No. of wins: " + counterpos);
sb.append("\n");
sb.append("No. of losses: " + counterneg);
alertDialog.setMessage(sb.toString());
And the best way to do it would be to extract the static texts into a string resource (in the strings.xml file). Use the %d (or %s if you want to insert strings rather than ints) to get the dynamic values in the right places:
<string name="alert_message">No. of attempts: %1$d\nNo. of wins: %2$d\nNo. of losses: %3$d</string>
And then in code:
String message = getString(R.string.alert_message, counter, counterpos, counterneg);
alertDialog.setMessage(message);
You can also insert newlines directly in the strings.xml file:
<string name="my_string_text">This would revert your progress.\n\n Are you sure you want to proceed?</string>
Kotlin simplifies the solution by:
chaining the calls of the set methods
using interpolated strings
as follows:
"AlertDialog.Builder
This Builder object to allow for chaining of calls to set methods
"
(https://developer.android.com/reference/android/app/AlertDialog.Builder)
fun alertDemo() {
var counter: Int = 5
var counterpos: Int = 2
var counterneg: Int = 3
val builder = AlertDialog.Builder(this)
.setTitle("Statistics:")
.setMessage("""
|number of
|
|attempts: $counter
|wins: $counterpos
|losses: $counterneg
""".trimMargin())
.show()
}
I had prepared a screen-shot of the result, but as I am new here, I appear to have learned that uploading screenshots may be restricted to higher level community peers. Or did I miss something? Thank you for enlighting (perhaps not only) myself :)
The screenshot comes in a nice format without showing the bars.
PS:
For the minimalists, due to chaining, we could even eliminate the redundant "val builder ="
I have been developing a game with opengles for android and have a glsurfaceview displayed. I also have a textview displayed over this by using
addContentView(mytextview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) );
mytextview contains a string which is made up of a string plus the parsed value of an int. eg something like
mytextview = new TextView(this);
mytextview.setText("Score: " + Integer.toString(score) );
The textview and the integer value display but
My question is how do I update this when the integer value changes?
you'll have to call mytextview.setText("Score: " + Integer.toString(score) );
every time the score changes.