Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a text as a Button in Android

Tags:

android

I want to use a text as a button in Android. Suppose I have one text like 'Register'. I want to use this text as a button means when i will click in the text, it will open the Registration Screen. I am using XML for developing the UI and i am doing it for Android Tablet Application.

Thanks in advance

like image 493
Arindam Mukherjee Avatar asked May 24 '11 04:05

Arindam Mukherjee


People also ask

How do you make a text button?

I can get the text from a button: String buttonText = button. getText();

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.

Is onClick deprecated in Android?

onClick is prompted deprecated.

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.


3 Answers

Set the following property in the xml of TextView:

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

Now in the java src file recieve this TextView and set OnClickListener.

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

    tv.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //perform your action here  
        }
    });
like image 91
Dinesh Sharma Avatar answered Sep 21 '22 03:09

Dinesh Sharma


In your XML file use this for your TextView :

android:clickable="true"

Then in your source set on click listener :

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

    public void onClick(View view) {
          your codes here
    }
});
like image 45
Pouyan Avatar answered Sep 19 '22 03:09

Pouyan


Used Below Code.

In XML file Textview assign this property

android:clickable="true"

and in java side OnClickListener used.

like image 26
Nikhil Avatar answered Sep 21 '22 03:09

Nikhil