I have a really weird problem where the setVisibility()
function works in one part of the code, but in the other it doesn't even though they are the exact same.
Here is my code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
TextView verifyMsg = findViewById(R.id.verifyEmailMessage);
ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
bottomPlayer.setVisibility(View.VISIBLE);
} else {
bottomPlayer.setVisibility(View.INVISIBLE);
}
if(!fAuth.getCurrentUser().isEmailVerified()){
verifyMsg.setVisibility(View.VISIBLE);
...
The first if statement gets triggered and doesn't change the visibility at all. When second when gets triggered it suddenly gets triggered. So the Textviews visibility gets changed but the imageviews doesn't. Am I being extremely stupid or what did I do wrong?! No logcat error btw. Here is also my xml file:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/verifyEmailMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Bitte bestätigen sie die Email"
android:textAlignment="center"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#111111"
android:id="@+id/bottomPlayer"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
I don't know what to do anymore. This problem is stupid.
EDIT: since the problem seems to lie somewhere else: here is my whole class:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
setWifiLock();
BottomNavigationView bottomNavigationView =
findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this,
R.id.fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
createNotificationChanel();
FirebaseAuth fAuth = FirebaseAuth.getInstance();
//AlertDialog.Builder resetAlert = new AlertDialog.Builder(this);
TextView verifyMsg = findViewById(R.id.verifyEmailMessage);
Button verifyBtn = findViewById(R.id.verifyEmailBtn);
ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
bottomPlayer.setVisibility(View.VISIBLE);
} else {
bottomPlayer.setVisibility(View.INVISIBLE);
}
if(!fAuth.getCurrentUser().isEmailVerified()){
verifyBtn.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);
NotificationCompat.Builder builder = new
NotificationCompat.Builder(MainActivity.this, "lemubitA")
.setSmallIcon(R.drawable.ic_baseline_message_24)
.setContentTitle("Bitte bestätige deine Email")
.setContentText("- Schmitties Technik Freunde")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(100, builder.build());
}
verifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fAuth.getCurrentUser().sendEmailVerification().addOnSuccessListener(new
OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Bestägungsemail wurde
gesendet", Toast.LENGTH_SHORT).show();
verifyBtn.setVisibility(View.GONE);
verifyMsg.setVisibility(View.GONE);
}
});
}
});
}
private void setWifiLock() {
WifiManager.WifiLock wifilock = ((WifiManager)
getApplicationContext()
.getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
wifilock.acquire();
}
private void createNotificationChanel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "BaywatchBerlin";
String description = "Mit Klaas Heufer Umlauf";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new
NotificationChannel("lemubitA", name, importance);
notificationChannel.setDescription(description);
notificationChannel.setSound(null, null);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
Alright I found the problem: I haven't provided it in my question since I thought it didn't matter. This is the code where the setVisibility
doesn't work
ConstraintLayout constraintLayout = findViewById(R.id.main_activity);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
bottomPlayer.setVisibility(View.VISIBLE);
constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,
R.id.bottomPlayer,ConstraintSet.TOP,0);
} else {
bottomPlayer.setVisibility(View.INVISIBLE);
constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,
R.id.bottomNavigationView,ConstraintSet.TOP,0);
}
constraintSet.applyTo(constraintLayout);
and this is the one where it works:
ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
bottomPlayer.setVisibility(View.VISIBLE);
} else {
bottomPlayer.setVisibility(View.INVISIBLE);
}
The ConstraintSet
is what prohibiting the setVisibility
. Any ideas??
There may be an easier approach but i just disjointed the two functions and now it works fine:
ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
bottomPlayer.setVisibility(View.VISIBLE);
} else {
bottomPlayer.setVisibility(View.INVISIBLE);
}
ConstraintLayout constraintLayout = findViewById(R.id.main_activity);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
if((((ServiceState) this.getApplication()).isRunning())){
constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,R.id.bottomPlayer,ConstraintSet.TOP,0);
} else {
constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,R.id.bottomNavigationView,ConstraintSet.TOP,0);
}
constraintSet.applyTo(constraintLayout);