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
I can get the text from a button: String buttonText = button. getText();
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.
onClick is prompted deprecated.
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.
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
}
});
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
}
});
Used Below Code.
In XML file Textview assign this property
android:clickable="true"
and in java side OnClickListener used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With