How can I make a TextView look like a Button?


Solution 1

You should be able to just set the style in the layout XML file.

See for a list of the built-in platform style. Not sure how well it would work, but its reasonably easy to do. Try this:

<TextView android:layout_height="wrap_content" style="@android:style/Widget.Button" android:layout_marginRight="5sp" android:text="" android:layout_width="fill_parent"></TextView>

EDIT: the issue is that the default button style sets the android:clickable attribute. Try and add the android:clickable attribute and set it to false:

<TextView android:layout_height="wrap_content" style="@android:style/Widget.Button" android:layout_marginRight="5sp" android:text="" android:clickable="false" android:layout_width="fill_parent"></TextView>

Solution 2

You should just be able to create a Drawable of a button and use setBackgroundDrawable() on the TextView.

Alternatively, you could use the android:background XML attribute.

Solution 3

Take this complete layout from my project:

<?xml version="1.0" encoding="utf-8"?>

<!-- Solution 1: New Button appearance -->
<!--TextView xmlns:android=""
    android:clickable="false" /-->

<!-- Solution 2: Old Button appearance -->
<TextView xmlns:android=""
      android:gravity="center_vertical|center_horizontal" />

without clickable=false onItemClick() is not fired in ListView/GridView etc.

without focusable=false you have not the pressed button effect when you press a TextView button.

Author by


Updated on August 06, 2022


  • Admin
    Admin 10 months

    I have a ListActivity in Android. Each row is just an item that starts another activity with some resource. By default this is of course a TextView.

    I'd like (and by "I'd like", I mean "the client insists") that TextView to look like a button. If I actually make them Buttons, the default list click handler doesn't work anymore - even if they're non-focusable - so I have to write code to manually inflate the view containing the button and set a click handler.

    Is there some way instead I can just make the TextView look like a Button without any of a Button's behaviors?

  • Admin
    Admin about 12 years
    I am using a CursorAdapter and a ListActivity - ideally I never have to call any particular function on the "Button" because there's no code that knows it exists.
  • Will Tate
    Will Tate about 12 years
    edited answer to include the XML attribute you could use if you don't want to do it programmatically.
  • Admin
    Admin about 12 years
    This, somewhat amazingly, does not work. Simply having the Button style is enough to make the view block list click events.
  • Admin
    Admin about 12 years
    Is there some name I can use to get the "default" button style? Including my own drawable image will increase the program size and risk conflicting with the platform default.
  • Femi
    Femi about 12 years
    You should be able to just override the android:clickable attribute. See my edit.
  • Admin
    Admin about 12 years
    Finding the default image is not my concern - users having a different one for their system theme is.
  • Admin
    Admin about 12 years
    That works. Thanks. You just saved me from doubling the lines in this trivial activity.
  • Karan Harsh Wardhan
    Karan Harsh Wardhan about 4 years
    nice this lets me simply convert a textview to a button without having to do any other xml/programming changes
  • Partha Paul
    Partha Paul over 1 year
    How to give borderRadius to it??