I am very new to java but cant wrap my head around why the if statement doesn't work. The value of the button is Press Me For Answer. The if statement is just not run. but as you can see in the code when i log the value of the button, it shows Press Me For Answer, so why is the if not running????
package com.noob.first;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.PrintStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = findViewById(R.id.doSome);
final TextView tv = findViewById(R.id.textView2);
boolean pressed = true;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val = btn.getText().toString();
PrintStream psObj = new PrintStream(System.out);
psObj.println(val);
psObj.flush();
if(val == "Press Me For Answer"){
btn.setText("ahhhh");
};
btn.setText(String.format("%sOoof", val));
}
});
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="Why Am I Here?"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<Button
android:id="@+id/doSome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="81dp"
android:layout_marginLeft="81dp"
android:layout_marginTop="69dp"
android:layout_marginEnd="82dp"
android:layout_marginRight="82dp"
android:text="Press Me For Answer"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>