TextView onclickListener - android

In my application i have added textviews dynamically using array,and given id's for each with a count variable, which increment's count according to the addition of textviews.In onClickListener of each textview i want to perform some oparations,but when i'm trying to do this operation is getting performed on all textviews.
Below is the code,i'm not getting what's wrong.please help me.
// here i have added textview dynamically
mtxtview[colTextCount]=new TextView(this);
mtxtview[colTextCount].setId(colTextCount);
mtxtview[colTextCount].setLayoutParams(new LayoutParams(20,20));
And In onclickListener-
#Override
public void onClick(View v) {
System.out.println("onclick...");
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
//if i added 3 textview.its giving me all 3 textview's text(getText())
}
}

use switch-case and View.getId() to check which TextView is Clicked before starting for loop . try it as :
#Override
public void onClick(View v) {
System.out.println("onclick...");
switch(v.getId())
{
case _mTextViewId:
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
}
break ;
// same for others....
}
}

see TextView onClick() not working
You should do setOnClickListener for each TextView Object.

Related

How could I know which "TextView" is clicked

I was creating a dynamic Text View and I need to know which one of those Text views was clicked by the user i read that i need to use getTag() method but it keeps return null when i try it this is my activity code in java:
for(int i=0;i<size;i++){
TextView temp = new TextView(this);
temp.setId(i);
temp.setId(i);
String s = "";
temp.setText(s);
temp.setTextColor(Color.RED);
mylieniarlayout.addView(temp);
tv[i] = temp;
}
final TextView answertv = findViewById(R.id.answertv);
mylieniarlayout.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"ID: "+ v.getTag(), Toast.LENGTH_SHORT).show();
answertv.setText("Clicked ID: " + v.getTag());
}
});
Firstly, setTag() and getTag() methods on view are not used for identifying the view. We use getId() for identifying the view. Please read document here,
getTag on android developers
Secondly, your onClickListner is on layout which won't give you the selected id of textView. In your code you are setting an ID for the text view and trying to fetch a tag which is a mistake.
onClickListener should be on the textview of which you want a tag.
While creating a textView set the tag and then you will get the tag for that textview.
You can try if else condition
Like:
if(v.getTag() == 0){
//Show position 0 here
} else {
//Other position
}
Hope you got your answer

TextView set multiple string into a setText(int resid)

Good morning,
How can I put multiple string ressources inside the setText to display them in order ?
I have a layout with a TextView (id: TxtDisp) and a Button (id: NextSentence) that change the text when I click on it.
NextSentence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TxtDisp.setText(R.string.sentence_2);
}
});
Where or how can I put four to six string ressources to be display in order when the button is clicked ?
Thanks in advance !
You could put the string resources in an array, and get the string from that. So add a class member to track the which sentence is next
private int nextSentenceId = 0;
then in onCreate use code like this
final int[] sentences = new int[]{R.string.sentence_1, R.string.sentence_2, R.string.sentence_3};
NextSentence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( nextSentenceId < sentences.length ) {
TxtDisp.setText(sentences[nextSentenceId]);
++nextSentenceId;
}
}
});
Make sure to catch when you are at the last sentence or you will get an array out of bounds error.
You can do it easily when you hold these strings in an array or something and have a counter that hold which string is displayed right now like so
in onCreate() method put your sentences in an ArrayList
ArrayList<Integer> strings = new ArrayList();
strings.add(R.string.sentence1);
strings.add(R.string.sentence2);
strings.add(R.string.sentence3);
then on the button click you can use the counter and track which is selected
NextSentence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TxtDisp.setText(strings.get(count++));
}
});
I hope this would help you

How to write onclick actions for dynamically generated textviews in android?

I have to display the textviews dynamically in android and need to write onclick action for each textview. I am able to display the textviews dynamically but I didnt get
how to write the onclick action for each textview. Please help me regarding this...Will be thankful to you..
You should check this code. Create an onclicklistener and then use setOnClickListener(); method.
private TextView textview1, textview2;
//initialize them
OnClickListener customTextviewOnClicklistener = new OnClickListener()
{
#Override
public void onClick(View v)
{
if (v == textview1)
{
// Here your code for textview1
Log.i("Clicked Item", "textview1");
}
else if (v == textview2)
{
// Here your code for textview2
Log.i("Clicked Item", "textview2");
}
else
{
//Here your code for others
}
}
};
textview1.setOnClickListener(customTextviewOnClicklistener);
textview2.setOnClickListener(customTextviewOnClicklistener);
i hope this may help you.
Create the views dinamically however you want to do and add the listener right then.
TextView tv = new TextView(ActivityName.this);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ActivityName.this, "tv text: " + ((TextView) v).getText().toString()).show();
//Do whatever you want to do here.
}
});
layout.addView(tv); //layout added on the xml for example, or by an inflater.

Example for clickable TableRow in Android

I have a table of texts with each row having a text. I want to display a popup when one of the rows in the table is clicked. Does TableLayout in android support this? If so, is there an example?
You can attach the onClickListener with the TextView in each row. Or you could set it directly on TableRow directly
android.widget.TableRow tableRow = ..
tableRow.setOnClickListener(new OnClickListener(){
public void onClick() {
}
});
OnClickListener
Programatically doing it i used this code inside onCreate method of my activity:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//codigoo
}
});
I had to use the View.OnClickListener and the #Override or it wouldnt work!

Dynamically creating Buttons and setting onClickListener

I have problem with handling dynamically created Buttons on Android. I'm creating N buttons and I have to do the same method when button is clicked but I have to know which button is clicked.
for (int i = 0; i < NO_BUTTONS; i++){
Button btn = new Button(this);
btn.setId(2000+i);
...
btn.setOnClickListener((OnClickListener) this);
buttonList.addView(btn);
list.add(btn);
Cucurrently I'm adding ID to every button and I'm using the method below to see which button was clicked. (line btn.setId(2000+i); and btn.setOnClickListener((OnClickListener) this);). This method is also implemented in the activity.
#Override
public void onClick(View v) {
switch (v.getId()){
case 2000: selectButton(0);
break;
...
case 2007: selectButton(7);
break;
}
}
This doesn't look good to me so i'm asking is there some better way to do this? or how to send some information to onclick event? any suggestions?
You could create a method that returns an onclickListener and takes a button as a parameter. And then use that method to set the onClicklistener in the first loop you have..
Update: code could be soemthing along these lines:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
button.setText("text now set.. ");
}
};
}
as a method in the activity and then use it in the loop like this
button.setOnClickListener(getOnClickDoSomething(button));
I got one solution for this..
use this code in onCreate
linear = (LinearLayout) findViewById(R.id.linear);
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
Button[] btn = new Button[num_array_name.length];
for (int i = 0; i < num_array_name.length; i++) {
btn[i] = new Button(getApplicationContext());
btn[i].setText(num_array_name[i].toString());
btn[i].setTextColor(Color.parseColor("#000000"));
btn[i].setTextSize(20);
btn[i].setHeight(100);
btn[i].setLayoutParams(param);
btn[i].setPadding(15, 5, 15, 5);
linear.addView(btn[i]);
btn[i].setOnClickListener(handleOnClick(btn[i]));
}
after onCreate create one method of return type View.OnClickListener like this..
View.OnClickListener handleOnClick(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
}
};
}
Button.OnClickListener btnclick = new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button = (Button)v;
Toast.makeText(getApplicationContext(), button.getText().toString(),2).show();
}
};
call this listener by btn.setOnClickListener(btnclick);
View IDs should not be used for these purposes as View Ids are generated on compilation time depending on IDs defined in xml layout files.
Just place your own IDs in the setTag() method which is available at the View level (so Buttons inherit them). This "tag" can be anything that allow you to recognize a View from others. You retrieve its value with getTag().
instead use setTag() function to distinct easily.
for(int i=0;i<4;i++) {
Button btn = new Button(this);
btn.setTag(i);
btn.setOnClickListener(new View.OnclickListener() {
#Override
public void onClick(View v) {
int i=v.getTag();
switch(i) {
case 1: btn.setText(i);
break;
case 2: btn.setText(i);
break;
case 3: btn.setText(i);
break;
case 4: btn.setText(i);
break;
default: btn.setText("Others");
}
}
}
"This doesn't look good to me" why not? doesn't it work? You could also create a static member variable holding a list of all added buttons, and then look for the clicked button in that list instead.
I don't know why you would want to create N buttons, it looks like your value of N is greater than 10 at least, if you are not trying to show them all at once (I mean fit all of them into one single screen, no scrolling) you could try to recycle the invisible buttons just like we do for list view using a list view holder. This would reduce your memory footprint and boost performance, and differentiate the buttons based either on the text you set on them or a tag or you can even hold a reference to those small number of buttons.
Is preferable not to mess up with the ids, setTag and getTag methods were designed for that purpose, it's the fast and clean way to set a bunch of button listeners on a dynamic layout
This answer may you help:
https://stackoverflow.com/a/5291891/2804001
public class MainActivity extends Activity implements View.OnClickListener
{
LinearLayout linearLayout;
Button [] button;
View.OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.parent_lay);
String[] array={"U123","U124","U125"};
int length=array.length;
System.out.println("11111111111111111111111111");
button=new Button[length];
for(int i=0;i<length;i++)
{
button[i]=new Button(getApplicationContext());
button[i].setId(i);
button[i].setText("User" + i);
button[i].setOnClickListener(this);
linearLayout.addView(button[i]);
}
}
#Override
public void onClick(View view)
{
view.getId();
Button button=(Button)findViewById(view.getId());
button.setText("Changed");
}
}

Categories

Resources