This is the textView2 code in activity_main.xml:

Java Code:
<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checking Connection.."
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="47dp"/>
In the designer the textView2 is in the middle of the screen not in the center a bit upper from the center but in the middle.

Now i'm trying to change it's text in a real time inside a FOR loop:


Java Code:
public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);

                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    counter++;
                                    text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {

                                            text.setText("Checking Connection With Ip: " + ipaddresses[counter]);
                                            text.setLayoutParams(params);
                                        }
                                    });

                                    response = Get(ipaddresses[i]);
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {



                                    try
                                    {
                                        final String a = new String(response,"UTF-8");

                                       


                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }
                                
                                else
                                {
                                    
                                }





                        }
                        if (response == null)
                        {
                            text.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    text.setText("Connection Failed");
                                }
                            });
                        }
                    }
                });
                t.start();
            }
        });

    }

The last part i added in the FOR loop is:

Java Code:
text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            text.setText("Checking Connection With Ip: " + ipaddresses[i]);
                                        }
                                    });
Some problems:

When i change here the text of textView2 i need to move textView2 to the left so all the text will be inside the screen.

Second problem is that it dosen't identify the variabl: i


Now in the MainActivity.java inside onCreate i added:

Java Code:
params =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params is: private RelativeLayout.LayoutParams params;
Also the variable counter is global.


The problems i face now:


1. When clicking the button and the loop FOR start the textView2 jump to the top of the screen but i wanted it to move only to the left to be stay on same height level and to move to the left so all the text will be in this line. But for some reason the textView2 jump to the top of the screen.


2. I can't access to the variable i
When i'm doing for (int i = 0..... then if i use this I variable in here:


Java Code:
 
                                    text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {

                                            text.setText("Checking Connection With Ip: " + ipaddresses[counter]);
                                            text.setLayoutParams(params);
                                        }
                                    });
Instead the counter if i will put i in here: text.setText("Checking Connection With Ip: " + ipaddresses[i]);
I will get error that it can't find i

And if i declare variable i global as final the error will gone but then this i variable is not part of the FOR
I need somehow to use the for (int i
Variable and not my own global i variable.

So i tried to use the counter the problem is that the counter will not change will not count in real time only if i click the button all the time.
And i want that it will count in real time according to the FOR progress.


I can't figure out how to solve this two problems.