I want to create two TextView dynamically and show them in a linearLayout called activity_ranking. I try this and the LogCat says that the problem is in the line: "ranking.addView(fecha);" Do you know what it happens? Thanks!
public void listarPuntuaciones(){
LinearLayout ranking = (LinearLayout)findViewById(R.layout.activity_ranking);
for(int i=0; i<puntuaciones.size(); i++){
String aux[]=this.puntuaciones.elementAt(i).split(";");
TextView fecha = new TextView(this);
Log.i(aux[0],"aux0");
fecha.setText(aux[0]);
ranking.addView(fecha);
TextView puntos = new TextView(this);
Log.i(aux[1],"aux1");
puntos.setText(aux[1]);
ranking.addView(puntos);
}
}
I guess ranking is null, because when you tried to find it byId, you should have given R.id.layout_id as a parameter.
Look here
Related
Wantto ask user to enter number of TextEdit or ViewText and then draw them.
For-example user enter 7,then seven ViewText draw.I knew this is a suitable way but in my prgrm i cant change the whole structure,the i got this erro at:
tv = new TextView(this);
and error is: "The constructor TextView(new View.OnClickListener(){}) is undefined".
i knew i have to do:
implements OnClickListener
but i cant change the prgrm now.SO is there anyway to create TextView(or any View objct) without refrence it to 'layout' xml?
LinearLayout ll = (LinearLayout)findViewById(R.id.myLL);
for(int i=1 ; i<= 10 ; i++){
TextView tv = new TextView(this);
tv.setText("String/String/String");
ll.addView(tv);
}
THANKS,,
You're adding the LinearLayout to itself. The last line should be ll.addView(tv);
LinearLayout ll = (LinearLayout)findViewById(R.id.myLL);
for(int i=1 ; i<= 10 ; i++)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("String/String/String");
ll.addView(tv);
}
replace 'new TextView(new)' with 'new TextView(getApplicationContext())' .
My problem is creating activity to enter an array manually in Android. So I tried to create multiple EditText by these code lines:
public void EnterArray(int n)
{
for(int i=0; i<n; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
EditText txt = new EditText(this);
txt.setId(i);
layout.addView(txt, params);
}
}
The remain problem is how can I get the text from them by using the Save Button I create in the layout. Is there any resolve? I hope there's someone could help with it.
Thank you in advance ^^
You can try something like this.
int count =layout.getChildCount();
for(int i=0;i<count;i++)
{
EditText text=(EditText)layout.getChildAt(i);
String value= text.getText().toString()
}
I want to add a LinearLayout wrapped around a TextView and Button programmatically. I want it to take a String array and then using the length of the string array, add that many TextViews each with their own button.
So first:
String [] s = { .... the values ....}
int sL = s.length;
TextView t1 = new TextView (this);
// then somehow create t2, t3... etc. matching the length of the String array.
Is this the best way to do this or is there another way to do this? For some context, it's a quiz app and I've created a list of categories inside resources as values and I'm trying to programmatically get my app to create as many TextViews as there are categories then set each TextView to each category then get each button to take the user to that category of questions.
You are starting it right, just do a for loop and add textviews to your linearlayout.
// You linearlayout in which you want your textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
String [] s = { .... the values ....}
int sL = s.length;
TextView textView = null;
// For later use if you'd like
ArrayList<TextView> tViews = new ArrayList<TextView>();
for (int i = 0; i < sL; i++)
{
textView = new TextView(this);
textView.setText(s[i]);
linearLayout.addView(textView);
tViews.add(textView);
}
There is nothing wrong with this way of doing it. If you want to use these textview later on (set text for them or something) store them in an Array of some kind. Edited code
You can do the following:
for(int i=0;i<s.length;i++){
TextView t=new TextView(this);
t.setText(s[i]);
yourLinearLayout.addView(t);
}
But I really think that using a ListView would be better for performance ;)
Well i made an activity where i am creating some TextViews based on the size of a string array! But despite the fact that my string array has 4 items on it, which i tested it with debugging, the textviews that are created is only 1. If anyone has an idea about it please tell me :)
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
final TextView[] tv = new TextView[daily_lessons.length];
final LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
for (int i=0; i<daily_lessons.length; i++){
tv[i] = new TextView(this);
tv[i].setText(daily_lessons[i]);
tv[i].setTextSize(20);
tv[i].setLayoutParams(new LinearLayout.LayoutParams((int)LayoutParams.FILL_PARENT,(int) LayoutParams.WRAP_CONTENT));
tv[i].setGravity(Gravity.CENTER);
layout.addView(tv[i]);
}
If you still need an answer to this question here is what I would do.
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
TextView tmpView = null;
for (int i=0; i<daily_lessons.length; i++){
tmpView = new TextView(this);
tmpView.setText(daily_lessons[i]);
tmpView.setTextSize(20);
layout.addView(tmpView , new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
I use this type of code alot for my dynamically genenerated content (obtain content from prepopulated database).
The TextView seems to be created but might now be visible on the GUI may be stacked over each other etc.
1.Use the layout.setOrientation(ORIENTAIION.VERTICAL) on the parent linear layout's.
2.Use the childCount() on layout to make it sure on the fact that the all 4 text views have been added to the snippet.
3.Also make sure your are not using removeALLView() etc methods for your case study to the problem case.
How can I to add an array of TextView data into an existing text view? I tried the following code:
TextView tvArrQuestion[]=(TextView) findViewById(R.id.Textparse);
...but I get this error:
Type mismatch: cannot convert from TextView to TextView[]
How can I resolve this?
You can do solve like ths:
first take a string append all the text arraydata to it.
String txt="";
for(int i=0;i<tvArrQuestion.length();i++)
{
txt=txt+tvArrQuestion[i].getText().toString();
}
than you can set it to textview
lets take textview tv so code is:
tv.setText(txt);
You cant give like that in ur code. Place ur code as:
TextView tvArrQuestion=(TextView) findViewById(R.id.Textparse);
and give
tvArrQuestion.setText(your array data);
Another Concept is below As this type u also add Text view
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
TextView name[];
TextView website[];
TextView category[];
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
Try this code using existing Layout
Textview name=(TextView)findViewById(R.id.textView1);
String txt="";
for(int i=0;i
}
name.setText(txt);