Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know it's 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

Where am I wrong 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. How I set another LinearLayout for that type of appearance?

like image 925
Shreyash Mahajan Avatar asked Sep 07 '11 13:09

Shreyash Mahajan


People also ask

Which code snippet allows you to programmatically add a TextView to a LinearLayout?

LinearLayout linearLayout = findViewById(R. id. rootLayout); // Create TextView programmatically.

How do you declare a LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do you align text in LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.


2 Answers

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:

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 blankText = new TextView(context);
   TextView wordText = new TextView(context);
   TextView answerText = new TextView(context);
   blankText.setText(" ");
   wordText.setText("Word:");
   answerText.setText("Answer:");

   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);
}
like image 101
DeeV Avatar answered Oct 13 '22 01:10

DeeV


        LinearLayout lila = new LinearLayout(this);
        ArrayList<Button> alb = new ArrayList<Button>();
        int nButton = 10;
        for (int i = 0; i < nButton; i++)
        {
            alb.add(new Button(this));
            lila.addView(alb.get(i));
        }
        //works the same way with TextView
        alb.get(5).setText("myButton");

Maybe this could help.

EDIT : Sorry strictly the same as DeeV.

like image 32
mthpvg Avatar answered Oct 13 '22 01:10

mthpvg