Narendra
Narendra

Reputation: 1868

How to add onclicklistener to dynamically generated text view?

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

    }

Upvotes: 8

Views: 20704

Answers (9)

Vinod Makode
Vinod Makode

Reputation: 973

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

Upvotes: 0

Khuswant Singh
Khuswant Singh

Reputation: 31

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.

Upvotes: 3

Rajesh Narwal
Rajesh Narwal

Reputation: 908

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
    }

Upvotes: 0

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

        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

Upvotes: 1

kosa
kosa

Reputation: 66637

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

Upvotes: 0

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

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

Upvotes: 0

idiottiger
idiottiger

Reputation: 5177

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.

Upvotes: 1

Hiren Dabhi
Hiren Dabhi

Reputation: 3713

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.

Upvotes: 14

Vipin Sharma
Vipin Sharma

Reputation: 151

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

Upvotes: 0

Related Questions