Question: I need help fixing my codes. They have a few errors and therefore isn't running. Please share Screenshot of the code running properly. Earlier in

I need help fixing my codes. They have a few errors and therefore isn't running. Please share Screenshot of the code running properly.
Earlier in this course, you created a simple layout in Android Studio. For this assignment, you will be building off of the UI you already created and will practice some simple coding with Android Studio. It will be important to use this as an opportunity to continue getting a feel for the tool, while also reviewing your skills.
Prompt
Open the file you saved from the Module Three assignment in Android Studio, then edit the MainActivity.java file.
Specifically, you must address the following rubric criteria:
Create a function that displays a greeting. In Android Studio, create a new public function named SayHello() that returns a void. The function will need to accept a View as a parameter. Within the function, ensure that it displays a message and check that the contents of nameText are not null.
Create validation of the elements and inputs. To complete this task you must do all of the following:
If the content of nameText is not null, write "Hello" and the contents of nameText to textGreeting.
If the content of nameText is null, write "You must enter a name" to textGreeting.
On activity_main.xml, set the onClick of buttonSayHello to SayHello().
Dynamically enable and disable the button for appropriate situations. For buttonSayHello, the following pattern should be created:
Dynamically disable buttonSayHello if there is no text in nameText.
Dynamically enable buttonSayHello when text is entered in nameText.
What to Submit
Here are my codes.
MainActivity.java file:
package com.example.myveryfirstapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText nameText;
private TextView textGreeting;
private Button buttonSayHello;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = findViewById(R.id.nameText); // Assuming you have an EditText with id "nameText"
textGreeting = findViewById(R.id.textGreeting); // Assuming you have a TextView with id "textGreeting"
buttonSayHello = findViewById(R.id.buttonSayHello); // Assuming you have a Button with id "buttonSayHello"
buttonSayHello.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
SayHello(view);
}
});
// Initially disable the button if nameText is empty
if (nameText.getText().toString().isEmpty()){
buttonSayHello.setEnabled(false);
}
// Add a TextWatcher to dynamically enable/disable the button based on text input
nameText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){
// Not needed for this task
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
buttonSayHello.setEnabled(!charSequence.toString().isEmpty());
}
@Override
public void afterTextChanged(Editable editable){
// Not needed for this task
}
});
}
public void SayHello(View view){
String name = nameText.getText().toString();
if (name != null && !name.isEmpty()){
textGreeting.setText("Hello "+ name +"!");
} else {
textGreeting.setText("You must enter a name");
}
}
}
activity_main.xml file:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!