How to use text as a button in Android

I want to use text as a button in Android. Suppose I have one text, for example "Register". I want to use this text as a button, when I click in the text, it will open the registration screen. I use XML to develop the user interface, and I do it for the Android Tablet Application.

Thanks in advance

+3
source share
4 answers

Set the following property in xml TextView:

android:background="#dadada"
    android:clickable="true"

Now in the java src file get this TextView and install OnClickListener.

    TextView tv=(TextView)findViewById(R.id.text);

    tv.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //perform your action here  
        }
    });
+4
source

The code used below.

In the Textview XML file, assign this property

android:clickable="true"

and in the java side, OnClickListener is used.

+3
source

View xml:

android:onClick="yourMethodName"

The context in which this Viewis used is the class this method should have (usually this is yours Activity)

For instance:

<TextView
  android:onClick="register"
  android:layout_width="wrap_parent"
  android:layout_height="wrap_parent"/>

In your context, you will need the following method (again, yours Activity):

public void register(View v) {
  //
}

This is what you can do with any type View. Here is the link for Android .

+1
source

In your XML file, use this for your TextView:

android:clickable="true"

Then in your source, set the click on the listener:

TextView txtRegister = (TextView) findViewById(R.id.txtRegister);
txtRegister .setOnClickListener(new OnClickListener() {

    public void onClick(View view) {
          your codes here
    }
});
+1
source

All Articles