how to check if a TextView exist - android

I created on loop 5 TextViews, set to them some text and added they to LinearLayout.
After that I need to change text in them. I want to delete all of them and create new one on loop again. But before TextView deleting I must to be sure that its exists. How to do it?
for(int i=0; i<5; i++){
TextView tv = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
tv.setId(i);
tv.setText("some data");
ll.addView(tv);
}
Thank you for help and your time!

If you want to delete all of them then just perform ll.removeAllViews().

You should keep a reference to each of the text views you create if you want to remove them.
If I understand your question correctly, simply create an array of TextViews with 5 elements and run the loop to do what you want with them.
To "delete" them you can call:
tv[i].setVisibility(View.GONE);
To change text and re-add them:
tv[i].setText("New Text");
tv[i].setVisibility(View.VISIBLE);

Better solution is to hold references to your TextViews in a list, so you can interact with them later on after you've created them. Otherwise you won't be able to see the objects.

Related

How to create text boxes dynamically in eclipse android

I want to design a simple android application.That prompt user to enter number of numbers you want.Then it should create text boxes dynamically according to user requirements.can any one give me the code.Thanks in advance
The question you are asking will be closed soon because of off-topic but I will give you little tips about adding the TextView OR EditText, If you want to add TextView in number of times then do like this
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0; i<number; i++)
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("TextView "+i);
this.myLayout.addView(tv); //Where mLayout is your Parent Layout
}

Optimize Performance for creating views at runtime

I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.

Create a RelativeLayout dynamically and positioning views inside

I would like to create a method which returns a RelativeLayout created dynamically. To be clear, let's use this simplified example:
private RelativeLayout createLayout() {
RelativeLayout layout = new RelativeLayout(activity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
TextView tv1 = new TextView(activity);
tv1.setText("Text 1");
TextView tv2 = new TextView(activity);
tv2.setText("Text 2");
TextView tv3 = new TextView(activity);
tv3.setText("Text 3");
layout.addView(tv1);
layout.addView(tv2);
layout.addView(tv3);
return layout;
}
Now I want to position these TextViews relatively to each other. For that I have the idea to use a LayoutParams with the addRule method.
But this method requires an ID, e.g. addRule(RelativeLayout.BELOW, tv2Id). It means that I have to set an ID for each TextViews.
My problem is that the createLayout method will be called several times, so the question is:
Do I have to set different IDs for the TextViews each time the method is called in order to avoid conflicts ? If so, how can I do that ?
Most generally, Is there a better solution for doing it ?
EDIT
The idea behind this is to have a kind of ListView, where each item contains a Map (that can be shown or hidden).
Problem: the Map can't be scroll if it is inside a ListView (at least I did not manage to do that).
For that, I have decided to use a ScrollView and a LinearLayout to copy the behaviour of a ListView. This way the Map can be scrolled correctly and now, all I have to do is to create the items dynamically
ID's don't have to be unique. As you can see from this extract
setId (int id)
Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
But like you said, if you want to avoid conflict then you have to find a way to generate unique identifiers for each view.
Frankly, IMO I don't think it matters much the value of the ID. You can use 10, 20, 30. Just make sure you can have access to them anytime you need it, possible using a static final variable.
You asked if there is a better solution, yes there is. The most preferred way is to inflate an xml layout.

how to make android app containing huge text content

I want to make an android app that have a huge text content. imagine that i want to make a dictionary app. so what should i do. what method should i choose. should i make lots of textviews and activities or there is a solution?
I'm newbie in android programming...please help me! I tried by making a lot of textviews and activity manually but it's time-consuming...
Take a look at http://developer.android.com/guide/topics/ui/layout/listview.html you can import words for example from a databases and put them into the listview then dynamically create activities.
If you want TextViews you can always make them by code.
You declare a xml with a scrollView on top and inside a linearlayout.
On the class you do (or something close to that):
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
for(String text: textList){
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) layout).addView(valueTV);
}
Hope it helps

Receiving Focus Change Events for a dynamic TableRow

I have a TableLayout that is focusable.
I dynamically add to TableRows to this TableLayout.
Although I can get the TableRow, and its contents (a few TextViews), to respond to Touch events, I can't get any of them to respond to Focus events.
Aside from setting a OnFocusChangeListener, is there anything else I need to do?
TableRow tableRow = new TableRow(this); // "this" is a valid Context...
tableRow.setFocusableInTouchMode(true);
tableLayout.addView(tableRow);
TextView someNameTextView = new TextView(this);
someNameTextView.setText("Foo");
someNameTextView.setFocusableInTouchMode(true);
tableRow.addView(someNameTextView);
tableRow.setOnFocusChangeListener(someRowFocusChangeListener);
someNameTextView.setOnFocusChangeListener(someRowFocusChangeListener);
Are you actually calling setFocusable (see http://developer.android.com/reference/android/view/View.html#setFocusable%28boolean%29) on the Views and TableRows?
I was using the wrong component for the UI interaction I was looking for.

Categories

Resources