making a textview clickable in android
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>
<开发者_Go百科;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
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
}
});
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;
}
}
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.
}
});
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" />
精彩评论