Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android button text and html

how do I use html in an android widget Button?

like image 310
user121196 Avatar asked Jul 14 '09 20:07

user121196


People also ask

How do I code a button in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

What is the HTML element for button?

The <button> HTML element is an interactive element activated by a user with a mouse, keyboard, finger, voice command, or other assistive technology. Once activated, it then performs a programmable action, such as submitting a form or opening a dialog.

What is android ImageButton?

Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.

How to change button style android Studio?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


2 Answers

You should be able to use Html.fromHtml() to convert raw HTML into a Spanned object that you can use in a setText() call on your button. As the Html documentation states, "Not all HTML tags are supported".

like image 139
CommonsWare Avatar answered Sep 18 '22 17:09

CommonsWare


Mark's answer is spot on, as usual. Here is a more complete example for reference though (this is a confusing part of the docs). (And, yes, I know this example isn't using a Button, but this is the one I had handy, and it's the same idea.)

String needs to be a resource:

 <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="mystring">    
        You can use regular text, and escaped HTML markup
        &lt;br /&gt;&lt;br /&gt;
        A simple BOLD example &lt;b&gt;StackOverflow&lt;/b&gt;.
    </string>
    </resources>

Then get the resource and use Html.fromHtml() (if you are using an EditText, you also need to make sure the buffer is set to SPANNABLE):

 public class MyActivity extends Activity {
    TextView myTextView;

       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.about);     

          myTextView = (TextView) this.findViewById(R.id.mytextview);
          myTextView.setText(Html.fromHtml(getResources().getString(R.string.mystring)),
                             TextView.BufferType.SPANNABLE);
       }  
       ...

Lastly, note that all HTML doesn't work, of course. So depending on your requirements, this might not be entirely useful. Also see this issue if you try to use a link (anchor tag) and you want it to respond to being clicked. For simple stuff, see Linkify.

like image 23
Charlie Collins Avatar answered Sep 21 '22 17:09

Charlie Collins