6

I am making an android application,I have 4 textviews namely ProductId,Title,description,image.I want when i click on each one of them product id should be displayed.I have a webservice for this.

Output of webservice is

vmsc>
<response code="0" message="Success"/>
−
<responsedata>
−
<productcategories>
−
<productcategory>
<id>1</id>
<title>Celebrities</title>
<description>Celebrities</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>2</id>
<title>Music</title>
<description>Music</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>3</id>
<title>Sports</title>
<description>Sports</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>4</id>
<title>Fashion</title>
<description>Fashion</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>5</id>
<title>Religion</title>
<description>Religion</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>6</id>
<title>Others</title>
<description>Others</description>
<image>
        </image>
</productcategory>
</productcategories>
</responsedata>
</vmsc>

Thanks in advance Tushar

MByD
  • 135,866
  • 28
  • 264
  • 277
Tushar
  • 5,907
  • 15
  • 49
  • 81

4 Answers4

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

});
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
7

you can also do it using xml like

      <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="handleOnClick"
        android:clickable="true"
        android:text="Clickable TextView"
        android:textSize="30sp" />

and handle onClick like

public void handleOnClick(View view)
{
   switch(view.getId())
   { 
      case R.id.textView:
      //do your stufff here..
      break;
      default: 
      break;
   }
}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
1

You can simply create OnClickListeners of your textviews like:

TextView textview = new TextView(this);
        textview.setText("This is a textview");

        textview.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // do something here.
            }
        });
mudit
  • 25,306
  • 32
  • 90
  • 132
0

this click listener works:

setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View viewIn) {
                try {
                    Log.d(TAG,"GMAIL account selected");
                } catch (Exception except) {
                    Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
                }
            }
        });

the text view is declared with no reference to android:clickable="true" (default wizard):

        <TextView
            android:id="@+id/tvGmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/menu_id_google"
            android:textSize="30sp" />
tony gil
  • 9,424
  • 6
  • 76
  • 100