Home » Android » android – How to add onclicklistener to dynamically generated text view?

android – How to add onclicklistener to dynamically generated text view?

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my application generating dynamic text view. I want to add onclickListener to my text view.
How can I do it please give me some hint.
Here hard code for textview .

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

    }
How to&Answers:

here is 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 useful to you.

Answer:

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 on this class.

Answer:

first create a 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.

Answer:

        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 generic code for dynamically adding on click listner on any view or view group dynamically

Answer:

Create one View.onClickListener() object “mListener”.
Add this object to tv.setOnClickListener(mListener) in for loop.

Answer:

You need to call setOnClickListener(..) on TextView Instance.
In the sample below we use 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");
     }
 });

Answer:

It is not different from the one you do when you create text in xml:

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

Answer:

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
    }

Answer:

Yes I have also getting the same problem but friend I got the solution and it’s worked for me

I have added Textview in run time and also get them click below is 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);
     }