Why TextView variable needs to be inside a loop - android

LinearLayout x=(LinearLayout)findViewById(R.id.english_no);
Why this code is wrong-
TextView wordview=new TextView(this);
for(int i=0;i<english.size();i++)
{
wordview.setText(english.get(i));
x.addView(wordview);
}
and this one is correct-
for(int i=0;i<english.size();i++)
{
TextView wordview=new TextView(this);
wordview.setText(english.get(i));
x.addView(wordview);
}
I couldn't understand the difference.

here in the first example you are just referring to the first TextView that you created and changing its value and adding it to the view, eventually the x (hoping a Linearlayout) will have english.size() number of views where the content of every view would be same and that is the last content of english

Related

ANDROID: Delete textviews which are created dynamically

I have added multiple TextViews dynamically in a layout,
for(int x=4;x<result.length();x++)
{
JSONObject collegeData = result.getJSONObject(x);
Log.i("Classlist",""+x);
TextView tv = new TextView(this);
Animation animation = AnimationUtils.loadAnimation(student_profile.this, android.R.anim.slide_in_left);
tv.startAnimation(animation);
tv.setTag(tag);
tv.setLayoutParams(lparams);
tv.setText(collegeData.getString("date") + " " + collegeData.getString("day_name"));
tv.setTextSize(17);
this.linearLayout_top5classes.addView(tv);
}
This loop adds textViews according to the data received by the url,Now i want to remove the textviews which were created in this loop and i cant find a proper method to do so....I only want to remove these textviews and not all the textviews
UPDATE
First i used
int prv=0;
then
String tag ="textView_"+x;
prv++;
in the first loop to generate multiple tags
then i removed them with
for(int x=4;x<prv;x++)
{
View view = this.linearLayout_top5classes.findViewWithTag("textView_"+x);
this.linearLayout_top5classes.removeView(view);
Log.i("prv value",prv+"");
}
Of course there is a way. Just look for child views with tag:
View view = this.linearLayout_top5classes.findViewWithTag(tag);
this.linearLayout_top5classes.removeView(view);
If you add ID's to child views, then:
View view = this.linearLayout_top5classes.findViewById(id);
this.linearLayout_top5classes.removeView(view);
If possible try using different layout for the dynamically added textviews. and then remove views from second layout only. By using,:
linearLayout_top5classes.removeAllViews();
To remove particular view with tag
View view = this.linearLayout_top5classes.findViewWithTag(tag);
this.linearLayout_top5classes.removeView(view)
To remove view from particular position
this.linearLayout_top5classes.removeViewAt(position);
Use following logic to remove any view from layout.
this.linearLayout_top5classes..post(new Runnable() {
public void run() {
this.linearLayout_top5classes..removeView(view);
}
});

Android layout for Loop Text

I would like to be able to for loop in my layout and add text to the textviews dynamically, this does not error out but I get the lost row in the display for example
Tw04 One4
I would like to be able to display
One1 Two1
One2 Two2
etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rel_layout);
for (int i = 0; i < 5; i++) {
TextView woTxt = (TextView) findViewById(R.id.ticker_price);
woTxt.setText("One"+i);
TextView cusTxt = (TextView) findViewById(R.id.ticker_symbol);
cusTxt.setText("Two"+i);
}
}
You may add TextViews programmatically to your layout as below :
TextView [] txt1 =new TextView[5];
for(int i=0;i<5;i++)
{
txt1[i]=new TextView(YourActivity.this);
txt1[i].setText("One"+i);
txt1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
linear.addView(txt1[i]);
}
where linear is a LinearLayout from your layout.
You have only two text views, where in fact you need 10 on your example, one text view for each item you want to display. I suggest that you make a ListView instead, where each line of the list will be a couple of text views.

set TextView value for array

I have make an application where I am swiping number of photos,I have added these photos in an array and I want to get the simple numerical values(for example:-1,2,3 etc) for these items and for this I have used the for loop,and set these values to the textview and then dynamically adding these textview to the linearlayout and and then this linearlayout to the viewgroup container.
By doing this I am just able to show the last number value for an array item;below is my code.
LinearLayout linearlay=new LinearLayout(context);
linearlay.setOrientation(LinearLayout.VERTICAL);
imageView.setImageResource(image_id[position]);
linearlay.addView(imageView,0);
for(int i=0;i<image_id.length;i++)
{
linearlay.removeView(textView);
textView.setText("Image: "+(i+1));
linearlay.addView(textView);
((ViewPager) container).addView(linearlay);
}
return linearlay;
} TextView textView=new TextView(context);
I want to setText value as 1,2,3... and show it in layout but only the last value is being shown.
Do below three changes -
1) Remove linearlay.removeView(textView); from code.
It is deleting your last added textview
2) Also add append inplace of setText
3) Also put linearlay.addView(textView);
((ViewPager) container).addView(linearlay);
outside loop.
Try this
linearlay.addView(imageView,0);
textView=new TextView(context); // declare textview as a class member
linearlay.addView(textView);
((ViewPager) container).addView(linearlay);
for(int i=0;i<image_id.length;i++)
{
textView.append("Image: "+(i+1));
}
return linearlay;
}
Put the following line outside for loop:
linearlay.removeView(textView);
Bcs, it is removing the textview everytime.

get array of TextViews

I'm making an app that stores results in a multiple textviews,
First, I need to get the views, they are 20 views named result 1, .... result 20.
how can i get them to an array of textview.
I found this method but it's too long
TextView [] results = {(TextView)findViewById (R.id.result1),
(TextView)findViewById (R.id.result2),(TextView)findViewById (R.id.result3),
(TextView)findViewById (R.id.result4),(TextView)findViewById (R.id.result5),
(TextView)findViewById (R.id.result6).....};
thank you for help
How you started is correct, now think about putting that repetitive code in a cycle.
For example design a method that will take as the input an array of your TextView resources, and using a "for" cycle find that view by corresponding id.
private TextView[] initTextViews(int[] ids){
TextView[] collection = new TextView[ids.length];
for(int i=0; i<ids.length; i++){
TextView currentTextView = (TextView)findViewById(ids[i]);
collection[i]=currentTextView;
}
return collection;
}
And then you use it like this:
// Your TextViews ids
int[] ids={R.id.result1, R.id.result2, R.id.result3};
// The resulting array
TextView[] textViews=initTextViews(ids);
if you have a handle to parent the layout that contains the text views, you can recursively discover them with a function like this,
void getTextViews(View view, List<TextView> textViews) {
if (view instanceof TextView) {
textviews.add((TextView)view);
else if (TextView instanceof ViewGroup) {
getTextViews((ViewGroup)view, textViews);
}
}
now call it like this,
ViewGroup topLayout = findViewById(...);
List<TextView> views = new ArrayList<TextView>();
getTextViews(topLayout, views);
TextView[] textViewArray = textViews.toArray(new TextView[0]);
this is quite a bit longer, but it has the advantage of not needing to change the code if you add, remove, or rename a textview.
IMHO, don't focus on writing less code, focus on writing clear code. the speed at which you type is rarely the limiting factor in your productivity.

text not changing in a TextView while doing Layout Inflater

in my app when the activity get loaded, i am doing the following
setContentView(R.layout.main2);
layoutToAdd = (LinearLayout) findViewById(R.id.linearLayout1);
for(i=0; i<num;i++)
{
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.camera, null);
layoutToAdd.addView(view);
}
The value of num differs for each time.
In my LayoutInflater layout i have a text view, edit text and a button.
Now they are shown according to the number of times mentioned in num, now for each time i want the text and button name to be changed. How to set the text for TextView and Button.
Just set them after you inflate the layout
View view = inflater.inflate(R.layout.camera, null);
TextView tv = view.findViewById(R.id.textviewid); //id defined in camera.xml
Button b = view.findViewById(R.id.buttonid); //id defined in camera.xml
tv.setText();
b.setText();
layoutToAdd.addView(view);
What I suggest it,you should create an xml file containing just a LinearLayout.
Then programaticall,you should create TextViews,EditTexts and Buttons and add it to LinearLayout.
You can set different properties of those components like below:
Here is example of setting TextView's properties.For rest of the components,you can set the same.
TextView tv=new TextView(context);
tv.setBackgroundResource(R.drawable.textview_bg);
tv.setPadding(20, 5, 40, 5);
tv.setText("set your text");
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.RED);
tv.setClickable(true);
tv.setId(id);//where id is an integer which should be unique for each TextView
layout.addView(tv);
Also you need to create and add all these three component inside the for loop,providing unique ids depending on i you use to iterate the loop!And you can have a String array for textviews and buttons to set their names in for loop,which would be easier for you for passing the string you like to set to them in loop.
You must store the reference of the view inflated and store in a List then when you want to modify simply list.get(2).findViewById(R.id.textbox_id).
Hope it help.

Categories

Resources