Updating variables in addcontentview - android - android

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.

Related

Android TextView created dynamically becomes gray

So I have a LinearLayout then inside is a ScrollView then inside is another RelativeLayout and finally inside is another LinearLayout with id mylinearlayout.
I want to create TextView's dynamically it works fine, but the text seems to be disabled like GRAY color. Although i didn't set any color..
Here's my code:
final LinearLayout linlayout = (LinearLayout) findViewById(R.id.mylinearlayout);
final TextView tv = new TextView(getApplicationContext());
tv.setTextSize(20);
tv.isEnabled();
tv.setGravity(Gravity.CENTER);
if(orderstatus.equals("Pending")) {
tv.setText("OrderID: "+orderid + "\n"+"OrderDate: " + orderdate + "\n" +"OrderStatus: "+orderstatus + " \n\n");
tv.setId(i + 5);
linlayout.addView(tv);
}
Use this code
linlayout = (LinearLayout) findViewById(R.id.mylinearlayout);
TextView tv = new TextView(getApplicationContext());
tv.setTextSize(20);
tv.isEnabled();
tv.setGravity(Gravity.CENTER);
if(orderstatus.equals("Pending"))
{
tv.setText("OrderID: "+orderid + "\n"+"OrderDate: " + orderdate + "\n" +"OrderStatus: "+orderstatus + " \n\n");
tv.setId(i + 5);
tv.setTextColor(Color.BLACK);
linlayout.addView(tv);
}
You doing well, but miss one moment. When you add view into layout, view automaticly sets color from current system styles. If you need to change color of your text, do this:
tv.setTextColor(YOUR_COLOR);
Your TextView text may grey because it's using the system Theme rather than your Activity's. You could try passing this instead of getApplicationContext() in your TextView() constructor. This is true for Toast objects, for example, and may also apply to widgets.

Strange Glitch For Loop Inflating Views

I am trying to take a string (which is formatted HTML) and split it at every double enter. I know that before and after a double enter will be a p block tag, and I'm using a for loop to split it in this way. Here is my code
for(String s : rawHTML.split("\\n\\n")){
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
comm.setText(Html.fromHtml(s) );
Expected behavior from this is every "Currently is" in the log should be blank. If there are two or more p blocks, though, when I setText, it overwrites the previous text.
For example, I have a string
<p>Test</p>\n\n<p>Hello</p>
I should be seeing two TextViews like so
[Test]
[Hello]
Instead, I see this
[Hello]
[]
I am really stumped and can't figure out this strange issue.
Thank you!
The following code snippet shoud work for you.
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
for(String s : rawHTML.split("\\n\\n")){
comm.append(Html.fromHtml(s) );
}

Grab TextView (in a ListVIew Row ) from ContextMenu

I know to get a string of a specific TextView in a ListView, I can do this:
ReviewUser = ((TextView) rowView.findViewById(R.id.labelUser))
.getText().toString();
What if I want to get the TextView itself?
The TextView is an integer and I simply want to get the TextView and add 1 to it.
So you already specified that you know how to get the specific text from your ListView. Since you want to modify that same TextView, the rest is simple. This code is lengthier just to show the steps.
TextView textView = (TextView) rowView.findViewById(R.id.labelUser)
String text = textView.getText().toString();
int num = Integer.valueOf (text).intValue() + 1;
textView.setText (""+num);
Also, if you are working with a String ArrayAdapter and you know the index of the row you want to modify, what you can do is (assuming arrayAdapter is initialized and index is your index variable):
String text = arrayAdapter.get(index);
arrayAdapter.remove (text);
arrayAdapter.insert ((Integer.valueOf (text).intValue() + 1 ) + "", index);

How to show two values in a single textview on different lines

How do you show two values on different lines using a single testview? I can get this by using two textviews but I want to show values in a single textview. What should I d0?
xmlRespone[3][1]= Salman Ahmed
xmlRespone[4][1]= 435000;
TextView lblWelcome = (TextView) findViewById(R.id.lblWelcome);
lblWelcome.setText("Welcome " + xmlRespone[3][1]);
TextView lblYouraccountbalance = (TextView)
findViewById(R.id.lblYouraccountbalance);
lblYouraccountbalance.setText(" balance :" + xmlRespone[4][1]);
Try "\n" for new line.
TextView lblWelcome = (TextView) findViewById(R.id.lblWelcome);
lblWelcome.setText("Welcome " + xmlRespone[3][1] + "\n" + xmlRespone[4][1]);
Output : Welcome Salman Ahmed
435000

Changing text within textview

So is it possible to change text size of part of the text in textview dynamically
Let's say we have a method that does that...what am I missing...I want to make x variable twice as big as y...they are string btw...
private void Data_transfer() {
test3.setText( x + " " + y);
}
You can use the Spannable type with a TextView to set multiple styles in one field.
In your case, you can do this like so, using the fromHtml() method which generates a Spannable string:
test3.setText( Html.fromHtml( "<font size='10'>" + x + "</font> <font size='5'>" + y + "</font>" ) );
And you can, of course, change 10 and 5 to whatever values you like.
private void Data_transfer() {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("Welcome to android");
}

Categories

Resources