3

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.

batman
  • 4,728
  • 8
  • 39
  • 45
  • means after click u want to change textview image if im right????? – duggu Feb 13 '13 at 11:15
  • It's duplicate question of this. http://stackoverflow.com/questions/5537043/making-a-textview-clickable-in-android – Shadow Feb 13 '13 at 11:19

8 Answers8

3

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.

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
2

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
            }
        });
Misha Bhardwaj
  • 1,377
  • 10
  • 14
1

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
 }

 };);
Android_coder
  • 9,953
  • 3
  • 17
  • 23
1

You can use Touchlistener also:

textview.setOnTouchListener(new OnTouchListener() {

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

just do this

yourTextView.setOnClickListener(new OnClickListener() {

@Override
        public void onClick(View v) {
            // do your work here
        }
    });
CoderDecoder
  • 445
  • 4
  • 18
1

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.

Steph
  • 117
  • 4
0

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

        }
    });
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
0

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;
        }

    }
};
duggu
  • 37,851
  • 12
  • 116
  • 113