Tschogge
Tschogge

Reputation: 146

Unable to get Text out of EditText

So I want to read the input of an EditText field. It is not working. I debugged it and I saw, that the EditText view was listed there. But when I printed it to the console, it was empty. I don't know why, because I did put a text in it. Also, the ID is right, and I really don't know what's wrong

Here is the View where the EditText is:

<?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=".counters.AddCounter">

    <TextView
        android:id="@+id/txtNewCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/allerta"
        android:text="@string/addNewCounter"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSaveCounter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/allerta"
        android:text="@string/save"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtCounterName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/allerta"
        android:text='@string/newCounterName'
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtNewCounter" />

    <EditText
        android:id="@+id/inpuTextNewCounterName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/name"
        android:ems="10"
        android:hint="@string/name"
        android:inputType="textPersonName"
        android:singleLine="true"
        android:text="Cookies don't"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtCounterName" />

    <TextView
        android:id="@+id/txtCounterEntryValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/allerta"
        android:text="@string/entryNum"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inpuTextNewCounterName" />

    <EditText
        android:id="@+id/numEntryNum"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:hint="@string/number"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtCounterEntryValue" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here is my code to evaluate the form:


public class AddCounter extends AppCompatActivity {

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

        //Setting save button action listener
        findViewById(R.id.btnSaveCounter).setOnClickListener(new ActionListeners().getSaveCounter());
    }

    public static void evaluateCounter(View view) throws NoNameException, NumberOutOfBoundException {
        //Getting context
        Context context = view.getContext();

        //Counter Name
        View viewEdit = View.inflate(context, R.layout.activity_add_counter, null);
        EditText counterName = viewEdit.findViewById(R.id.inpuTextNewCounterName);

        System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");

        //Input to string
        String name = counterName.getText().toString();

        //If no name was specified exception thrown
        if (name.equals("")) {
            throw new NoNameException(context.getString(R.string.noNameSpecified));
        }

        //get entry number
        EditText numberView = View.inflate(context, R.layout.activity_add_counter, null).findViewById(R.id.numEntryNum);

        //Number
        int number = 0;
        //If nothing specified
        if (!(numberView.getText().toString().equals(""))) {
            if (Integer.parseInt(numberView.getText().toString()) > 1000 || Integer.parseInt(numberView.getText().toString()) < -1000) {
                throw new NumberOutOfBoundException(context.getString(R.string.numOutOfBound));
            }

        }
        //New Counter Object
        Counter counter = new Counter(number, name);

        //storing counter
        ToJson toJson = new ToJson();
        toJson.storeCounter(counter, context);
    }
}

As you can see here, this if-statement is always true. To look, what the text looks like, I printed out a text:

System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");

I did put a word in the input and the output is empty, as you can see here:

I/System.out: Cookies fly like:!

Here is my ActionListener:¨

public class ActionListeners {
    private OnClickListener saveCounter = v -> {
        try {
            //Evaluate Counter
            AddCounter.evaluateCounter(v);
        } catch (NoNameException e) {
            //If no name was specified
            CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.noNameSpecified));
            e.printStackTrace();
        } catch (NumberOutOfBoundException e) {
            //If entry is higher or lower than available
            CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.numOutOfBound));
        }
    };

    public OnClickListener getSaveCounter() {
        return this.saveCounter;
    }

}

As you can see, I did set the text to "Cookies can't fly" in the xml file, however, if I change the text to something different in the app, the output is the text I did set in the XML.

I/System.out: Cookies fly like:Cookies can't fly!

What is the problem?

Upvotes: 0

Views: 73

Answers (1)

Elfnan Sherif
Elfnan Sherif

Reputation: 629

try this

public class AddCounter extends AppCompatActivity{


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

            //declare Counter Name EditText
            EditText counterName = findViewById(R.id.inpuTextNewCounterName);

            //declare btnSaveCounter Button
            Button btnSaveCounter = findViewById(R.id.btnSaveCounter);
            //declare  get entry number EditText
            EditText numberView = findViewById(R.id.numEntryNum);


            //btnSaveCounter setOnClickListener
            btnSaveCounter.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {

                    //Input to string
                    String name = counterName.getText().toString();
                    System.out.println("Cookies fly like:" + name + "!");

                    //If no name was specified
                    if (name.equals("")){
                        System.out.println( "getString(R.string.noNameSpecified)" );//todo
                    }
                    
                    //If nothing specified
                    String numbrv = numberView.getText().toString();
                    if (!(numbrv.equals(""))){
                        if (Integer.parseInt(numbrv) > 1000 || Integer.parseInt(numbrv) < -1000) {
                            System.out.println( "getString(R.string.numOutOfBound)" );//todo
                        }
                    }

                    //Number
                    int number = 0;
                    
                    //New Counter Object
                    Counter counter = new Counter(number, name);

                    //storing counter
                    ToJson toJson = new ToJson();
                    toJson.storeCounter(counter, context);
                    


                }  });

            




        }

        

    }

Upvotes: 1

Related Questions