Jesse Galdamez
Jesse Galdamez

Reputation: 81

Cannot get Toast from EditText

I have a multi-edit text field in a card view I want to get a string from and show in a toast before moving on to another activity. I can fill in text just fine when running the activity, but when I click the submit button nothing happens. What am I doing wrong or what am I missing in the code?

Here is my XML file:

        <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        card_view:cardCornerRadius="4dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="0dp">

        <EditText
            android:id="@+id/value1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/choice_hint1c"
            android:inputType="textMultiLine" />

    </android.support.v7.widget.CardView>

    <Button
        android:id="@+id/choiceButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_btn_shape"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_marginStart="125dp"
        android:text="@string/submit"
        android:textColor="#FFFFFF" />

Class file:

   import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class Choice extends AppCompatActivity {


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


    }

    public void addListenerOnButton() {

        Button submitButton = (Button) findViewById(R.id.choiceButton);
        final EditText editTextV1 = (EditText) findViewById(R.id.value1);
        final String valueOne = editTextV1.getText().toString();


        submitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (valueOne.equals("")) {
                    //happens if first field is empty
                    Toast.makeText(
                            getApplicationContext(),
                            "First field is empty",
                            Toast.LENGTH_SHORT).show();

                } else {

                    //save selection

                    Toast.makeText(
                            getApplicationContext(),
                            valueOne,
                            Toast.LENGTH_SHORT).show();

                    //save response to SQLite

                    Intent intentSurvey = new Intent(Choice.this, MainActivity.class);
                    startActivity(intentSurvey);

                }

            }

        });


}
}

Upvotes: 0

Views: 444

Answers (4)

John Joe
John Joe

Reputation: 12803

  1. You miss to add addListenerOnButton() inside onCreate method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choice);
    addListenerOnButton(); // add this
    }
    
  2. To check whether EditText is empty

     if(TextUtils.isEmpty(valueOne)) 
    
  3. Move String valueOne = editTextV1.getText().toString(); inside onClick

Upvotes: 3

AlterionX
AlterionX

Reputation: 81

First is that I don't see the method addListenerOnButton() within the onCreate. This was mentioned.

Second is that the final String valueOne is being set on create, with the initial value of the EditText. This value is then persisted through closure of the anonymous class.

You need to get the String version of the text field within the listener.

Upvotes: 1

Josh Beckwith
Josh Beckwith

Reputation: 1540

You're on the right track. When asserting on a string coming from an EditText, I prefer to use:

if(Strings.isNullOrEmpty(someStringHere)) {
    do this
}

This is utilized from the espresso library. Import to your class file with

import android.support.test.espresso.core.deps.guava.base.Strings;

Make sure to add Espresso to your build.gradle file.

A string can be null, but not equal "", that's why I pursue the route mentioned above. I'm assuming your toast is showing a 'null' String.

If this does not work, I suspect it's because you're trying to find a view by id within an onClick method. onClick (and other 'on' methods for the matter of fact) have different context than your activity has. That's why When making a toast, you had to say CLASSNAME.this, rather than just 'this'.

Try pulling out your edit text, and making it a private member of your class. Find the edit text by id as you did before, but do it above and outside the onClickListener. Once inside the onclick method, you should be able to get the text values as you have already.

If neither of these solve your issue, you're going to have to post more of your code.

Upvotes: 1

Mixon McLaren
Mixon McLaren

Reputation: 267

I don't know what is Choice.this but first argument of Toast need be Context Example: Toast.makeText(getContext(), "Your text for toast", Toast.LENGTH_SHORT).show();

Upvotes: 1

Related Questions