Rectangle 27 246

textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface is the Attribute textStyle.

As Shankar V added, to preserve the previously set typeface attributes you can use:

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);

I think you should loose the DEFAULT: holder.title.setTypeface(holder.title.getTypeface(), Typeface.BOLD);

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 243

textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface is the Attribute textStyle.

As Shankar V added, to preserve the previously set typeface attributes you can use:

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);

I think you should loose the DEFAULT: holder.title.setTypeface(holder.title.getTypeface(), Typeface.BOLD);

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 79

Let's say you have a style called RedHUGEText on your values/styles.xml:

<style name="RedHUGEText" parent="@android:style/Widget.TextView">
    <item name="android:textSize">@dimen/text_size_huge</item>
    <item name="android:textColor">@color/red</item>
    <item name="android:textStyle">bold</item>
</style>

Just create your TextView as usual in the XML layout/your_layout.xml file, let's say:

And in the java code of your Activity you do this:

TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
textViewTitle.setTextAppearance(this, R.style.RedHUGEText);

It worked for me! And it applied color, size, gravity, etc. I've used it on handsets and tablets with Android API Levels from 8 to 17 with no problems.

Remember... this is useful only if the style of the text really depends on a condition on your Java logic or you are building the UI "on the fly" with code... if it doesn't, it is better to just do:

<TextView android:id="@+id/text_view_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content 
    android:text="FOO" 
    style="@style/RedHUGEText" />

You can always have it your way!

This will only set a subset of attributes that can be defined in a <style> tag. It does the common ones (size, color, style), but doesn't apply others (including padding, background, gravity, etc)

there are two methods, one for api < 23, and one for api 23+. They appear identical except the one for <23 takes a context argument and the one for 23+ does not. Under the hood, the one for api 23+ calls the method for <23 and uses the member context for the textview.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 78

Let's say you have a style called RedHUGEText on your values/styles.xml:

<style name="RedHUGEText" parent="@android:style/Widget.TextView">
    <item name="android:textSize">@dimen/text_size_huge</item>
    <item name="android:textColor">@color/red</item>
    <item name="android:textStyle">bold</item>
</style>

Just create your TextView as usual in the XML layout/your_layout.xml file, let's say:

And in the java code of your Activity you do this:

TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
textViewTitle.setTextAppearance(this, R.style.RedHUGEText);

It worked for me! And it applied color, size, gravity, etc. I've used it on handsets and tablets with Android API Levels from 8 to 17 with no problems.

Remember... this is useful only if the style of the text really depends on a condition on your Java logic or you are building the UI "on the fly" with code... if it doesn't, it is better to just do:

<TextView android:id="@+id/text_view_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content 
    android:text="FOO" 
    style="@style/RedHUGEText" />

You can always have it your way!

This will only set a subset of attributes that can be defined in a <style> tag. It does the common ones (size, color, style), but doesn't apply others (including padding, background, gravity, etc)

there are two methods, one for api < 23, and one for api 23+. They appear identical except the one for <23 takes a context argument and the one for 23+ does not. Under the hood, the one for api 23+ calls the method for <23 and uses the member context for the textview.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 57

setTextAppearance
setTextTypeface

there is a new method for api 23+ that just doesn't take a context argument.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 57

setTextAppearance
setTextTypeface

there is a new method for api 23+ that just doesn't take a context argument.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 38

String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text_view_str));
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
SpannableString spannablecontent=new SpannableString(o.content.toString());
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                         0,spannablecontent.length(), 0);
// set Text here
tt.setText(spannablecontent);
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>

</resources>

 tv.setTextAppearance(getApplicationContext(), R.style.boldText);

or if u want through xml

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 38

String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text_view_str));
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
SpannableString spannablecontent=new SpannableString(o.content.toString());
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                         0,spannablecontent.length(), 0);
// set Text here
tt.setText(spannablecontent);
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>

</resources>

 tv.setTextAppearance(getApplicationContext(), R.style.boldText);

or if u want through xml

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 5

This question is asked in a lot of places in a lot of different ways. I originally answered it here but I feel it's relevant in this thread as well (since i ended up here when I was searching for an answer).

There is no one line solution to this problem, but this worked for my use case. The problem is, the 'View(context, attrs, defStyle)' constructor does not refer to an actual style, it wants an attribute. So, we will:

  • Create a style that you want to use
  • Apply a style for that attribute on our theme
  • Create new instances of our view with that attribute

In 'res/values/attrs.xml', define a new attribute:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customTextViewStyle" format="reference"/>
    ...
</resources>
<style name="CustomTextView">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:paddingLeft">14dp</item>
</style>

In 'res/values/themes.xml' or 'res/values/styles.xml', modify the theme for your application / activity and add the following style:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
    </style>
    ... 
</resources>

Finally, in your custom TextView, you can now use the constructor with the attribute and it will receive your style

public class CustomTextView extends TextView {

    public CustomTextView(Context context) {
       super(context, null, R.attr.customTextView);
    }
}

It's worth noting that I repeatedly used customTextView in different variants and different places, but it is in no way required that the name of the view match the style or the attribute or anything. Also, this technique should work with any custom view, not just TextViews.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 5

This question is asked in a lot of places in a lot of different ways. I originally answered it here but I feel it's relevant in this thread as well (since i ended up here when I was searching for an answer).

There is no one line solution to this problem, but this worked for my use case. The problem is, the 'View(context, attrs, defStyle)' constructor does not refer to an actual style, it wants an attribute. So, we will:

  • Create a style that you want to use
  • Apply a style for that attribute on our theme
  • Create new instances of our view with that attribute

In 'res/values/attrs.xml', define a new attribute:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customTextViewStyle" format="reference"/>
    ...
</resources>
<style name="CustomTextView">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:paddingLeft">14dp</item>
</style>

In 'res/values/themes.xml' or 'res/values/styles.xml', modify the theme for your application / activity and add the following style:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
    </style>
    ... 
</resources>

Finally, in your custom TextView, you can now use the constructor with the attribute and it will receive your style

public class CustomTextView extends TextView {

    public CustomTextView(Context context) {
       super(context, null, R.attr.customTextView);
    }
}

It's worth noting that I repeatedly used customTextView in different variants and different places, but it is in no way required that the name of the view match the style or the attribute or anything. Also, this technique should work with any custom view, not just TextViews.

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 2

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
textview.setTypeface(Typeface.DEFAULT_BOLD);

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 2

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
textview.setTypeface(Typeface.DEFAULT_BOLD);

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 1

Ive resolved it with two simple methods.

<style name="SearchInfoText">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">@color/Church_Grey</item>
    <item name="android:shadowColor">@color/Shadow_Church</item>
    <item name="android:shadowRadius">3</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
</style>
TextView locationName = new TextView(getSupportActivity());
    locationName.setId(IdGenerator.generateViewId());
    locationName.setText(location.getName());
    locationName.setLayoutParams(super.centerHorizontal());
    locationName.setTextSize(24f);
    locationName.setPadding(0, 0, 0, 15);
    locationName.setTextColor(getResources().getColor(R.color.Church_Grey));
    locationName.setShadowLayer(3, 1, 1,  getResources().getColor(R.color.Shadow_Church));

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 1

Ive resolved it with two simple methods.

<style name="SearchInfoText">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">@color/Church_Grey</item>
    <item name="android:shadowColor">@color/Shadow_Church</item>
    <item name="android:shadowRadius">3</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
</style>
TextView locationName = new TextView(getSupportActivity());
    locationName.setId(IdGenerator.generateViewId());
    locationName.setText(location.getName());
    locationName.setLayoutParams(super.centerHorizontal());
    locationName.setTextSize(24f);
    locationName.setPadding(0, 0, 0, 15);
    locationName.setTextColor(getResources().getColor(R.color.Church_Grey));
    locationName.setShadowLayer(3, 1, 1,  getResources().getColor(R.color.Shadow_Church));

java - Android - set TextView TextStyle programmatically? - Stack Over...

java android textview
Rectangle 27 8

TextView headerValue = (TextView) header.findViewById(R.id.list_header);
View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
    TextView headerValue = (TextView) header.findViewById(R.id.list_header);
    headerValue.setText( this.getString(R.string.headerSupervise) );

    listView.addHeaderView(header);
    listView.setAdapter(adapter);

Works!!!Thks...can you please explain me what was the problem?

I think I got it: I was getting the TextView headerValue from the context and not from the View header. Correct?

Correct!! If it solved your issue then close the question.Happy Coding

java - Dynamically set text in a TextView inside a Layout - Android - ...

java android android-layout android-textview
Rectangle 27 5

android:autoLink="web"
<TextView
    android:id="@+id/txt_post_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:autoLink="web"
    android:text=""
    android:textColor="@color/wall_msg"
    android:textSize="16sp" />

How to set a hyperlink in clickable textview? Android Java - Stack Ove...

java android url hyperlink textview
Rectangle 27 5

android:autoLink="web"
<TextView
    android:id="@+id/txt_post_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:autoLink="web"
    android:text=""
    android:textColor="@color/wall_msg"
    android:textSize="16sp" />

How to set a hyperlink in clickable textview? Android Java - Stack Ove...

java android url hyperlink textview
Rectangle 27 3

Have you tried using html to achieve this? So for example have the following as your text and then setting

String text = "<a href='www.link.com'>Click here</a>";

textView.setText(Html.fromHtml(text));

How to set a hyperlink in clickable textview? Android Java - Stack Ove...

java android url hyperlink textview
Rectangle 27 3

Have you tried using html to achieve this? So for example have the following as your text and then setting

String text = "<a href='www.link.com'>Click here</a>";

textView.setText(Html.fromHtml(text));

How to set a hyperlink in clickable textview? Android Java - Stack Ove...

java android url hyperlink textview
Rectangle 27 9

TextView
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL));

@greg7gkb tv.setTypeface(null, Typeface.BOLD); won't this do the same(clear an existing typeface style)?

android - Set TextView style (bold or italic) - Stack Overflow

android android-textview