How to add onclicklistener to dynamically created text view?

My application creates a dynamic text view. I want to add onclickListener to a text view. How can I do this, please give me a little hint. Here's the hard code for text viewing.

for (int i = 0; i < subCategory.length; i++) { TextView tv = new TextView(this); tv.setText(subCategory[i]); tv.setId(i); sCategoryLayout.addView(tv); } 
+7
source share
9 answers

here is the code:

 TextView tv[] = new TextView[subCategory.length]; for (int i = 0; i < subCategory.length; i++) { tv[i] = new TextView(this); tv[i].setText(subCategory[i]); tv[i].setId(i); sCategoryLayout.addView(tv[i]); tv[i].setOnClickListener(onclicklistener); } 

onclicklistener method:

 OnClickListener onclicklistener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == tv[0]){ //do whatever you want.... } } }; 

hope you find it helpful.

+13
source
 for (int i = 0; i < subCategory.length; i++) { TextView tv = new TextView(this); tv.setText(subCategory[i]); tv.setId(i); tv.setOnClickListener(this); sCategoryLayout.addView(tv); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case 1: //Write Code For Click Here break; default: break; } } 

Impelement OnClickListner in this class.

+3
source

first create an onclick listener:

  OnClickListener listener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub final int id = v.getId(); //use id to process different text view } }; 

and then using tv.setOnClickListener(listener) to listen.

+1
source
  for(int i = 0 ;i<mediaList.size();i++){ view_media_gallery_item = LayoutInflater.from(view.getContext()).inflate(R.layout.e_media_gallery_item, null); TextView title = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_title); TextView subtitle = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_subtitle); ImageView flux_Title_Image =(ImageView) view_media_gallery_item.findViewById(R.id.media_gallery_item_img); title.setId(i+100); subtitle.setId(i+1000); flux_Title_Image.setId(2000+i); view_media_gallery_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("view media clicked"); Media m = (Media )v.getTag(); medialistner.setOnItemclick(m); } }); // flux_Title_Image.setBackgroundDrawable(mediaList.get(i).getThumb()); media_Gallery_List.addView(view_media_gallery_item); } } 

this common code for dynamically adding to a click list in any view or viewing group dynamically

+1
source

Create a single View.onClickListener () "mListener" object. Add this object to the tv.setOnClickListener (mListener) loop for the loop.

0
source

It is no different from what you do when creating text in xml:

 tv[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //your logic. } }); 
0
source

You need to call setOnClickListener (..) on the TextView instance. In the example below we use an anonymous inner class

  TextView tv = new TextView(this); tv.setText("sample"); tv.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Log.i("", "Inside onclick"); } }); 
0
source
 for (int i = 0; i <subCategory.length; i++) { tv[i] = new TextView(this); tv[i].setText(subCategory[i]); tv[i].setId(i); sCategoryLayout.addView(tv[i]); tv[i].setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub v.getId(); //here u can get the id on that basis u can perform any action //dont forget to implements OnClickListener in your activity } 
0
source

Yes, I have the same problem, but I have a solution, and it worked for me.

I added Textview at runtime, and also get them click below - this is the code.

 for (int i = 0; i < albumItemList.size(); i++) { toplayout = getActivity().getLayoutInflater().inflate(R.layout.addphotoalbumlistinfater, null); newTV = (TextView)toplayout.findViewById(R.id.textView) ; textViewcount= (TextView)toplayout.findViewById(R.id.textViewcount) ; ll = (RelativeLayout) toplayout.findViewById(R.id.mainitemlist) ; ll.setTag("position"+i); newTV.setText(albumItemList.get(i).getAlbumName()); textViewcount.setText(""+albumItemList.get(i).getCount()); ll.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(getActivity(),""+v.getTag(),Toast.LENGTH_SHORT).show(); } }); linearLayoutforaddchild.addView(toplayout); } 
0
source

All Articles