Is it possible to find the added TextView to update it with other options.
I am adding by this code new TextView with a Button. Now, I want also set a onClickListerner for the dynamically added TextViews. For this I have to find them with findViewById method. But they aren't createt yet.
Can I make this in a other way and if yes, How?
Button hinzufügenButton = (Button) findViewById(R.id.hinzufügen_Button);
hinzufügenButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View view){
EditText tischName = (EditText) findViewById(R.id.tisch_name_EditText);
TextView tisch = new TextView(getApplicationContext());
tisch.setText(tischName.getText());
tisch.setAllCaps(true);
tisch.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tisch.setBackgroundColor(Color.parseColor("#9FA8DA"));
tisch.setTextSize(25);
tisch.setId(id);
id++;
LinearLayout tischeAnzeigen = (LinearLayout) findViewById(R.id.tische_LinearLayout);
tischeAnzeigen.addView(tisch);
}
});
Just add the onClickListener as you create your dynamic views
Example:
...
tisch.setId(id);
tisch.setOnClickListener(yourListener);
...
You can use setId to add an id. But you probably don't need to. If you're creating a view dynamically, you have a reference to it when you create it. Just set it on that reference. No need to find it.
Related
So I generated some button. The numbers it depends on the user (when clicked a button, create a new one).
This is how I made it:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
I want to remove/delete them, but I don't know how.... I gave them a unique id, but I still dunno how can I remove them :/
I cannot comment another post, but using
command = new Button(this)
might involve an implicit memory Leak on this! (which can be the Activity). Rather use Context object. Or remove the button at least.
Then because you have the parent of your Button. Just remove it:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
Make command a global variable. Then you can access it later, and call command.setVisibility(View.GONE);
So at the top, of your class file, you would declare the global variable:
Button command;
Then remove the redefinition later on and instead assign to the global variable:
command = new Button(this);
Then when you want to hide it, call:
command.setVisibility(View.GONE);
Try using the documentation, 5 seconds of research can lead you to the RemoveView method.
layout.removeView(command);
Update
If you have a null pointer exception on this line, means your layout is null, not your command. Make your layout variable global for that class.
Also be sure to keep different variables for each of your created buttons. If you have a global variable, and create 10 buttons using the same variable you will only have a reference to the last one created. If you explain exactly when you want to remove the button we might be able to help you further.
As an example, if you want to remove the button when the user clicks on it you can change your clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
One other implementation that may help others who view this thread is that you can consider removing all the child elements in the parent layout. Once you get the view's parent (which I assume is a layout container),you can remove all the child elements.
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
You can also do it like this for security purpose:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
You can also use this snipper
For Adding the Button
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
For removing the button
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);
I created a dynamic form without much problems, but I need to recover the values from the fields (controls) of the form, but I'm not sure of how to do this.
For example, I have this piece of code:
if(tipoP.equals("TEXTAREA")){
EditText ta = new EditText(this);
ta.setId(i);
LayoutParams params3 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, (float) 0.3);
params3.setMargins(20, 0, 20, 0);
ta.setLayoutParams(params3);
ta.setLines(3);
ta.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
ll.addView(ta);
}
How do I add a listener that captures the text of the EditText and put it inside a Vector variable?
I tried this:
ta.setOnClickListener(new OnClickListener(){
public void onClick(View view){
EditText t = (EditText) findViewById(i);
res.add(t.getText().toString);
}
});
But I'm not getting the id (variable i) because its in another execution environment. How do I solve this? Any help would be appreciated!!
You should not use setId for dynamically created views but setTag and findViewByTag.
You could create a button dynamically and set an onClickListener on it. Inside the listener, you can just reference the EditText directly (no need for tags or ids) as long as you have made it final.
I created new TextViews in my GUI dynamically and I collected them in an array of TextViews, but I can't set up OnClickListeners because of that (they're inside of an array)...
So what I want to do now is evaluate if the TextView is clicked and handle that event, but I'm not sure how to do so...
If I'm not being to clear, please tell so I can write down the problem with all the details...
Thanks!
Even when you are creating rows dynamically you still have a reference to the TextView. Simply add the OnClickListener to this reference, like this:
TextView tv1 = new TextView(this);
tv1.setText(string);
tv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tv1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Do something
}
});
row.addView(tv1);
So your TextViews are in an array like TextView[] textViews?
Just call textViews[0].setOnClickListener().
If you used an ArrayList, then it would be textViews.get(0).setOnClickListener()
I am 2 days old to Android Programming. I may be comitting a mistake at the core level. If this is the case, pardon.
I am trying to add a text box to a Relative Layout. When a button is clicked.
The method is bound to the button by the attribute android:onClick="method"
By doing the below.
public method (View view){
RelativeLayout vRL = (RelativeLayout)findViewById(R.layout.rLayout);
TextView vET = new TextView(this);
vET.setText("Text added to view.");
vET.setId(1);
vRL.addView(vET);
setContentView(R.layout.rLayout);
}
But I am getting a null pointer exception at vRL.addView(vET);
what is it that I am doing wrong? -OR-
Am I not adding the TextView element properly?
in below line
RelativeLayout vRL = (RelativeLayout)findViewById(R.layout.rLayout);
change the R.layout.rLayout to R.id.rLayout
setContentView(R.layout.rLayout);
put this line in onCreate() of Activity..
and this line from method()
RelativeLayout vRL = (RelativeLayout)findViewById(R.id.rLayout);
Replace below line with
`RelativeLayout vRL = (RelativeLayout)findViewById(R.id."id name of relative layout");`
Change like this:
public method (View view){
RelativeLayout vRL = (RelativeLayout)findViewById(R.id.rLayout);
TextView vET = new TextView(this);
vET.setText("Text added to view.");
vET.setId(1);
vRL.addView(vET);
setContentView(R.layout.rLayout);
}
change the R.layout.rLayout to R.id.rLayoutR.layout.rLayout);
I have a LinearLayout comprising of a few Buttons and I add this to my activity in the onCreate(..) method with setContentView(R.layout.myscreen). No surprises so far.
How do I get a reference to an iterator to these buttons? I'd like to add listeners to them but I'd rather not directly reference the Button's using their android:id.
Similar questions have been asked here and here but they don't quite answer my question.
Try something like this provide an id root_layout in xml to LinearLayout
LinearLayout mLayout = (LinearLayout) findViewById(R.id.root_layout);
for(int i = 0; i < mLayout.getChildCount(); i++)
{
Button mButton = (Button) mLayout.getChildAt(i);
mButton.setOnClickListener(this);
}
Where mLayout is object of you Linear Layout and Your activity must implements OnClickListener and here goes general listener
#Override
public void onClick(View v)
{
Button mButton = (Button)v;
String buttonText = mButton.getText().toString();
}
NOTE: For this to work properly you Linear Layout must only contains button no other views
You should take a look at my answer here.
In short. I'd assign the buttons a listener by setting the onClick attribute in the XML layout on each Button.
Inside of your Activity you'll need a public method like the one below which basically is what you want to do in your listener.
public void myFancyMethod(View v) {
// do something interesting here
}
If you want to go for accessing other elements you may try following syntax:
<ElementClass> <referencevariable> = (<ElementClass>) findViewById(R.id.<id_of_the_element>);
For Example:
TextView textView= (TextView) findViewById(R.id.t1); //I used t1 to refer my textview in the Layout.
This might work.
Then you can use these views with their inbuilt methods to perform as many as work you want.