I have created a set of TextViews programmatically using a for loop. This what i have tried.
for(int i=1; i<5; i++){
valueTV = new TextView(AddMyVehicle.this);
linearLayout.addView(valueTV);
vehicleModelReturned = myVehicleData.getString("VehicleModel"+x, "");
valueTV.setText(vehicleModelReturned);
valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
valueTV.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
valueTV.setTextColor(Color.parseColor("#333333"));
i++;
}
this.valueTV.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
valueTV.setText("Hello");
}
});
I need to change the text of clicked TextView to "Hello". How can i achieve this?
You have to add a listener to all your TextView created.
And be careful, you incremented i twice : one in the first line of the for and another one in the end of the for.
for (int i = 1; i < 5; i++)
{
final TextView valueTV = new TextView(AddMyVehicle.this);
linearLayout.addView(valueTV);
vehicleModelReturned = myVehicleData.getString("VehicleModel" + x, "");
valueTV.setText(vehicleModelReturned);
valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
valueTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
valueTV.setTextColor(Color.parseColor("#333333"));
valueTV.setId("test");
valueTV.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
TextView tv = (TextView) v;
tv.setText("Hello");
}
});
}
First, set the OnClickListener to all the TextViews you create, not just the last one. That is, move the setOnClickListener() inside the for loop.
Second, in onClick(), change the text of the clicked view and not again the last one you created. The View v param is the view that was clicked. You can cast it to TextView.
Related
I am working with android LinearLayout and i am adding a TextView programmicataly and then on TextView click add an EditText in View. Now I want to get data at then end on button click from all added EditTexts in layout. here is my coding through which i am working.
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText editTextItem = new EditText(SearchRecipe.this);
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
i want to get all the data from EditText on searchRecipe click
Assign the id to the EditText:
editTextItem .setId({yourId});
Then inside searchRecipe onClick method, invoke:
EditText editTextItem = (EditText) parentLayout.findViewById({yourId});
And then use it as you want.
Add an arraylist and put the edittexts in it to keep track of them.
ArrayList<EditText> etList = new ArrayList();
In onClick of textview just add to it at the end.
etList.add(editTextItem);
In searchReciepe go through your list.
for(Edittext et : etList){
et.getText();
[...]
}
Edit: Obviously remember to remove it from the List on remove.
private int EDITTEXT_ID = 1;
private List<EditText> editTextList = new ArrayList<EditText>();
// Add Edittext programmatically in your view
private void addView() {
EditText editText = new EditText(this);
editText.setId(EDITTEXT_ID);
EDITTEXT_ID++;
editText.setText("Hello there " + EDITTEXT_ID);
editTextList.add(editText);
lnvEditText.addView(editText); // lnvEdittext is my LinearLayout which is added in XML file
}
// Get values of every Edittext on click of button
for(int i=0; i<editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
}
I have a list of Strings. I am adding a RelativeLayout dynamically for each of them and adding TextViews dynamically in each of the relativelayouts. Now when I click, I am sending this textview value to the next activity. Below is the code.
for(int i = 0; i < Names.size(); i++)
{
textView = new TextView(this);
textView.setText(Names.get(i));
rt=new RelativeLayout(this);
rt.setBackgroundResource(R.drawable.mednamedash);
int curTextViewId = prevTextViewId + 1;
int curRLId = prevRLId + 1;
textView.setId(curTextViewId);
rt.setId(curRLId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
final RelativeLayout.LayoutParams tvParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tvParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rt.addView(textView, tvParams);
prevTextViewId = curTextViewId;
prevRLId=curRLId;
noon.addView(rt, params);
rt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Dashboard.this,ReminderPopUp.class);
i.putExtra("medicinename", textView.getText().toString());
startActivity(i);
}
});
}
The problem is that if I have 10 RelativeLayouts with different texts and I am clicking on anyone of them, no matter which one I am clicking, only the last value of textview is passed to the next activity; not the value for which I clicked.
Please help
Thank you
You are getting always the last one because you are overriding the member class at every iteration of the loop.
you could set the OnClickListener to the TextView instead of the RelativeLayout and cast v, the clicked View, to TextView
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Dashboard.this,ReminderPopUp.class);
i.putExtra("medicinename", ((TextView)v).getText().toString());
startActivity(i);
}
});
try to use the clicked view which is your textview for that change your code to this.By following so you will get the context of currently clicked textview not the last one.
rt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView txt=(TextView)v;
Intent i=new Intent(Dashboard.this,ReminderPopUp.class);
i.putExtra("medicinename", txt.getText().toString());
startActivity(i);
}
});
In onClick, get the text view from the relative layout as
TextView textView = (TextView) rl.getChildAt(0);
String name= textView.getText().toString();
Say I have buttons that are created dynamically:
for(int j = 0; j < spirits.length;
j++){
Button imgBtn = new Button(v.getContext());
imgBtn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imgBtn.setMinimumWidth(100);
imgBtn.setMinimumHeight(100);
imgBtn.setId(j+1);
imgBtn.setTag(spirits[j]);
imgBtn.setText(spirits[j]);
imgBtn.setOnClickListener(new SpiritsClickListener());
cabinet_layout.addView(imgBtn);
}
I want to change the text of the button every time it's pressed (On - Off)
How can I reference the buttons within the OnClickListener class?
in your onClickListener, you have a function called onClick(View v){} where v is the View that was clicked. You may use v to get details about the button, including its ID. You can also take this view, and if you know it is a button, cast it to a button.
Button clicked = (Button)v;
You can then use it in your javacode just as you would normally use a button.
Why don't you just call new OnClickListener() inside that loop like this
for(int j = 0; j < spirits.length;j++){
Button imgBtn = new Button(v.getContext());
imgBtn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imgBtn.setMinimumWidth(100);
imgBtn.setMinimumHeight(100);
imgBtn.setId(j+1);
imgBtn.setTag(spirits[j]);
imgBtn.setText(spirits[j]);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do what you need to do here
}
});
cabinet_layout.addView(imgBtn);
}
Create an OnClickListener for dynamically created buttons as:
// Create Listener for Button
private OnClickListener SpiritsClickListener = new OnClickListener()
{
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
Button btn = (Button) view;
String btnText = btn.getText();
if(btnText.equalsIgnoreCase("On")){
btn.setText("Off");
}else{
btn.setText("On");
}
}
};
add this Listener to dynamically created buttons as:
imgBtn.setOnClickListener(SpiritsClickListener);
A table is being created such that:
TableLayout layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(4,5));
layout.setPadding(1, 1, 1, 1);
for(int i=0; i<7; i++) {
TableRow tr = new TableRow(this);
for(int j=0; j<6; j++) {
Button b = new Button(this);
b.setText("0");
b.setOnClickListener(buttonListener);
tr.addView(b);
}
layout.addView(tr);
}
super.setContentView(layout);
The OnClickListener buttonListener is:
View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
Button thisButton = (Button) findViewById(((Button)v).getId());
//thisButton.setText(Integer.toString(Integer.parseInt((String) thisButton.getText()) + 1));
thisButton.getText();
}
};
The call to thisButton.getText() throws a NullPointerException, and I'm not sure why. Can anyone help me out?
TableLayout layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(4,5));
layout.setPadding(1, 1, 1, 1);
for(int i=0; i<7; i++) {
TableRow tr = new TableRow(this);
for(int j=0; j<6; j++) {
Button b = new Button(this);
b.setText("0");
b.setOnClickListener(buttonListener);
tr.addView(b);
}
layout.addView(tr);
}
setContentView(layout);
}
View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
Button btn1 = (Button)v;
//thisButton.setText(Integer.toString(Integer.parseInt((String) thisButton.getText()) + 1));
String name = btn1.getText().toString();
}
};
I have tested this will work.
When you create view dynamically you don't to assgin an id to it.
you work using the object of the view.
I hope it makes sense to all the people who are curious to set id for dynamic buttons.
One problem in your code.
Have not set id of Button
b.setText("0");
b.setOnClickListener(buttonListener);
b.setId(i);
Assumption:
I can't see anything regarding setting id to button
The below line may causing exception:
Button thisButton = (Button) findViewById(((Button)v).getId());
Why are you doing? Instead you are already passing a view in click action: View v
So you can do straight way like:
String strText = ((Button) v).getText();
It mean your thisButton is probably null, because the statement before that was not successful.
Button thisButton = (Button) findViewById(((Button)v).getId());
does not look right. Usually the paramenter inside findViewById() should be a resource id.
such as
Button thisButton = (Button) findViewById(R.id.button);
R.id.button is defined by your layout xml.
You may simply get the text from the passed view, instead of recreating a button.
public void onClick(View v) {
if(v instanceof Button)
String text = ((Button) v).getText();
}
I created buttons dynamically, which is assigned to url address obtained from handlers as settext().
But I am not able to get text of that button,if it is clicked,since arg.gettext() is not working in OnClickListener.
Is there any way to get text of the button which is created dynamically
for ( i = 0; i <itemList.getTitle().size()-1; i++) {
title[i] = new TextView(this);
title[i].setTextColor( -16711936 );
title[i].setTextSize(18);
title[i].setText("Title = "+itemList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setTextColor(-16776961);
description[i].setText("Description = "+itemList.getDescription().get(i)+"......");
more[i]=new Button(this);
more[i].setText(itemList.getLink().get(i));
layout.addView(title[i]);
System.out.println("Title view is set");
layout.addView(description[i]);
//System.out.println("Description view is set");
layout.addView(more[i]);
more[i].setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener(){
public void onClick(View arg) {
// TODO Auto-generated method stub
String value=(should get the text of the selected button)
}
Any help would be greatly appreciated.
The View in onClick() is your Button.
Just downcast it:
Button btn = (Button) arg;
String btnText = btn.getText();