I know this question has been asked several times, and with different solutions, but I have tried all I could find here, and none worked. As the title suggest, in Android Studio, my EditText widget's field isn't showing any type of text, even what it has by default (like the hint and default text). The text is there if I print it out , but not displaying. It is for a simple login page.
This is the xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:text="Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:id="@+id/atcoNameTag" />
<TextView
android:text="Password : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/atcoNameTag"
android:layout_marginTop="30dp"
android:id="@+id/passTag"
android:layout_alignStart="@+id/atcoNameTag"/>
<TextView
android:text="Role : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/roleNameTag"
android:layout_below="@+id/passTag"
android:layout_alignStart="@+id/atcoNameTag"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:layout_above="@+id/passTag"
android:layout_centerHorizontal="true"
android:id="@+id/insertName"
android:layout_alignTop="@+id/atcoNameTag"
android:layout_toEndOf="@+id/atcoNameTag"
android:layout_alignStart="@+id/insertPass"
android:hint="username"
android:cursorVisible="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="@+id/passTag"
android:layout_toEndOf="@+id/passTag"
android:id="@+id/insertPass"
android:layout_alignTop="@+id/passTag"
android:hint="password"
android:cursorVisible="true"/>
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignTop="@+id/roleNameTag"
android:layout_toEndOf="@+id/roleNameTag"
android:layout_alignStart="@+id/insertPass"/>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
And this is the java file :
package mecals.mecalsapp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
Button nameBtn = (Button) findViewById(R.id.loginButton);
nameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameEntry = (EditText) findViewById(R.id.insertName);
nameEntry.setTextColor(Color.BLACK);
String name = nameEntry.getText().toString();
//TODO username checking, username not showing , need devC Team's work
if (name.length() > 0) {
Toast toastYes = Toast.makeText(getApplicationContext(), "Hi there, " + name + "!", Toast.LENGTH_SHORT);
toastYes.show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast toastNo = Toast.makeText(getApplicationContext(), "Please insert name !", Toast.LENGTH_SHORT);
toastNo.show();
}
EditText passwordEntry = (EditText) findViewById(R.id.insertPass);
passwordEntry.setTextColor(Color.BLACK);
String pass = passwordEntry.getText().toString();
//TODO password checking , need devC Team's work
}
});
}
}
And these are the solutions I have tried , to no avail :
Difference between content_main.xml and activity_main.xml?
Android edittext typed text not showing
Android EditText typed value not showing
Text not displayed in Android editText when changing android:hint to android:text
EditText in Android doesn't show text when typing while using the on-screen keyboard
Edit Text hints not appearing on later version SDK
edittext not showing the typed text in android
In short, I have
-set the text colour to Black , with nameEntry.setTextColor(Color.BLACK);
-I have added android:windowSoftInputMode="adjustPan" in the manifest
-I have NOT used something like android:gravity , just android:layout
-the cursor is set to visible
-I even checked the height of the widget
-I checked the size of the text with android:ems to be 10
-and the input type is set to text for the first field and textPassword for the second
Help !
PS : before anyone asks , my Android Studio version is 2.2.3 , and the API I am using is min 21.
The problem is that the height of the EditText field is much to small to display the text. This is present because both the top and the bottom of the EditText field are set according to the TextView left of the EditText. But since the height of the TextView is smaller than the normal height of the EditText you force it to be very narrow (and so there is not enough space for the Text). If you remove this line
android:layout_alignTop="@+id/atcoNameTag"
you can see that the text gets displayed normally.
There are actually several ways to fix this:
It is probably a good idea to go with the last solution and put the Textview and EditText fields that belong together into a separate LinearLayout and remove the align calls like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Name : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/atcoNameTag" />
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/insertName"
android:hint="username"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:id="@+id/second"
android:layout_below="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Password : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/passTag"/>
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/insertPass"
android:hint="password"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
android:layout_marginTop="10dp"
>
<TextView
android:layout_weight="30"
android:text="Role : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/roleNameTag"/>
<Spinner
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/spinner"/>
</LinearLayout>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.