Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Need help outputting “You must enter a name” when nameText is null. With the code I currently have it doesn’t recognize when the nameText is empty

Posted on November 11, 2022 By

Solution 1 :

Probably you need to check if the text of your EditText is null, not the EditText widget itself.

Try changing your condition

if(nameText != null) {

to

if (!TextUtils.isEmpty(nameText.getText())) {

Problem :

MainActivity

package com.example.greetings_john_sporn;

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 {

EditText nameText;
TextView textView;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nameText = (EditText) findViewById(R.id.nameText);
    textView = (TextView) findViewById(R.id.textGreeting);
    btn = (Button) findViewById(R.id.buttonSayHello);
    nameText.setText("");
}

public void sayHello(View view) {

    if(nameText != null) {
        textView.setText("Hello " + nameText.getText());
        btn.setEnabled(true);
    } else {
        textView.setText("You must enter a name");
        btn.setEnabled(false);
    }

}
}

Need help setting the text to “You must enter a name” when the nameText is null. For some reason even when the nameText field is empty it still outputs “Hello” when it should be outputting “You must enter a name”.

Comments

Comment posted by John S

Thank you! This is fixed the issue. Unfortunately I can’t upvote it until I get more reputation.

Comment posted by Vasily Kabunov

@JohnS Happy to hear that the answer helped you. You can mark the answer as correct – click grey V icon at the left.

READ  [FIXED] android - DrawerActivity with Tabs
Powered by Inline Related Posts
Android Tags:android-edittext, android-studio, java, textview, xml

Post navigation

Previous Post: [FIXED] android – W/Firestore: (24.1.2) [CustomClassMapper]: No setter/field for about found on class
Next Post: [FIXED] android – FusedLocationProviderClient and LocationCallback() object queries

Related Posts

[FIXED] java – TextInputLayout “setError(null)” before “setErrorEnabled(false)” necessary? Android
[FIXED] android night mode – how the setting of AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM relate to result of getResources().getConfiguration().uiMode Android
[FIXED] iOS/Android – Disabling Mobile Data (Cellular Data) via SDK Android
[FIXED] android – Printing from mobile browser to bluetooth printer Android
[FIXED] android – How to enable picking Google Docs files (kept on Google Drive) with Intent? Android
[FIXED] java – Need assistance with my deprecated FirebaseInstanceId code Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme