In my testing code app I have a problem with setting if statement with getVisibility in it.
Basicaly, app should reveal (from GONE to VISIBLE) 1 TextView tv1, and then reveal 1 LinearLayout LL1 with 2 Buttons inside it btn1 & btn2. When either is pressed, LL1 should disappear (from VISIBLE back to GONE) and in its place, new tv2 or tv3 should appear (from GONE to VISIBLE) depending on which button is pressed. tv4 should wait until LL1 is GONE, and then appear. A 1s interval is set between appearances.
Everything runs ok, until IF part which doesnt work. I have also tried
if (btn1.isPressed() || btn2.isPressed())
and
if (tv2.getVisibility()==View.VISIBLE || tv3.getVisibility()==View.VISIBLE)
but it didnt work. I guess the problem is the way I acces getVisibility.
I even tryed defining global boolean x=false;, and then in OnClickListener setting x=true; but then if (x) {... returns false!?
HOW TO MAKE IF STATEMENT WORK!?
Everything (tv1..tv4, btn1, btn2, LL1) in layout file is predefined and inittialy set to GONE. This is my 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() {
startApp();
}
}
);
mainThread.start();
}
public void startApp() {
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
TextView tv1 = (TextView) findViewById(R.id.line1);
TextView tv2 = (TextView) findViewById(R.id.line2);
TextView tv3 = (TextView) findViewById(R.id.line3);
TextView tv4 = (TextView) findViewById(R.id.line4);
LinearLayout LL1 = (LinearLayout) findViewById(R.id.LL1);
show_line(tv1, 1000);
btnLL(LL1, btn1, btn2, tv2, tv3);
//THIS IS THE PROBLEM IT ALWAYS RETURNS TRUE
if (LL1.getVisibility()==View.GONE) show_line(tv4, 1000);
}
public void show_line (final TextView tv, int duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv.getHandler().post(new Runnable() {
public void run() {
tv.setVisibility(View.VISIBLE);
}
});
/*tv.setVisibility(View.VISIBLE); CANT USE THIS BECAUSE IM NOT IN UI THREAD*/
}
public void btnLL (final LinearLayout LL, Button btnLeft, Button btnRight, final TextView tvLeft, final TextView tvRight) {
//LL.setVisibility(View.VISIBLE); AGAIN NOT IN UI THREAD
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);
}
});
}
});
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);
}
});
}
});
}
boolean x=false;then inonClickfor both buttons at the very end I addedx=true;and instead of IF I usedwhile(!x) {}an empty loop so it would wait for action frombtnLL()method. In the beggining ofshow_line()method I addedif (x==true) x=false;and it worked like I wanted it. - Nemanja