views:

1062

answers:

5

Ok right , i asked how to create a random number from 1-100 for android and i came to this

    TextView tv = new TextView(this);

    int random = (int)Math.ceil(Math.random()*101); 

    tv.setText("Your Number Is..."+ random );

What this does is create the default kinda "hello world" style text view and says "Your Number Is.... [Then Random Number]

My problem is that i cant change the layout of this text , because it is not defined in XML, if someone could tell me how to change the style , or like make the random number into a string so i could use it for any Textview layout that would be great ..

Thanks :)

+1  A: 

Putting it into a string is easy.

String randomAsAString = Integer.toString(random)

You can then use the XML properties of the TextView to change its formatting, such as android:textSize="30dp" or android:textColor="#900".

By the way, if you're happy with the answer to your previous question, you should go back and mark an answer as "Accepted". That gives 'reputation' points to the person whose answer you accepted and closes the question so that people don't think you're still waiting for a better answer. You can read more about reputation in the FAQ.


Edit: You can't reference the string entirely in xml while still giving it a random number. This is because the "@string/some_string" format only allows unchangeable strings. The execption to this is using parameters, e.g. setting the string as

<string name="random_number">The random number is %d</string>

Then you could call up that string using something like

yourTextView.setText(this.getString(R.string.random_number, random))

As for your other question about setting a background to a textView, that's also easy.

yourTextView.setBackgroundDrawable(R.drawable.....)

You should take advantage of Eclipse's autocomplete feature... it makes finding these commands a lot easier. For example, simply type the name of your TextView followed by a period, pause half a second for the list of options to come up, then "setB" and it should then filter the list to the three setBackground Drawable/Resource/Color options.

Steve H
How do i know what string it has gone to ?
Dan
and i get a yellow underline underneath "randomAsAString" it says that it is never used
Dan
randomAsAString is the string it went into; it's just the silly name I picked. The yellow line telling you it isn't used means exactly that. If you don't then use the string somewhere, Eclipse warns you of that. You need to actually tell Android to set that string to the TextView. I can't get to Eclipse now, but it should be as simple as yourTextView.setText(randomAsAString) replacing with the correct names.
Steve H
Is there any way i can locate that string in xml , e.g I make a text view and do .. android:text="@string/randomAsAString" ?
Dan
I'll edit my answer to give more details.
Steve H
+1  A: 
  • If by change the style you mean the text color, text size, and you want to change them programmatically, have a look at the setTextColor and setTextSize methods.

More info here

  • If you want more advanced formatting to set programmatically, see this link.

The below example demonstrates how to make your text bold and italic.

tv.setText("Your Number Is..."+ random, TextView.BufferType.SPANNABLE );
Spannable myText = (Spannable) tv.getText();
myText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,myText.length(),0);

Edit: Try the below for the android:textSize="100dp" and android:gravity="center" :

tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 100);
tv.setGravity(Gravity.CENTER);
ccheneson
Could you tell me how to format my text so it is about 100dp and the gravity is in the center ?
Dan
@Dan: See my edit
ccheneson
Thankyou so much !! , but is there one of those commands to set the background to a R.drawable , the black background is abit boring , If you can answer that for me i will be very gratefull and mark this answer as the correct one :)
Dan
If you want for example have a green background: tv.setBackgroundColor(Color.GREEN); So you just replace the Color.GREEN with the color you want. You can also put your color under res/colors.xml and reference it
ccheneson
To set the background to a drawable, see the following example:tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));(Here "icon" refers to icon.png under res/drawable when creating a default project in Eclipse.
ccheneson
A: 

You could also do something like this.

Define your string in strings.xml like:

<string name="your_number_is">Your number is <xliff:g id="number">%s</xliff:g>.</string>

Create a TextView in a layout xml:

<TextView android:id="@+id/your_number_is"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/your_number_is"
    android:gravity="center"
    android:textSize="100dip"
    />

Then your code would look like:

TextView tv = (TextView) findViewById(R.id.your_number_is);
int random = (int)Math.ceil(Math.random()*101);
tv.setText(getString(R.string.your_number_is, random));

This will make it a lot easier when you later on would like to change your text or maybe localize your app.

johan
I get a red line under "(R.id.your_number_is)" and "(R.string.your_number_is"
Dan
Make sure that you have specified the TextView with the correct id "your_number_is" and have the entry in the strings.xml with the correct name "your_number_is". If that does not help and you are using Eclipse try to run "Fix Project Properties" under the "Android Tools" menu on the project.
johan
Thankyou so much !!! It works :D
Dan
A: 
tv.setText(Html.fromHtml("Your number is: <b>" + random + "</b>"));

For basic HTML text-styling tags.

Bloudermilk
A: 

if you have thead troubles use this:

new Thread(){ 
      public void run(){ 
            TextView v = (TextView)findViewById(R.id.mytext); 
            v.setText("TEST"); 
      } 
    }.start();
rear