batman
batman

Reputation: 4928

Making to look TextView clickable, Android?

I'm very new to Android and I have some doubts.

I have a TextView:

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go Back" />

how come I make the text Go Back look like clickable one? I'm asking in terms of look and feel. TextView should be shown as clickable as how Button getting rendered.

Thanks in advance.

Upvotes: 3

Views: 5365

Answers (8)

Steph
Steph

Reputation: 115

You don't even need a listener:

<TextView
    android:id="@+id/needCheeseburger"
    android:clickable="true"
    android:onClick="getCheeseburger" />

And then just:

public void getCheeseburger(View view) {
    Intent intent = new Intent(this, giveMeCheeseburger.class);
    startActivity(intent);
}

This works perfectly fine for me.

Upvotes: 1

CoderDecoder
CoderDecoder

Reputation: 445

just do this

yourTextView.setOnClickListener(new OnClickListener() {

@Override
        public void onClick(View v) {
            // do your work here
        }
    });

Upvotes: 1

duggu
duggu

Reputation: 38439

Hope use full to u:-

boolean text_click =false;
TextView textview = (TextView) findViewById(R.id.textview1);
    public OnClickListener textOnClick = new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (Constants.text_click) {
            textview.setBackgroundResource(R.drawable.textbox);

            text_click = false;
        } else {

            textview.setBackgroundResource(R.drawable.textboxonpress);

            text_click = true;
        }

    }
};

Upvotes: 0

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

use like this android:clickable="true"

textview

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:text="Large Text" />

text.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

Upvotes: 0

Sebastian Breit
Sebastian Breit

Reputation: 6159

create a xml file in your drawable folder called "mybutton.xml" and write this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true">

    <item 
        android:state_pressed="true"
        android:drawable="@drawable/mybutton2"/>
    <item
        android:drawable="@drawable/mybutton1"/>

</selector>

Then add two png's into your drawables folder... mybutton1.png, mybutton2.png. So you have 2 different states for your button.

Now set the background to your textview:

android:background="@drawable/mybutton"

then, in your code you must set a clicklistener:

findViewById(R.id.mytextview).setOnClickListener(new OnClickListener(){

 @Override
 public void onClick(View v) {
    //your code goes here
 }

});

And that's all... You can use shapes instead of images too.

Upvotes: 3

user1744952
user1744952

Reputation: 518

You can use Touchlistener also:

textview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        });

Upvotes: 1

Misha Bhardwaj
Misha Bhardwaj

Reputation: 1387

For making your TextView clickable just add this to your TextView:

android:clickable="true"

after that you can set onClickListener to it by using this:

    yourTextView.setOnClickListener(new OnClickListener() {

    @Override
            public void onClick(View v) {
                // do your work here
            }
        });

Upvotes: 2

Android_coder
Android_coder

Reputation: 9993

Try this:

final TextView view = (TextView) findViewById(R.id.textview1);
view.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
// request your webservice here. Possible use of AsyncTask and ProgressDialog
// show the result here - dialog or Toast
 }

 };);

Upvotes: 1

Related Questions