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

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

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.

Applying dimensions from dimens.xml to text formatted with Html.fromHtml

I have this code for setting the text of a TextView:
TextView txt = new TextView(this);
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />" + "<small>" + m.getText() + "</small>" + "<br />");
The <small> mark is working, but I'd like to set the text size according to my dimensions defined in the dimens.xml file, which I use for all other text in my application. Adding the TextView through an xml layout is not an option since I don't know how many TextViews I'll be adding.
Dimensions in the dimens.xml file are set up like <dimen name="text_size_320dp_small">16sp</dimen>.
How can I apply these dimensions to my text formatted with Html.fromHtml?
Thanks a lot.
I have tested following code myself. You can do it like this.
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />"
+ "<font textsize="
+ getResources().getDimension(R.dimen.text_size_320dp_small) + ">" + m.getText()
+ "</font>" + "<br />"));
[Updated]:
Just came up with some references and updates :
You can store this in strings.xml
<string name="mystring"><font size = "%s"></string>
In code you can write as:
int sptopx = getResources().getDimensionPixelSize(R.dimen.text_size_320dp_small);
Spanned modified = Html.fromHtml( context.getString(R.string.mystring, sptopx) );
myTextView.setText(spanned);
TextView txt = new TextView(this);
txt.setText(
Html.fromHtml(
"<b>" + m.getTitle() + "</b>" +
"<br />" +
modified +
">" + m.getText() + "</font>" +
"<br />"
)
);
for details about html tags support in TextViews you can check this link.
You can't directly, the small tag creates a RelativeSizeSpan with a proportion of .8f, which is hardcoded into the implementation of Html.fromHtml.
Leaves two options that I can see, set the text size to 20sp (which would make small work out to 16sp). Probably not ideal.
The other option is to use a custom tag <mySmall> by replacing all occurrences of <small> and </small> with <mySmall>& </mySmall>. And then call fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) with a TagHandler that integrates a AbsoluteSizeSpan into the output Editable.
Why don't you use txt.setSizeText(yoursize)? However you can retrieve your dimensions using this:
float yourDimen = getResources().getDimension(R.dimen.your_dimen_name);

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");
}

Update "Log.i" in TextView with ScrollBar

I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.
I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.

Updating variables in addcontentview - 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.

Categories

Resources