I have a form where the user enter the client info. When I try to validate the fields I have a problem, I used editText.setError(errorMessage) and it's showing the error message, but it shown on the top of my EditText making it confusing to the user. Like the image below.
Any ideas of how I can fix this?
EditText on XML
<LinearLayout
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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="br.com.intelecto.intesigmobile.activity.ClienteEditActivityFragment">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_nome"
android:imeActionId="#+id/cli_name"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPersonName"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_telefone"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="phone"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_email"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_cpf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_cpf"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="number"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_endereco"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_endereco"
android:imeActionId="#+id/cli_address"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPostalAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_bairro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_bairro"
android:imeActionId="#+id/cli_district"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="#+id/sp_estados"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
<Spinner
android:id="#+id/sp_cidade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:prompt="#string/cliente_cidade"/>
This ClienteEditActivity holds two fragments with different forms so I listen for a action menu click at the fragment, when menu is clicked I use this to call may validation:
if (!isValidEmail(etEmail)) {
cancel = true;
focusView = etEmail;
}
//after other validations
if (cancel){
focusView.reqeustFocus();
} else {
//save datas
}
The isValidEmail method is like this:
protected boolean isValidEmail(EditText editText) {
validate = new Validate(editText);
validate.addValidador(new ValidadorVazio(getContext()));
validate.addValidador(new ValidadorEmail(getContext()));
return validate.isValid();
}
My Validateclass looks like this:
public class Validate{
private EditText etFonte;
public Validate(EditText etFonte){
this.etFonte = etFonte;
}
public void addValidador(AbstractValidador validador){
lalidadores.add(validador);
}
public boolean isValid(){
for (AbstractValidador validator : lValidadores) {
try {
if (!validator.isValid(etFonte.getText().toString())) {
if(errorNotification != null) {
errorNotification.onInvalid(this);
} else {
setSourceViewError(validator.getMessage(), validator.getErrorDrawable());
}
return false;
} else {
if(errorNotification != null) errorNotification.onValid(this);
}
} catch (ValidadorException e) {
e.printStackTrace();
if(errorNotification != null) {
errorNotification.onInvalid(this);
} else {
setSourceViewError(e.getMessage(),validator.getErrorDrawable());
}
return false;
}
}
etFonte.setError(null);
return true;
}
private void setSourceViewError(String errorMessage, Drawable errorDrawable) {
if(errorDrawable != null) {
etFonte.setError(errorMessage, errorDrawable);
} else {
etFonte.setError(errorMessage);
}
}
}
Set error on TextInputLayout instead of EditText, Here's your code...
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_nome"
android:imeActionId="#+id/cli_name"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPersonName"
android:maxLines="1"
android:singleLine="true"/>
Get your TextInputLayout at runtime and apply following code...
TextInputLayout textInputLayout = (TextInputLayout) findVeiwById(R.id.textInputLayout);
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Please enter a valid license number");
It will produce output something like this...
In your layout's root element, find the following line and remove it.
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Everything will be fine!
I assume that you have set an error on wrong reference of edit text. You have to set error on the same edit email edit text and also have to request focus on same edit text.
Here is the code snippet for activity:-
private EditText mEmailET;
#Override
protected void onCreate(Bundle savedInstanceState) {
mEamilET = (EditText)findViewById(R.id.et_email);
}
private void setSourceViewError(String errorMessage, Drawable errorDrawable) {
if(errorDrawable != null) {
mEamilET.setError(errorMessage, errorDrawable);
mEamilET.requestFocus();
} else {
mEamilET.setError(errorMessage);
mEamilET.requestFocus();
}
}
If you still face the issue please send the activity code too for better understanding for us and appropriate answer.
Do not use "include" tag to show your layout. Just replace that tag with your layout and you good to go. And you would want to set marginTop="60dp" so the toolbar is accessible.
Related
I'm trying to implement a layout where user will input their phone number. The phone number will be 11 digits. And so I wanted to build a text input field with a fixed width that can accommodate 11 characters.
NOTE: I don't want the input field to be wider than 11 characters. Let's say each character is 10px wide. So the input field should be 11*10px = 110px wide (I'm not taking the drawable icon into account here).
Here's what I've tried in XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_48"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
But this produces an output like the following:
As you can see, despite using ems and minEms, the field is 2 characters wide (it's 0 character wide if I don't add android:text). But I want it to have a width of 11 characters (not more or less). Why is ems not effective here and what's the solution?
Just edit your layout width
from
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
I just edited your code
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_48"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
You should set a 11 digits monospace text in your TextInputEditText widget (for example 12345678901), then define a ViewTreeObserver.OnGlobalLayoutListener in your fragment, that calculates the width when the layout is available to be measured.
You could instantiate the observer in onResume() of your fragment, and remove it in onPause(). You should also store the width during the rotation of the activity.
Here below a full working example:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ConstraintLayout constraintLayout;
TextInputEditText textInputEditText;
int viewWidth;
ViewTreeObserver.OnGlobalLayoutListener viewTreeObserver = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
viewWidth = textInputEditText.getMeasuredWidth();
textInputEditText.setWidth(viewWidth);
textInputEditText.setText("");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
viewWidth = savedInstanceState.getInt("viewWidth", 0);
}
constraintLayout = findViewById(R.id.id_constraintlayout);
textInputEditText = findViewById(R.id.phone);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("viewWidth", viewWidth);
}
#Override
public void onResume() {
super.onResume();
if (viewWidth == 0) {
textInputEditText.setText("12345678901");
ViewTreeObserver vto = constraintLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(viewTreeObserver);
} else textInputEditText.setWidth(viewWidth);
}
#Override
public void onPause() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(viewTreeObserver);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(viewTreeObserver);
}
super.onPause();
}
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/id_constraintlayout"
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=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I enclose an image of the result here below, where I already typed 6 digits:
For me the given answers didn't work. But this did:
<com.google.android.material.textfield.TextInputLayout
...
android:maxEms="12"
minEms="12">
<com.google.android.material.textfield.TextInputEditText
...
singleLine="true">
</com.google.android.material.textfield.TextInputLayout>
I am trying to validate the form, i am using TextInputLayout error to show the error, when i click submit button in empty form, the error is shown in only Name field, and the error is not hiding when i fill the name text. Also the other fields not showing validation error.
activity_form.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/nameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/formNameEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_hint_name"
android:inputType="textPersonName"
android:imeOptions="actionNext"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/EmailTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/formEmailEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_hint_email"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/PhoneTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_hint_mobile_number"
android:imeOptions="actionNext"
android:id="#+id/formMobileEdit"
android:inputType="number"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/AlternateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_hint_alternate_number"
android:imeOptions="actionNext"
android:id="#+id/formAlternateEdit"
android:inputType="number"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/JEETextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:id="#+id/formJeeEdit"
android:hint="#string/form_hint_jee"
android:inputType="number"
android:minLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/PercentageTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_hint_percentage"
android:imeOptions="actionNext"
android:id="#+id/formPerEdit"
android:inputType="numberDecimal"
android:minLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/CityTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/form_hint_city"
android:id="#+id/formCityEdit"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/deptSpinner"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</Spinner>
<Spinner
android:id="#+id/SourceSpinner"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
<Button
android:id="#+id/submit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundedbutton"
android:text="#string/form_submit_btn" />
</LinearLayout>
</ScrollView>
FormActivity.java
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Validation();
}
});
private void Validation(){
boolean isValid = true;
String dept = mDepartmentSpinner.getSelectedItem().toString();
String source = mSourceSpinner.getSelectedItem().toString();
String name = NameInputLayout.getEditText().getText().toString();
if(NameInputLayout.getEditText().getText().toString().isEmpty()){
NameInputLayout.setError("Enter Name");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
Log.d("form error", "Error removed");
}
String email = EmailInputLayout.getEditText().getText().toString();
if(EmailInputLayout.getEditText().getText().toString().isEmpty()){
NameInputLayout.setError("Enter Email");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
String phone = PhoneInputLayout.getEditText().getText().toString().trim();
if(phone.isEmpty()){
NameInputLayout.setError("Enter Phone Number");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
String alternate_num = AlternateInputLayout.getEditText().getText().toString();
if(alternate_num.isEmpty()){
NameInputLayout.setError("Enter Alternate Number");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
String jee_marks = JeeInputLayout.getEditText().getText().toString();
if(jee_marks.isEmpty()){
NameInputLayout.setError("Enter Roll Number");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
String percentage = PercentageInputLayout.getEditText().getText().toString();
if(percentage.isEmpty()){
NameInputLayout.setError("Enter Percentage");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
String city = CityInputLayout.getEditText().getText().toString();
if(city.isEmpty()){
NameInputLayout.setError("Enter Name");
isValid = false;
}else {
NameInputLayout.setErrorEnabled(false);
}
if(isValid){
Boolean insert = dbHelper.insertForm(name,email,phone,alternate_num,jee_marks,percentage,city,dept,source);
if(insert == true){
Toast.makeText(getApplicationContext(),"Form Submitted",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(FormActivity.this, MainActivity.class);
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
}
}
here is code snippet to remove error(user text change listener in edittext) you need to call for every edit text and textinput layout.
public static void addTextChangedListener(EditText e, final TextInputLayout t) {
e.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
if (!TextUtils.isEmpty(t.getError())) {
t.setError(null);
t.setErrorEnabled(false);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
and second thing you are validation EmailInputLayout and setting erroer to NameInputLayout in each input field.
-do copy paste but smartly.
It seems you are using the same textInputlayout (NameInputLayout) with all your fields.
Hence after entering the name , when the else block is executed the errorEnabled is set to false , causing other error to not set.
And for removing error in your else case do the following:-
NameInputLayout.setError(null)
instead of :-
NameInputLayout.setErrorEnabled(false);
Otherwise you will have to set setErrorEnabled to true, again to use it.
I am creating the simple login screen having textimputlayout floating labels.
The java file and xml is given below.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_cryptocurrency);
// get the reference of View's
emailTextInputLayout = (TextInputLayout) findViewById(R.id.emailTextInputLayout);
passwordTextInputLayout = (TextInputLayout) findViewById(R.id.passwordTextInputLayout);
email = (EditText) findViewById(R.id.emailEditText);
password = (EditText) findViewById(R.id.passwordEditText);
signIn = (Button) findViewById(R.id.signInButton);
// perform click event on sign In Button
signIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (validate(email, emailTextInputLayout) && validate(password, passwordTextInputLayout)) {
// display a Thank You message
Toast.makeText(getApplicationContext(), "Thank You", Toast.LENGTH_LONG).show();
}
}
});
}
// validate fields
private boolean validate(EditText editText, TextInputLayout textInputLayout) {
if (editText.getText().toString().trim().length() > 0) {
return true;
}
editText.requestFocus(); // set focus on fields
textInputLayout.setError("Please Fill This.!!!"); // set error message
return false;
}
and the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.design="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:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- first TextInputLayout -->
<android.support.design.widget.TextInputLayout
android:id="#+id/emailTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android.support.design:counterMaxLength="3">
<EditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Id" />
</android.support.design.widget.TextInputLayout>
<!-- first TextInputLayout -->
<android.support.design.widget.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<EditText
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<!-- sign In Button -->
<Button
android:id="#+id/signInButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Sign In"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
and the screen shot is
I have searched and tried a lot for edit text visibilty but not finding any proper reason when i give the background to xml file like any color it displays. But in the white background it is not visible . Please guide me for the mentioned issue.
You can change color of floating labels using theme, just add below code in your styles.xml
<style name="TextAppearence.App.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/red</item>
<item name="android:textSize">14sp</item>
</style>
And then apply this theme to you TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/gray"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint" />
</android.support.design.widget.TextInputLayout>
I'm new to Android dev and I'm trying to run a legacy app. Now I'm facing a problem with TextInputLayout. I have the following code:
public class LoginActivity extends AppCompatActivity {
#Bind(R.id.login_input_user)
TextInputLayout login_input_user;
#Bind(R.id.login_input_password)
TextInputLayout login_input_password;
#Bind(R.id.login_bt_enter)
Button login_bt_enter;
#Bind(R.id.login_version)
TextView login_version;
private String username;
private String password;
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mContext = getApplicationContext();
LoginServices.invalidateAccessToken();
ButterKnife.bind(this);
initViews();
}
private void initViews() {
if (BuildConfig.DEBUG) {
username = "test";
password = "test";
if (login_input_user.getEditText() != null)
login_input_user.getEditText().setText(username);
if (login_input_password.getEditText() != null)
login_input_password.getEditText().setText(password);
} else if (login_input_user.getEditText() != null) {
login_input_user.getEditText().setText(LoginServices.getCurrentLogin());
username = LoginServices.getCurrentLogin();
}
new BindValue(login_input_user).setBind(value -> username = value.toString());
new BindValue(login_input_password).setBind(value -> password = value.toString());
login_bt_enter.setOnClickListener(v -> {
Device.hideSoftKeyboard(LoginActivity.this);
if (isValidLogin()) {
if (Device.hasNetwork()) {
doLogin();
} else {
CustomAlert.showAlertMessage(LoginActivity.this, R.string.alerta_error, R.string.message_no_network);
}
} else {
CustomAlert.showAlertMessage(LoginActivity.this, R.string.alerta_error, R.string.login_error);
}
});
login_version.setText(String.format(getString(R.string.login_version_text), BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME));
}
}
This is my activity_login.xml
<?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"
android:background="#color/windowLoginBackground"
android:padding="8dp"
android:theme="#style/AppTheme">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/login_logo"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="48dp"
android:layout_marginTop="48dp"
android:contentDescription="#string/login_logo_contentdescription"
android:scaleType="fitEnd"
android:src="#mipmap/app_logo" />
<android.support.design.widget.TextInputLayout
android:id="#+id/login_input_user"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/login_lb_user"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/login_lb_password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_bt_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#color/colorAccent"
android:text="#string/login_bt_enter"
android:textColor="#color/white" />
</LinearLayout>
<TextView
android:id="#+id/login_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Versão: 1.0.0" />
When I hit initViews and get this line if (login_input_user.getEditText() != null) I get a NullPointerException. Looks like login_input_user is null.
Where can I initiate this?
This code has been already built and distributed some time ago. They used a previous version of Android Studio and I had to upgrade it to 3.0 and it's plugins to necessary versions to run. So this code once worked fine.
Not sure is it helps, but I can't comment questions yet.
Try to check which version of Butterknife you are using.
If you also updated the libs version it may break it.
After 8 version, annotations was changed.
#Bind becomes #BindView and #BindViews (one view and multiple views,
respectively).
https://github.com/JakeWharton/butterknife/blob/master/CHANGELOG.md
Try this
if (login_input_user.getEditText().toString() != null)
OR
if(login_input_user.getEditText().toString().trim().length() == 0)
I am facing a problem with the position of the error indicator of my EditText when calling editText.setError("...").
As you can see in the screenshot I am using a BottomSheetDialog with an EditText inside of it. When I display the error indicator, the text is completely out of place. It seems as if the dialog "thinks" that it is full-screen, while it is actually not.
This is my dialog layout file (phone_dialog.xml):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="#dimen/padding_layout_normal"
android:text="#string/dialog_title_edit_phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTitle"/>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPhone"/>
</android.support.constraint.ConstraintLayout>
My Activity layout file (activity_contacts.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvContacts"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
This is how I show the dialog from my Activity:
PhoneBottomDialog dialog = new PhoneBottomDialog(Context);
dialog.show();
This is my PhoneBottomDialog class:
public class PhoneBottomDialog extends BottomSheetDialog {
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
}
// ...
}
I am not performing any other layouting inside my custom PhoneButtomDialog. Changing the root layout of my dialog to RelativeLayout or LinearLayout as well as adding a ScrollView did not change anything. It's also not a device or specific Android version related issue as the problem occurs on all of my testing devices ranging from Android 5.0 to 7.1, it also occurs on the emulator.
Does anyone have an idea why this is happening?
You can use TextInputLayout and inside that you can define Edit Text.
I have done some modification inside your phone_dialog.xml.
phone_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="#android:color/holo_blue_light"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="10dp"
android:text="dialog_title_edit_phone" />
<android.support.design.widget.TextInputLayout
android:id="#+id/inputPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextInputLayoutLabel">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone"
android:imeOptions="actionDone"
android:inputType="phone"
android:maxLines="1"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etPhone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:text="Save" />
</LinearLayout>
</FrameLayout>
Inside style.xml add this.
<style name="TextInputLayoutLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#android:color/black</item>
<item name="android:textSize">15sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorAccent</item>
</style>
PhoneBottomDialog.java
public class PhoneBottomDialog extends BottomSheetDialog {
TextInputLayout inputPhone;
EditText edtPhone;
Button btnSave;
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
inputPhone = (TextInputLayout) view.findViewById(R.id.inputPhone);
edtPhone = (EditText) view.findViewById(R.id.etPhone);
btnSave = (Button) view.findViewById(R.id.btnSavePhone);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!validatePhone())
return;
}
});
}
private boolean validatePhone() {
if (edtPhone.getText().toString().isEmpty()) {
inputPhone.setError("Please enter valid phone number with country code.");
requestFocus(edtPhone);
return false;
} else {
inputPhone.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
// ...
}
And inside Activity.
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test24);
mContext = this;
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
PhoneBottomDialog dialog = new PhoneBottomDialog(mContext);
dialog.show();
}
Below you can see the output for this.
add compile 'com.android.support:design:24.2.0' in gradle dependencies
use TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="36dp"
app:errorTextAppearance="#style/TextErrorAppearance">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
........./>
</android.support.design.widget.TextInputLayout>
From Lollipop(5.0) version android provide TextInputLayout to do this.
Use below xml and java code to show same type view.
abc.xml:
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:theme="#style/AppTheme">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#color/icons"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
Abc.java:
private TextInputLayout
textInputLayout_User = (TextInputLayout) findViewById(R.id.text_input_layout_user);
textInputLayout_User.setError(getString(R.string.valid_username));