Reputation: 11321
This is the textView2 code in my activity_main.xml
<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:
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
{
text.post(new Runnable()
{
@Override
public void run()
{
text.setText("Checking Connection With Ip: " + ipaddresses[i]);
}
});
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 part i added now is:
text.post(new Runnable()
{
@Override
public void run()
{
text.setText("Checking Connection With Ip: " + ipaddresses[i]);
}
});
Two 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
Upvotes: -1
Views: 2037
Reputation: 101
To move the TextView, you have to do this:
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);
and to access the i variable, you have to declare it final.
Upvotes: 1