I'm having a bunch of textviews and buttons initially set as GONE in .xml and I'm setting up code to reveal them (make them VISIBLE) one by one in short intervals. If I use UI thread, I risk "Application not responding" (or "ANR") if the interval is longer then about 5s, but if I create new Thread then I must use
.getHandler().post(new Runnable() {
public void run() {
.setVisibility(View.VISIBLE);
}
which keeps going back to UI thread (in my understanding), so... what is the point then of having new Thread!?
Can someone explain and elaborate on this please :)
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 1"
android:id="@+id/tv1"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 2"
android:id="@+id/tv2"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 3"
android:id="@+id/tv3"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 4"
android:id="@+id/tv4"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 5"
android:id="@+id/tv5"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/LL1"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn1"
android:text="Btn1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn2"
android:text="Btn2"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_textview_background"
android:text="Linija 6"
android:id="@+id/tv6"
android:layout_gravity="end"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_textview_background"
android:text="Linija 7"
android:id="@+id/tv7"
android:layout_gravity="end"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 8"
android:id="@+id/tv8"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 9"
android:id="@+id/tv9"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 10"
android:id="@+id/tv10"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 11"
android:id="@+id/tv11"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textview_background"
android:text="Linija 12"
android:id="@+id/tv12"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/LL2"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn3"
android:text="Btn3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn4"
android:text="Btn4"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_textview_background"
android:text="Linija 13"
android:id="@+id/tv13"
android:layout_gravity="end"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_textview_background"
android:text="Linija 14"
android:id="@+id/tv14"
android:layout_gravity="end"
android:visibility="gone"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
test activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
Thread mainThread = new Thread(
new Runnable() {
@Override
public void run() {
start_game();
}
}
);
mainThread.start();
}
public void start_game () {
Button btn1 = (Button) findViewById(R.id.btn1);
... btn4
TextView tv1 = (TextView) findViewById(R.id.tv1);
... tv14
LinearLayout LL1 = (LinearLayout) findViewById(R.id.LL1);
LinearLayout LL2 = (LinearLayout) findViewById(R.id.LL2);
show_line(tv1, 1000);
show_line(tv2, 1000);
show_line(tv3, 1000);
show_line(tv4, 1000);
show_line(tv5, 1000);
btnLL(LL1, btn1, btn2, tv6, tv7);
if (btn1.isPressed()) show_line(tv8, 1000);
if (btn2.isPressed()) show_line(tv9, 1000);
show_line(tv10, 1000);
show_line(tv11, 1000);
show_line(tv12, 1000);
public void show_line(final TextView tv, int duration) {
if (x) x=false;
viewArray[viewIndex++]= tv;
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv.getHandler().post(new Runnable() {
public void run() {
tv.setVisibility(View.VISIBLE);
}
});
}
public void btnLL (final LinearLayout LL, Button btnLeft, Button btnRight, final TextView tvLeft, final TextView tvRight) {
linearArray[llIndex++]=LL;
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.VISIBLE);
}
});
btnLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.GONE);
}
});
tvLeft.getHandler().post(new Runnable() {
public void run() {
tvLeft.setVisibility(View.VISIBLE);
}
});
x = true;
myViewArray[myViewIndex++]=tvLeft;
}
});
btnRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.GONE);
}
});
tvRight.getHandler().post(new Runnable() {
public void run() {
tvRight.setVisibility(View.VISIBLE);
}
});
x = true;
myViewArray[myViewIndex++]=tvRight;
}
});
while (!x){}
}