iDroid Explorer iDroid Explorer - 5 months ago 16
Android Question

How to add multiple TextView dynamicall to the define LinearLayout in main.xml

I know its possible to add some layouts like

Button
and
TextView
to a
LinearLayout
. I want to create more then one
TextView
based on the condition of a for loop.

I have tried it with my own way, but I am not able to create it. Does anybody know how to create it?

The for loop will be different based on the condition. Please help me regarding this. Is it possible to create the array of
TextView
?

I already have set the layout with

setContentView(R.layout.result_page);


I am using Function to add that view to my existing view:

The function is :

public void addAll()
{

LinearLayout layout = (LinearLayout)findViewById(R.id.myLayout);
layout.setOrientation(1);
TextView name[] = null;
TextView website[] = null;
TextView category[] = null;
for (int i = 0; i < 5; i++)
{
name[i] = new TextView(this);
name[i].setText("Name = Shreyash");
website[i] = new TextView(this);
website[i].setText("Website = shreyah.co.cc");
category[i] = new TextView(this);
category[i].setText("Website Category = OWN");
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
}


But after that if i run the Application, it shows me error like:

09-08 11:03:28.755: ERROR/AndroidRuntime(318): FATAL EXCEPTION: main
09-08 11:03:28.755: ERROR/AndroidRuntime(318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quiz.spellingquiz/com.quiz.spellingquiz.ResultDisplayPage}: java.lang.NullPointerException
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.os.Looper.loop(Looper.java:123)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at java.lang.reflect.Method.invoke(Method.java:521)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at dalvik.system.NativeStart.main(Native Method)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): Caused by: java.lang.NullPointerException
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at com.quiz.spellingquiz.ResultDisplayPage.addAll(ResultDisplayPage.java:59)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at com.quiz.spellingquiz.ResultDisplayPage.onCreate(ResultDisplayPage.java:34)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): ... 11 more


I don't know where I am wrong.

please help me regarding this.

I got the result with DeeV's code like this:
enter image description here

But I want to set the Word under the tag word, and Answer under the tag answer. But how it is possible ?
I think i have to set another LinearLayout for that. Can anybudy tell me how I set another LinearLayout for that type of appearance ?

Answer

You only need to create an array if you need to change the TextViews later. However, if you do need to make an array, then something like this should work.

List<TextView> textList = new ArrayList<TextView>(NUM_OF_TEXTS);
for(int i = 0; i < NUM_OF_TEXTS; i++)
{
   TextView newTV = new TextView(context);
   newTV.setText("New message.");
   newTV.setTextColor(0xFFFF0000);
   /**** Any other text view setup code ****/
   myLinearLayout.addView(newTV);
   textList.add(newTV);
}

If the text is static once created, then you can simply remove any reference to the list in code, and it will still be added to the LinearLayout.

EDIT:

Assuming I understand your question right, you want the layout to be something like this:

Word:
Big
Answer:
42

Word:
Small
Answer:
Tough

Word:
Example
Answer:
Another Answer

In that case, you literally don't have to do much. LinearLayout will put everything in the order that you place it with addView. To update my previous code, this should work:

TextView blankText = new TextView(context);
TextView wordText = new TextView(context);
textView answerText = new TextView(context);
blankText.setText(" ");
wordText.setText("Word:");
answerText.setText("Answer:");

List<TextView> wordList = new ArrayList<TextView>(NUM_OF_WORDS);
List<TextView> answerList = new ArrayList<TextView>(NUM_OF_ANSWERS);

for(int i = 0; i < NUM_OF_WORDS; i++){
   TextView newWord = new TextView(context);
   newWord.setText(**** some method of getting the word ****);
   TextView newAnswer = new TextView(context);
   newAnswer.setText(**** some method of getting the answer ****);
   /**** Any other text view setup code ****/

   myLinearLayout.addView(wordText);
   myLinearLayout.addView(newWord);
   myLinearLayout.addView(answerText);
   myLinearLayout.addView(newAnswer);
   myLinearLayout.addView(blankText);

   wordList.add(newWord);
   answerList.add(newAnswer);
}
Comments