L4c0573
L4c0573

Reputation: 343

EditText.getText().toString() is not returning current value

I´m trying to get the text that was inserted into a EditText in the UI.

The UI looks like this. In a fragment the EditText for name has a default value "Hello".

enter image description here

After the user has entered a new value (for example "Hello2") I´d like to get the new value when the user clicks the Add Button.

enter image description here

But what I recive is still the default value "Hello".

My Code looks like this:

XML

<TextView
    android:id="@+id/textView_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/backgroundtint_color"
    android:text="Name"
    android:textColor="@color/ntext_foreground"
    android:textSize="@dimen/ntext_fontsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.075"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/textview_addingredient"
    app:layout_constraintVertical_bias="0.050" />

JAVA Code in Fragment

public class AddIngredient extends Fragment{

//Controls
public EditText etN;
public EditText etK;
public EditText etF;
public EditText etC;
public EditText etP;
public TextView tv1;

//ViewModel
AddIngredientViewModel model;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_addingredient, container, false);

    // Get the values
    tv1 = rootView.findViewById(R.id.textview_addingredient);
    etN = rootView.findViewById(R.id.editText_name);
    etK = rootView.findViewById(R.id.editText_kcal);
    etF = rootView.findViewById(R.id.editText_fat);
    etC = rootView.findViewById(R.id.editText_carbs);
    etP = rootView.findViewById(R.id.editText_protein);

    // Inflate the layout for this fragment
    return rootView;
}


public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.fragmentbutton_addingredient).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Get current values
            String name = etN.getText().toString();
            int kcal = Integer.parseInt(etK.getText().toString().trim());
            int fat = Integer.parseInt(etF.getText().toString().trim());
            int carbs = Integer.parseInt(etC.getText().toString().trim());
            int protein = Integer.parseInt(etP.getText().toString().trim());

            model = new ViewModelProvider(requireActivity()).get(AddIngredientViewModel .class);

            Ingredient ingredient = new Ingredient(name, kcal, fat, carbs, protein){};
            model.SaveIngredient(ingredient);
        }
    });
}

}

So has anyone an idea, what I have tho change to recive the current value? Thanks!

-- EDIT --

Entire XML:

<?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"
    android:background="@color/screen_background"
    tools:context=".fragments.AddIngredient">

    <TextView
        android:id="@+id/textview_addingredient"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"
        android:text="Add Ingredient"
        android:textColor="@color/headercolor"
        android:textSize="@dimen/header_fontsize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.126"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <!-- Name -->
    <TextView
        android:id="@+id/textView_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Name"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_addingredient"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="textPersonName"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        android:text="Hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_name"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Kcal -->
    <TextView
        android:id="@+id/textView_kcal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Kcal"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_name"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_kcal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_kcal"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Fat -->
    <TextView
        android:id="@+id/textView_fat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Fat"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_kcal"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_fat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_fat"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Carbs -->
    <TextView
        android:id="@+id/textView_carbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Carbs"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_fat"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_carbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_carbs"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Protein -->
    <TextView
        android:id="@+id/textView_protein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Protein"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.078"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_carbs"
        app:layout_constraintVertical_bias="0.052" />

    <EditText
        android:id="@+id/editText_protein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_protein"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/fragmentbutton_addingredient"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:background="@color/button_background"
        android:gravity="left|center_vertical"
        android:padding="20dp"
        android:text="Add"
        android:textColor="@color/button_foreground"
        android:textSize="@dimen/button_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.243"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_protein"
        app:layout_constraintVertical_bias="0.250" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 148

Answers (1)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

Remove the default static text set to the editTextName field in your xml layout file. Due to this, the value is always getting stored as Hello. You shouldnt set text in EditTextview.

  <EditText
         android:id="@+id/editText_name"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"

  android:backgroundTint="@color/backgroundtint_color"
          android:ems="10"
         android:inputType="textPersonName"
         android:textColor="@color/ntext_foreground"
         android:textSize="@dimen/ntext_fontsize"
         android:text="Hello"  //Remove this line.

Upvotes: 1

Related Questions