I am using EditText with TextInputLayout. This is the code, that I am using to display error.
private boolean validateEmail() {
String email = inputEmail.getText().toString().trim();
if (email.isEmpty() || !isValidEmail(email)) {
inputLayoutEmail.setErrorEnabled(true);
inputLayoutEmail.setError(getString(R.string.err_msg_email));
requestFocus(inputEmail);
return false;
} else {
inputLayoutEmail.setErrorEnabled(false);
}
return true;
}
I am calling this method in edittext's textwatcher like in this link http://www.androidhive.info/2015/09/android-material-design-floating-labels-for-edittext/
Once I entered a valid input then clear that ,it will show error message as expected,But it wont work if i enter the text again and then clear it again.ie.It is not showing any error message.
I am using compile 'com.android.support:design:23.1.0' library.
inputLayoutEmail.setErrorEnabled(true);
is calling but error is not displaying. What might be the problem? How I can solve this?
In your layout file, ensure you have layout_height="wrap_content" for the TextInputLayout
instead of a dimension. This was causing the issue for me.
You just need apply,
inputLayoutEmail.setErrorEnabled(false);
inputLayoutEmail.setError(null);
It worked for me. Hope it will work for you too.
The example worked for me.
you use
compile 'com.android.support:design:23.1.0'
and the right one is
compile 'com.android.support:design:23.0.1'
I was having same issue and i was using data binding so adding below line solved my issue :
app:errorEnabled="true"
Use Android Support Library, revision 23.4.0 (May 2016)
Fixed an issue where TextInputLayout doesn't clear error tint after setErrorEnabled(false) on API level 21 - 22 (Issue 202829)
Related
Is is possible to set error on Text Input Layout without showing any error message (i'm already doing it in another place)?.
textinputlayout.setError("");
won't work unfortunately.
What i basically need is the textInputLayout to change its line color to red, but i need to do it programmatically. Thank you
You can hide the layout with error like this:
textinputlayout.setError("");
if (textinputlayout.getChildCount() == 2) {
textinputlayout.getChildAt(1).setVisibility(View.GONE);
}
Hope it's not too late, but the code behind setError is:
if (!mErrorEnabled) {
if (TextUtils.isEmpty(error)) {
// If error isn't enabled, and the error is empty, just return
return;
}
}
this means a simple workaround would be:
textinputlayout.setError(" ");
please be advised, that this will make TextView with error message visible - so it will make TextInputLayout higher even if it will be empty
since this passes the not-so-well-thought-out case of handling an empty error message request.
The error text (even " ") will take/reserve some height in the TextInputLayout. If you also want to get rid of it then
<com.google.android.material.textfield.TextInputLayout
...
app:errorTextAppearance="#style/MyZeroSizeTextAppearance"
...
>
Text appearance:
<style name="MyZeroSizeTextAppearance">
<item name="android:textSize">0sp</item>
</style>
and then
textinputlayout.setError(" ");
I know there are several similar questions regarding this has been asked here, however none of the solutions worked for me.
I have just transferred my application from Ecilpse(juno) to Android Studio 1.5.1; and from API 19 to API 23(compileSdkVersion).
Currently I encountered this error whereby "getResources().getColor(R.color.my_color)" is depreciated and cannot be used.
After searching online, I tried to use "ContextCompat.getColor(context, R.color.my_color)" instead. The error lies at the "getColor" part as it says that it cannot be resolved:
The method "ContextCompat" however allow me to use "getDrawable", "getExternalCacheDirs", "getExternalFilesDirs" and "getObbDirs"; just except for "getColor"
I have also ensure that I have these 2 in my app build.gradle (under dependencies{}):
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
Below are my imports for this class (DetailsActivity):
Additionally, I have also tried using "ResourcesCompat" other than "ContextCompat"; and it still did not work out for me.
I am still a beginner at Android Development, working on a school project.
Can anyone kindly provide some suggestions as of what I am doing wrong and point me towards the right direction? Thanks a lot in advance!
You can use this method.
This is the ContextCompat Developer Link from the Support Library:
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompat.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}
You don't have to use ContextCompat there if you are compiling with an API level greater than 24 as this is only a compatibility feature for early versions. You can also still use Context.getColor() But notice the method signature! If you just want a color in the default theme you only have to provide the int value of its reference: http://developer.android.com/reference/android/content/Context.html#getColor(int)
You are currently trying to access getColor() with two parameters (Color and Theme) which cannot work there as there is no method who supports these parameters. If you remove the 2nd parameter your current solution will work.
But it would be more convenient to use Context or Resources directly and drop the v4 Appcompat reference if you don't need it.
Before to use this method, fist of all add the Dependencies in build.gradle script. Which is as follows:
dependencies {
// other stuff here
compile 'com.android.support:support-v4:23.0.0'
}
Slowly, I believe I am simply to dump to get this to work and hope
that somebody of you, can help me.
I am using the Android Design Support Library, AppCompat and support libarties:
compile "com.android.support:design:23.0.1"
compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
All I wanted was a simple Toolbar, Tablayout and a ViewPager getting to work.
Like you can see in every other app, too.
But I am getting an annoying FloatingMenu on every activity that uses the
app_theme.
How do I remove this FloatingMenu?
Cheers!
Douplicates:
Remove Floating menu button from HTC one [noanswer]
Lot of people are facing this problem and its just not related to your code but to what kind of updates vendors have pushed.Try to confirm it by running your app on other devices too with different lollypop version.
Create a mainmenu.xml file with just one item (R.id.action_settings)
and try to inflate it within your code and set the visibility to false afterwards.
That will might solve the issue otherwise go into phone settings and button and then try to find out if there is any option to help you. It should be related to navigation or something i suppose.
Edit
Setting your sdk version 14 or above seems to solve the issue. We went through many hit and trial and this one seems to work. I believe its a sort of bug in the design library.
It's seems like HTC issue try to override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
settingsItem.setVisible(false);
return true;
}
While using Android Studio just now I was editing an XML file in the editor and I got this error in the Preview and Design windows:
Exception raised during rendering: Unable to find the layout for Action Bar.
I've tried restarting Android Studio, my Laptop and Googling for the answer but I can't find anything. Has anybody experienced anything similar?
I had this kind of error. On My Mac, there is API 22. If I choose it this error will appear. So clicking on API 21 or below would solve your problem
You probably need to update your tools
Those using Eclipse might try to choose Theme.DeviceDefault to eliminate the exception.
Update: as per comments this works for Android Studio also.
You can solve this problem by two ways
Change API from the tab, Try one of them from the available or Select PICK UP BEST or change your minimum sdk
Change the Theme if first solution not work
Click on Theme in Editor and change the theme:
MY issue is solved after this
I had this error on eclipse-3.8, adt-23.07, sdk-tools-23.1, appcompat_v7_r19.1.
After analyzing the error I found the problem is in
android-sdk-linux/platforms/android-23/data/layoutlib.jar:com/android/layoutlib/bridge/impl/Layout.class on method createActionBar at line if (this.mBuilder.isThemeAppCompat())
My solution:
(don't have the source, so) decompile Layout.class -> Layout.java
edit Layout.java, add method:
private boolean hasWindowDecorActionBar(SessionParams params) {
LayoutlibCallback callback = params.getLayoutlibCallback();
try {
callback.findClass("android.support.v7.app.WindowDecorActionBar");
} catch (ClassNotFoundException ei) {
try {
callback.findClass("android.support.v7.internal.app.WindowDecorActionBar");
} catch (ClassNotFoundException e) {
return false;
}
}
return true;
}
Locate the line if (this.mBuilder.isThemeAppCompat()) then change to :
if (this.mBuilder.isThemeAppCompat() && hasWindowDecorActionBar(params))
recompile Layout.java -> Layout.class and Layout$Builder.class
update layoutlib.jar with the new classes
restart eclipse
That's all. Now, I can render using API-23 all of my old app layouts that links against appcompat_v7_r19.1.
You suppose to do clean project.
i think it is a bug and there is no ultimate solution until this moment,
changing to API less than API-22 is a temp solution not more.
I'm trying to type some text inside an EditText:
public void testSearch() {
onView(withId(R.id.titleInput)).perform(typeText("Engineer"));
onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
}
I see the EditText is getting focus, but nothing happens. No text is typed.
I tried this on a phone and an emulator - same result.
Looks like I figured out the issue. It had to do with hardware vs software keyboard.
For Emulators:
Go to Settings -> Language & Input -> switch the Default Input to Sample Soft Keyboard.
For Phones:
Install a software keyboard from the Play store and switch to it. It appears that the native keyboards of some phones do not work.
It works now.
Had the same issue using Espresso 2. As a workaround I'm using replaceText instead of typeText.
public void testSearch() {
onView(withId(R.id.titleInput)).perform(click(), replaceText("Engineer"));
onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
}
If the EditText does not has the focus yet, you should click on it first. If this solves your problem, then there is no bug.
onView(withId(R.id.titleInput)).perform(click()).perform(typeText("Engineer"));
You can bypass the problem by calling setText on the EditText.
final EditText titleInput = (EditText) activity.findViewById(R.id.titleInput);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
titleInput.setText("Engineer");
}
});
Same issue resolved with the following:
editText.perform(scrollTo(), click(), clearText(), typeText(myInput), closeSoftKeyboard())
Interestingly, I only ever had a problem when my machine was working hard.
You can include it with in the code like this,
onView(withId(R.id.titleInput))
.perform(click(), replaceText("Engineer"), closeSoftKeyboard());
I fixed this issue by setting layout_height="wrap_content" on the View I wanted to click(). Maybe it can help someone here.
If you're using Genymotion, you may need to switch the default keyboard in Genymotion Configuration (it's an app on the emulator).
Go to Apps -> Genymotion Configuration -> Keyboard -> Virtual keyboard (click "Yes" when you're prompted to reboot)
NOTE: These changes do not persist after you close the emulator. You will need to set this every time you start the emulator.
for me I was annotated my test method with #UiThreadTest. I removed that and it solved.
Adding closeSoftKeyboard() after typeText() worked for me.
CODE:
onView(withId(R.id.editTextUserInput))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
This is how it is documented in the Android docs.