In the fragment there is an EditText where the user must enter any number (the input format is numeric). However, for some reason, the input does not occur. The numbers on the keyboard are pressed, and the characters in EditText are not displayed. What is the problem?
Here is the xml fragment:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CalculatorFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/calculator_text" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="16dp" /> <EditText android:layout_width="match_parent" android:layout_height="20dp" android:id="@+id/weight" android:layout_below="@+id/text" android:layout_marginTop="20dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:hint="@string/weight" android:inputType="number"/> <EditText android:layout_width="match_parent" android:layout_height="20dp" android:id="@+id/height" android:layout_below="@+id/weight" android:layout_marginTop="20dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:hint="@string/height" android:inputType="number"/> <Button android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/height" android:layout_marginTop="30dp" android:text="@string/count" android:onClick="onClick"/> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/count" android:layout_marginTop="20dp"/> </RelativeLayout> And here is the fragment code:
UPDATE2:
package asus.example.com.fitnessapp; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * A simple {@link Fragment} subclass. */ public class CalculatorFragment extends Fragment { TextView textView; EditText eWeight, eHeight; int nWeight, nHeight; public CalculatorFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_calculator, container, false); Button button = v.findViewById(R.id.count); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { eWeight = v.findViewById(R.id.weight); eHeight = v.findViewById(R.id.height); nWeight = Integer.parseInt(eWeight.getText().toString()); nHeight = Integer.parseInt(eHeight.getText().toString()); if (nWeight-nHeight<100) { textView.setText("Normal weight"); } } }); return v; } }