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 http://developer.android.com/reference/android/R.style.html 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="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/Widget.DeviceDefault.Button"
android:textAllCaps="false"
android:focusable="false"
android:clickable="false" /-->
<!-- Solution 2: Old Button appearance -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/btn_default"
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.

Admin
Updated on August 06, 2022Comments
-
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 about 12 yearsI 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 about 12 yearsedited answer to include the XML attribute you could use if you don't want to do it programmatically.
-
Admin about 12 yearsThis, somewhat amazingly, does not work. Simply having the Button style is enough to make the view block list click events.
-
Admin about 12 yearsIs 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 about 12 yearsYou should be able to just override the
android:clickable
attribute. See my edit. -
Admin about 12 yearsFinding the default image is not my concern - users having a different one for their system theme is.
-
Admin about 12 yearsThat works. Thanks. You just saved me from doubling the lines in this trivial activity.
-
Karan Harsh Wardhan about 4 yearsnice this lets me simply convert a textview to a button without having to do any other xml/programming changes
-
Partha Paul over 1 yearHow to give borderRadius to it??