I cannot seem to be able to use TextInputLayout in fragment. Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginFragment">
<android.support.design.widget.TextInputLayout
android:id="#+id/email_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:inputType="textEmailAddress"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
When I try to run I get this exception
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TextInputLayout"
I red this post which says I should use AppCompat Theme and I am using AppCompat Theme
Here is my Theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Adding this dependency to gradle solved the problem
implementation 'com.android.support:design:27.1.1'
Related
I have this error when i start Activity with Layout in Xamarin.Android project. How to fix this?
All android nuget packages v. 28.0.0.3
Android.Views.InflateException: 'Binary XML file line #1: Binary XML file line #1: Error inflating class android.suppot.design.widget.NavigationView'
XAML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/drawerLayout"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
layout="#layout/include_main"
/>
</LinearLayout>
<android.suppot.design.widget.NavigationView
android:id="#+id/navView"
android:layout_height="match_parent"
android:layout_width="300dp"
android:background="#android:color/white"
android:layout_gravity="start"
app:headerLayout="#layout/headerlayout"
app:menu="#menu/navmenu"
/>
</android.support.v4.widget.DrawerLayout>
NavMenu
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="#+id/navProfile"
android:title="Профиль"
android:icon="#mipmap/ic_person"/>
<item android:id="#+id/navPayments"
android:icon="#mipmap/ic_wallet"
android:title="Транзакции"/>
<item android:id="#+id/navHistory"
android:title="История"
android:icon="#mipmap/ic_history"
/>
<item
android:id="#+id/navPromo"
android:title="Промо"
android:icon="#mipmap/ic_discount"
/>
<item
android:id="#+id/navSupport"
android:title="Поддержка"
android:icon="#mipmap/ic_support"
/>
<item
android:id="#+id/navAbout"
android:title="О нас"
android:icon="#mipmap/ic_about"
/>
</group>
</menu>
Include_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Layout"
android:textSize="20sp"
android:gravity="center"
android:layout_gravity="center"
/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
header_layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/white"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<Refractored.Controls.CircleImageView
android:id="#+id/accoutImage"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_gravity="center"
android:src="#drawable/account"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/accountTitle"
android:layout_gravity="bottom"
android:gravity="center"
android:text="UserName"
android:layout_marginBottom="12dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:background="#android:color/holo_blue_dark"/>
</FrameLayout>
styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="Theme.Splash"
parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">#drawable/splash</item>
</style>
<style name="Theme.Register" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
</style>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette ->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/colorPrimary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/colorAccent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">#style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
You have misspelled the word support. Just try to replace
<android.suppot.design.widget.NavigationView>
with
<android.support.design.widget.NavigationView>
Besides, make sure the nuget:Refractored.Controls.CircleImageView have been installed, otherwise the code will report the error as well.
Note:
I have tested that It doesn't matter if the namespace is Refractored.Controls.CircleImageView or refractored.controls.CircleImageView.They all works.
try to change refractored.controls.CircleImageView instead of Refractored.Controls.CircleImageView in your header_layout :
<refractored.controls.CircleImageView
android:id="#+id/accoutImage"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_gravity="center"
android:src="#drawable/account"
android:scaleType="centerCrop"
/>
and :
<android.support.design.widget.NavigationView>
instead of:
<android.suppot.design.widget.NavigationView>
I've been experimenting with the new Material library so far, and noticed that the long press menu for TextInputEditText shows cut, copy, paste and other options as material buttons within the menu. I haven't done any custom code and the long press menu is system default. What's happening?
I've implemented 'com.google.android.material:material:1.0.0-alpha3' and all libraries are androidx. Testing on SDK23.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewmodel"
type="io.jayasurya.ruby.MainViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".AddFragment">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:hint="Name"
android:text="#={viewmodel.name}"
android:id="#+id/nameInput" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:id="#+id/btn"
android:text="#{viewmodel.name}" />
</LinearLayout>
</layout>
Styles
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
It is caused by material design view inflater, and Google has fixed it.
TextInputLayout working fine, when tried to set Error it showing error.
Code:
if (etFirstName.getText().length() == 0) {
etFirstName.requestFocus();
tvFirstName.setError("Please enter firstname");
} else {
tvFirstName.setError(null);
tvFirstName.setErrorEnabled(false);
}
Check Log:
java.lang.RuntimeException: Failed to resolve attribute at index 24
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:692)
at android.widget.TextView.<init>(TextView.java:629)
at android.widget.TextView.<init>(TextView.java:625)
at android.widget.TextView.<init>(TextView.java:621)
at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:297)
at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:344)
Although I did not fully understand why the problem occurs, it must have to do something about how Android applies colors to Views according to its states.
Here is the way I solved the problem:
1) Define custom style, that will be used only for styling the errors (or hints) only:
In your res/values/styles.xml add new style node:
<style name="error" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/colorAccent</item> <!--apply the color you wat here -->
<item name="android:textSize">12dp</item>
</style>
2) Apply the style to your TextInputLayout:
Also, make sure, that you specify errorEnabled attribute:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_email_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:errorEnabled="true"
app:errorTextAppearance="#style/error"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/hint_email"/>
</android.support.design.widget.TextInputLayout>
Give valid style for your TextInputLayout(
android:theme="#style/Theme.AppCompat")
<android.support.design.widget.TextInputLayout
android:id="#+id/testingInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppCompat">
<EditText
android:id="#+id/testingEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/testText"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
You need to add appCompat & support design dependency (if not)
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
In your manifest file add AppCompat theme,
<application
...
android:theme="#style/Theme.AppCompat">
Sytle theme for textinputlayout :
<style name="HintInputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#color/input_floating_color</item>
<item name="android:textSize">22dp</item>
<item name="colorAccent">#color/input_floating_color</item>
</style>
<style name="InputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ff000000</item>
<item name="android:textColorHint">#ff000000</item>
<item name="android:textSize">24dp</item>
<item name="colorAccent">#ff000000</item>
</style>
<style name="ErrorTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ffff0000</item>
<item name="android:textColorHint">#ffff0000</item>
<item name="android:textSize">18sp</item>
<item name="colorAccent">#ffff0000</item>
</style>
Use in Layout as :
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="#+id/name_signup"
app:errorEnabled="true"
app:errorTextAppearance="#style/ErrorTextAppearance"
app:hintTextAppearance="#style/HintInputTextAppearance"
android:layout_marginLeft="#dimen/left_margin_login"
android:layout_marginRight="#dimen/right_margin_login"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:textAppearance="#style/InputTextAppearance"
android:layout_height="wrap_content"
android:hint="Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="#+id/email_signup"
app:errorEnabled="true"
app:errorTextAppearance="#style/ErrorTextAppearance"
app:hintTextAppearance="#style/HintInputTextAppearance"
android:layout_marginLeft="#dimen/left_margin_login"
android:layout_marginRight="#dimen/right_margin_login"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:textAppearance="#style/InputTextAppearance"
android:layout_height="wrap_content"
android:hint="Email" />
</android.support.design.widget.TextInputLayout>
Solution by #Sevastyan works great for using xml layout.
However, if i have a TextInputLayout and a TextInputEditText which i created programmatically, and i did this:
tilCode.setErrorEnabled(true);
tilCode.setErrorTextAppearance(R.style.error);
I will get an error. But just:
tilCode.setErrorTextAppearance(R.style.error);
Works for me.
Use this it never need Theme.AppCompat
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tv_metadata_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tv_metadata_Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Use TextUtils
if(TextUtils.isEmpty(etFirstName.getText().toString()))
{
etFirstName.setError("Filed is mandatory!");
view = etFirstName;
}
Try below code to show error in TextInputLayout:
tvFirstName.setErrorEnabled(true);
tvFirstName.setError("Please Enter firstName");
I have a SwitchCompat in the action bar (v7.Toolbar) of my activity. I want it be colorStructure when off and colorAccent when on. It works fine on pre-Lollipop, but on Lollipop the activated switch is drawn in a color which I did not define anywhere. It looks like "material_deep_teal_200". Thats my only style definitions:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Default" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!--<item name="android:windowNoTitle">true</item>-->
<item name="windowActionBar">false</item>
<item name="colorSwitchThumbNormal">#color/colorStructure</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<!--<item name="selectBarStyle">#style/DefaultSelectBar</item>-->
</style>
</resources>
And that's the begin of the activity xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:theme="#style/Widget.AppCompat.Toolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="#color/colorPrimary"
android:paddingRight="#dimen/abc_dropdownitem_text_padding_right">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_icon_vertical_padding_material">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/helpButton"
android:src="#drawable/ic_help_large"
android:background="#00000000"
android:layout_gravity="center"/>
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/master_switch"
android:clickable="true"
android:checked="true"
android:gravity="center"
android:layout_gravity="right"
/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
So what do I need to change to make the switch have the same custom colors on KitKat and Lollipop?
Thanks
Actually the code as presented above works fine - also on Lollipop. I must have done a mistake during validation after cleaning up the problematic code.
Originally the problem was when I also had a configuration in values-v21. I assume that there was a typo or something like that which got lost when I was simplifying all into the default xml file.
So in short, the code as presented works, and I assume that having configurations in values-v21 would not cause a problem either.
I want to build a theme for my app. But it seems like my LiestView item don't apply the values.
This is the styles.xml:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:textViewStyle">#style/TextView</item>
</style>
<style name="TextView" parent="android:Widget.TextView">
<item name="android:layout_margin">8dp</item>
<item name="android:padding">0dp</item>
<item name="android:textColor">#87000000</item>
<item name="android:textSize">18sp</item>
</style>
in my activity i use a Listview with Custom Adapter but the TextViews from my item.xml did'nt have a margin:
<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="wrap_content"
android:clickable="true"
android:orientation="vertical"
tools:context="de.resper.enigma2chromecast.mainLive" >
<TextView
android:id="#+id/channelName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/hello_world" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:id="#+id/channelLine1"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
</LinearLayout>
what ever you designed xml, put in android manifest file in application theme
<application android:theme="#style/CustomTheme">