text not changing in a TextView while doing Layout Inflater - android

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.

Related

Why TextView variable needs to be inside a loop

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

findViewById for dynamically added View

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.

How can i access a Text view and print

I need to access a text view which is present inside Linear layout and print the text using gettext()?
Layout looks like:
Linearlayout
Linearlayout
Imageview
Relativelayout
Textview
The layouts around your TextView wouldn't matter unless you are playing with their Visibility.
The Activity in which you are accessing your TextView needs its call to:
setContentView(R.layout.yourLayout);
use textView= (TextView) findViewById(R.id.textViewId);
to map your textView and then can
textView.getText()
http://developer.android.com/guide/topics/resources/layout-resource.html
http://www.intertech.com/Blog/android-layout-and-id-attribute/
Create a linearLayoutObject for first layout and then create another object for TextView.
UiObject lLayout = new UiObject(new UiSelector().className(LinearLayout.class.getName()).index(1));
UiObject tView = lLayout.getChild(new UiSelector().className(TextView.class.getName()).index(1));
String tDetails = tView.getText();
Now you can use tDetails for comparison or other purpose.

Change the text of textview based on its ID

I am dynamically adding a textView into a layout and giving it ID's. Based on some conditions I want to change the text of the TextView based on its ID assigned.
Something like this: personOne.setText("abcd"); where personOne.id = buttonID;
personOne = new TextView(this);
int buttonID = 2000 + personCount;
personOne.setTextAppearance(this, R.style.button_text);
personOne.setTextColor(getResources().getColor(R.color.red));
personOne.setTextSize(15);
personOne.setText(personAdded);
personOne.setId(buttonID);
Help / Suggestions much appreciated.
Thank you
Use something like this:
TextView tv = (TextView) findViewById(yourId);
tv.setText("abcd");
Assuming the TextView is a child of the Activity's content view.
You can use the buttonID to find and manipulate TextView dynamically as follows:
TextView dynamicTextView = (TextView) findViewById(buttonID);
dynamicTextView.setText("The text you want to set");

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.

Categories

Resources