How do I get the onClick text in a TextView that is dynamically generated?
I have this code so far to create TextViews with texts from namesernames 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 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 this is only the onClick action for the last TextView. How do I get the onClick action in all TextViews?
+3
source to share
8 answers
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();
}
});
}
+3
source to share
You use one variable for TextViews and store it.
What you need to 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);
}
+1
source to share
Place this inside a 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);
+1
source to share
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);
}
+1
source to share
Declare TextView and set 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);
}
+1
source to share
Try the following:
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);
}
Inject an 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 an id for the TextView inside the for loop will help get them in the onClick callback.
0
source to share