Why does my android studio EditText view not display input, output, or the hint?



My EditText does not work like the videos and articles I have found. The EditText does not show what the user inputted at the time of input or when I tell the output to show there, the hint I assigned it also does not show up. Right now I have the output going to a TextView but can greatly reduce lines of code if I can get the EditText working properly. I am new to this and have troubles understanding. Thank you for the help.

Here is the xml file:

//DOES NOT SHOW INPUT AT TIME OF INPUT, OR WHEN TOLD TOLD BY OUTPUT. HINT DOES NOT SHOW EITHER-------
<EditText
android:id="@+id/MetalCounter"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:ems="10"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="15sp"
android:hint="0"
android:textColorHint="#000000"
android:inputType="number"
app:layout_constraintRight_toLeftOf="@id/guideline"
app:layout_constraintTop_toBottomOf="@id/ResourceSubmitButton" />

 //is put in top left corner for testing purposes---------------------------------
<TextView
android:id="@+id/MetalResult"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:text=""
android:textAlignment="center"
android:textColor="#000000"
android:textSize="15sp"
 />

and here is the java file:

package com.example.game;

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 {

// These are the global variables
EditText MetalCounter;
TextView MetalResult;
Button ResourceSubmitButton;
int Metal;

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

    //to get from user input and into variable form
    MetalCounter = (EditText) findViewById(R.id.MetalCounter);
    MetalResult = (TextView) findViewById(R.id.MetalResult);
    ResourceSubmitButton = (Button) findViewById(R.id.ResourceSubmitButton);

    //Submit button
    ResourceSubmitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to receive and show the inputted values into the MetalResult TextView
            MetalResult.setText(MetalCounter.getText().toString());
   

            //This is a test to see if the result would show up but does not
            Metal = Integer.parseInt(MetalCounter.getText().toString());
            MetalCounter.setText(MetalCounter.getText().toString());
        }
    });
}
}

Answer

Well, I think the problem lies here in this part

android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:ems="10"
android:textSize="15sp"

These attributes are not well-defined because you have set the layout_margin to 4dp which causes the view to shrink. so instead of having a width of 40dp, now you have a width of 32dps, similarly you have a height of only 12dps which is very small and doesn’t match with the selected textSize

So, my suggestion is changing the width and height attributes to wrap_content as follows

android:layout_width="wrap_content"
android:layout_height="wrap_content"

and you can keep everything else as it’s.

Remarks

  • Consider changing your variables to follow the camelCase convention
// Before
EditText MetalCounter;
TextView MetalResult;

//After
EditText metalCounter;
TextView metalResult;
  • Change the hint color’s opacity in order to differentiate it from the text itself
  • If you still have a problem so it may be due to the constraints in the bottom. If so, please post the full layout file.


Source: stackoverflow