Vlad.mir
Vlad.mir

Reputation: 695

How to get the text onClick on a TextView that is created dynamically?

I have this code so far, to create TextViews with the texts from usernames arrayList.

    TextView txt_con = null;

    for(int i=0; i<usernames.size(); i++)
    {
        txt_con = new TextView(this);
        txt_con.setLayoutParams(lparams);
        txt_con.setPadding(0, 30, 0, 0);
        txt_con.setText(usernames.get(i));
        ll_cont.addView(txt_cont);
    }

and the onClickListener

    txt_con.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            @SuppressWarnings("unused")
            TextView t = ((TextView)v);
            String str = t.getText().toString();
        }
    });

But, it only takes the onClick action on the last TextView. How do i get the onClick action in all of the TextViews ?

Upvotes: 4

Views: 1238

Answers (8)

Sakshi
Sakshi

Reputation: 141

TextView txt_con = null;

for(int i=0; i<usernames.size(); i++)
{
    txt_con = new TextView(this);
    txt_con.setLayoutParams(lparams);
    txt_con.setPadding(0, 30, 0, 0);
    txt_con.setText(usernames.get(i));
    ll_cont.addView(txt_cont);

    txt_con.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        @SuppressWarnings("unused")

        String str = txt_con.getText().toString();
    }
});
}

Upvotes: 4

burntsugar
burntsugar

Reputation: 58080

Declare the TextView and set the onClickListener inside the loop.

for(int i=0; i<usernames.size(); i++)
{
    TextView txt_con = new TextView(this);
    txt_con.setLayoutParams(lparams);
    txt_con.setPadding(0, 30, 0, 0);
    txt_con.setText(usernames.get(i));

   txt_con.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        @SuppressWarnings("unused")
        TextView t = ((TextView)v);
        String str = t.getText().toString();
    }
});

    ll_cont.addView(txt_cont);

}

Upvotes: 1

Narendra Singh
Narendra Singh

Reputation: 4001

Put this inside your for loop -

txt_con.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        @SuppressWarnings("unused")
        TextView t = ((TextView)v);
        String str = t.getText().toString();
    }
});

Do it before -

ll_cont.addView(txt_cont);

Upvotes: 1

Sarthak Mittal
Sarthak Mittal

Reputation: 6124

Try it like this:

TextView txt_con = null;

for(int i=0; i<usernames.size(); i++)
{
    txt_con = new TextView(this);
    txt_con.setId(i);
    txt_con.setOnClickListener(this);
    txt_con.setLayoutParams(lparams);
    txt_con.setPadding(0, 30, 0, 0);
    txt_con.setText(usernames.get(i));
    ll_cont.addView(txt_cont);
}

Implement OnClickListener from your Activity and do something like this:

 @Override
    public void onClick(View v) {
        switch(v.getId())
        {
           case 0:{
           //do something
           }
           case 1:{
           //do something
           }
           // and so on
        }
    }

Setting the Id for the TextView inside the for loop will help fetch them in the onClick callback.

Upvotes: 0

Kostya Khuta
Kostya Khuta

Reputation: 1856

 for(int i=0; i<usernames.size(); i++)
    {
        txt_con = new TextView(this);
        txt_con.setLayoutParams(lparams);
        txt_con.setPadding(0, 30, 0, 0);
        txt_con.setText(usernames.get(i));
        txt_con.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // This will be called for every textView separately    
            // YOUR Code  
        }
    });
    ll_cont.addView(txt_cont);

}

Upvotes: 1

Aegis
Aegis

Reputation: 5801

Why not make a simple listView with a ArrayListAdapter that takes the list op of usernames? Then you get onItemClick for free?

Upvotes: 0

krikara
krikara

Reputation: 2445

You are using a single variable for TextViews, and keep overwriting it.

What you should do is create an array of Text Views like this

int textViewCount = usernames.size();
TextView[] textViewArray = new TextView[textViewCount];

for(int i = 0; i<usernames.size(); i++) {
    textViewArray[i] = new TextView(this);
    textViewArray[i].setLayoutParams(lparams);
    textViewArray[i].setPadding(0, 30, 0, 0);
    textViewArray[i].setText(usernames.get(i));
    ll_cont.addView(txt_cont);
}

Upvotes: 1

Don Chakkappan
Don Chakkappan

Reputation: 7560

Yes . It should be like that.

You are referencing the same variable in the for loop. onClick listener will work only for the last TextView

Upvotes: 0

Related Questions