0

How to set array elements to dynamic created textview in android

MainActivity1 :

public class MainActivity1 extends Activity implements OnClickListener {
    EditText editText;
    Button button;
    public String Message;
    public String[] mobileArray;
    int len;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        editText = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);

        mobileArray = editText.getText().toString().split(",");
        len = mobileArray.length;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        LinearLayout rl = (LinearLayout) findViewById(R.id.linear1);

        TextView[] tv = new TextView[len];

        for (int i = 0; i <=len; i++) {
            tv[i] = new TextView(getBaseContext());
            tv[i].setText("" +mobileArray[i] );
            tv[i].setPadding(50, 50, 0, 0);
            tv[i].setTextColor(Color.parseColor("#FF000000"));
            rl.addView(tv[i]);

        }
    }

}

How to set array elements to dynamic created textview in android

Here everthing works fine, it creates dyanmic textview. But i need to display the array elements in textview.

When i try to set the array element app stops working. Please help me.

This my edit code

and my log cat is

      12-26 02:13:44.897: E/AndroidRuntime(2779): FATAL EXCEPTION: main
12-26 02:13:44.897: E/AndroidRuntime(2779): Process: com.example.new_word, PID: 2779
12-26 02:13:44.897: E/AndroidRuntime(2779): java.lang.NullPointerException
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.example.new_word.MainActivity1.onClick(MainActivity1.java:56)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.view.View.performClick(View.java:4438)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.view.View$PerformClick.run(View.java:18422)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Handler.handleCallback(Handler.java:733)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Looper.loop(Looper.java:136)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at java.lang.reflect.Method.invoke(Method.java:515)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at dalvik.system.NativeStart.main(Native Method)
12-26 02:13:47.467: I/Process(2779): Sending signal. PID: 2779 SIG: 9

Xml code is

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="Button" />

</LinearLayout>
</ScrollView>
5
  • pls declare textview with. TextView[] tv = new TextView[len]; Commented Dec 26, 2013 at 6:15
  • what is the value of len?? Commented Dec 26, 2013 at 6:45
  • length of mobileArray Commented Dec 26, 2013 at 6:46
  • yeah but what is the value of len?? Commented Dec 26, 2013 at 6:55
  • len should be 2 or 3 like that Commented Dec 26, 2013 at 7:00

6 Answers 6

1

try this...

TextView[] tv = new TextView[len];
for (int i = 0; i < len; i++) {
    tv[i] = new TextView(this);
    tv[i].setText(mobileArray[i]);
    tv[i].setPadding(50, 50, 0, 0);
    tv[i].setTextColor(Color.parseColor("#FF000000")); 
    // tv[i].setTextColor(Color.BLACK);
    rl.addView(tv[i]);
}

here you have used the color #000000 which means a transparent color. thus you are unable to see the Text

Sign up to request clarification or add additional context in comments.

5 Comments

are getting ArrayIndexOutOfBoundException still? or which exception?
when i cilck button it stops working Log cat error java.lang.NullPointerException android.os.Looper.loop(Looper.java:136)
so your mobileArray is null
i spit the string with comma and store tat in mobilearray
either your LinearLayout rl is null or text in EditText is empty
1

Try something like below:

 if(!(editText.getText().toString().equals(""))){

      TextView[] tv = new TextView[len];
      for (int i = 0; i <len; i++) {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText(mobileArray[i]);
        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
        }

  }

9 Comments

@user3133954 are you getting any Exception?
have you get all elements in mobileArray element.??
12-26 01:13:26.147: E/AndroidRuntime(1468): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
@Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout rl = (LinearLayout) findViewById(R.id.linear1); TextView[] tv = new TextView[50]; for (int i = 0; i <len; i++) { tv[i] = new TextView(getBaseContext()); tv[i].setText("" + mobileArray[i] ); tv[i].setPadding(50, 50, 0, 0); tv[i].setTextColor(Color.parseColor("#FF000000")); rl.addView(tv[i]); }
ohhkk. that the problem of len value.. check my answer now.. put len into textview array instend of 10.
|
0

The first error that i can point is in your for loop condition:[Change it too]

for (int i = 0; i <len; i++)

if the error still persists then please post your logcat;

Also change this code to display the text at that array:

tv[i] = new TextView(getBaseContext());
        tv[i].setText(mobileArray[i]);

2 Comments

your condition is i<=len which will definitely give a ArratIndexOutOfBound exception; try changing that condition. ALSO MAKE SURE YOU ARE NOT ADDING MORE THAN 10 TEXTVIEWS
Now what exception are you getting
0

Try this,

TextView[] tv = new TextView[len];
        for (int i = 0; i <len; i++) 
        {
            tv[i] = new TextView(MainActivity1.this);
            tv[i].setText(mobileArray[i]);
            tv[i].setPadding(50, 50, 0, 0);
            tv[i].setTextColor(Color.parseColor("#FF000000"));
            rl.addView(tv[i]);

        }

2 Comments

it seems like duplicate of my answer
We are posting answer at same time so i have seen your answer after posted my answer.
0

Try this sample code-

final int N = 10; // total number of textviews to add
final TextView[] tv = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);
    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);
    // add the textview to the linearlayout
    rl.addView(rowTextView);
    // save a reference to the textview for later
    tv[i] = rowTextView;
}

Comments

0

Use this code it will help you

    int []a=new int[4];
    a[0]=1;
    a[1]=2;
    a[2]=3;
    a[3]=4;
    for(int i=1;i<5;i++)
    {
        str4=str4+a.toString().valueOf(i)+"\n";
    }
RelativeLayout rl=(RelativeLayout) findViewById(R.id."**Your layout ID**");
        TextView textDynamic = new TextView(this);
        textDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        textDynamic.setText(str4);
        textDynamic.setBackgroundColor(Color.GREEN);
        textDynamic.setTextColor(Color.RED);
        textDynamic.setTextScaleX(2);
        textDynamic.setTextSize(20);
        textDynamic.isClickable();
        textDynamic.setPadding(10, 10, 10, 10);
        rl.addView(textDynamic);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.