2

Possible Duplicate:
android : set textView style at runtime

i just wanna know how i can implement style in runtime i saw some posts here but i couldnt find anything properly

i create a new Textview , like this

TextView title = TextView(this, null, R.style.TitleSep);

this its my style xml.

 <style name="TitleSep">
        <item name="android:layout_height">fill_parent</item>
         <item name="android:layout_width">wrap_content</item>
        <item name="android:lineSpacingMultiplier">1.1</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#ffff0000</item>
         <item name="android:padding">2dip</item>
    </style>

Nothing change at all, i saw the api and should be change the style instead i got the default style.

Any advice.

Cheers Ron

Community
  • 1
  • 1
ron
  • 450
  • 9
  • 23
  • 1
    its not the same question, in that question they set some attributes, when i create the object i pass the style but doesnt work! cheers – ron Apr 14 '11 at 06:31
  • i think u pass the attribute set as null.this is a problem – Pinki Apr 14 '11 at 06:39
  • Pinki what do u meant with set as null this could be a problem? – ron Apr 14 '11 at 06:41

2 Answers2

8

You can define the TextView in separated layout file (like Rajath DSouza suggested). And then load the view from layout file (inflate it) dynamically. For example:

TextView title = (TextView) activity.getViewInflate().inflate(R.layout.styled_textview, null, null);
GrAnd
  • 10,141
  • 3
  • 31
  • 43
  • Thanks i will check it, at the moment i try to make all my textview invisible and if the data cames i set to visible, thanks – ron Apr 14 '11 at 07:14
0

If you can use the layout file, do the following - it's simpler.

<TextView
    style="@style/TitleSep"
    android:text="hello" />

UPDATE: (from comment)

Why don't create the textview at compile time, but make it invisible, and set it to visible if and when you need to show it. The reason I suggest this roundabout method is because setting the style at compile time seems simpler.

Rajath
  • 11,787
  • 7
  • 48
  • 62
  • Ron asking it in runtime only – Pinki Apr 14 '11 at 06:37
  • I cant do that, because i got data from the service and this data tells me if i have to create the textview. Cheers m8 – ron Apr 14 '11 at 06:38
  • Why don't create the textview at compile time, but make it invisible, and set it to visible if and when you need to show it. The reason I suggest this roundabout method is because setting the style at compile time seems simpler. – Rajath Apr 14 '11 at 06:40
  • Thanks Rajath DSouza, im looking to invisible textView – ron Apr 14 '11 at 07:14
  • Great. If you found it useful, please help restore my points by upvoting this answer and/or accepting it :D – Rajath Apr 14 '11 at 07:23
  • can u edit the answer, because that its wrong, i'll do it just change u answer please – ron Apr 14 '11 at 07:25