How to make TextView look like a button?

I have ListActivity in Android. Each line is simply an element that triggers another action using some resource. By default, this is of course TextView.

I would like (and "I would like", I mean "client insists") that the TextView will look like a button. If I actually make them buttons, the default click handler does not work anymore, even if they are not subject to focus - so I need to write code to manually inflate the view containing the button and set the click handler.

Is there any way I can just make a TextView look like a Button without any action with a Button?

+5
source share
4 answers

You just have to set the style in the XML layout file.

See http://developer.android.com/reference/android/R.style.html for a list of platform built-in style. Not sure how well this will work, but it's easy enough to do. Try the following:

<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 problem is that the default button style sets an attribute android:clickable. Try adding an attribute android:clickableand 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>
+8
source

You can simply create a button Drawableand use setBackgroundDrawable()on TextView.

Alternatively, you can use android:backgroundthe XML attribute.

+5
source

:

<?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" />

clickable = false onItemClick() ListView/GridView ..

focusable = false TextView.

0

XML:

android:clickable="true"

and in a set of java files:

TextView youtTextView=(TextView)findViewById(R.id.yourTxt);

youtTextView.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
       //code your button click action/event
    }
});
0
source

All Articles