How to use TextInputLayout with Theme.material?

I am trying to use TextInputLayout with Theme.material and the code looks like ...

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:drawableLeft="http://schemas.android.com/apk/res-auto"
    android:background="#2196F3"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    tools:context="com.realmilk.MainActivity">

    <TextView
        android:id="@+id/login_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="22dp"
        android:gravity="center_horizontal"
        android:text="Account Login"
        android:textColor="#fff"
        android:textSize="26sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/login_title"
        android:layout_marginTop="70dp"
        android:background="#fff"
        android:elevation="4dp"
        android:orientation="vertical"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="30dp">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/ic_supervisor_account_black_24dp"
                    android:drawableTint="#FF4081"
                    android:singleLine="true"
                    android:hint="User Name"
                    android:inputType="textEmailAddress" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:singleLine="true"
                    android:drawableLeft="@drawable/ic_lock_black_24dp"
                    android:drawableTint="#FF4081"
                    android:hint="Password"
                    android:inputType="numberPassword" />
            </android.support.design.widget.TextInputLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:paddingTop="5dp"
                android:text="Forgot Password?" />


            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="22dp"
                android:background="#d67601"
                android:text="Sign in"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>

    <ImageButton
        android:id="@+id/user_profile_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/login_title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:background="@drawable/user_profile_image_background"
        android:elevation="4dp" />

</RelativeLayout>

MainActivity looks like this.

public class MainActivity extends AppCompatActivity {

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

When I try to execute this

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.                                                                 
           at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
           at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
           at com.realmilk.MainActivity.onCreate(MainActivity.java:11)
           at android.app.Activity.performCreate(Activity.java:6237)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476at android.app.ActivityThread.-wrap11(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344at android.os.Handler.dispatchMessage(Handler.java:102at android.os.Looper.loop(Looper.java:148at android.app.ActivityThread.main(ActivityThread.java:5417)

It was on the remote I was watching.

Can someone help me find where I was wrong.

+4
source share
2 answers

change the following files

style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>

        </style>
</resources>

colors.xml

<resources>
<color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#1a237e</color>
    <color name="transparent">@android:color/transparent</color>
    <color name="colorAccent">#FF4081</color>
    <color name="windowBackground">#313131</color>

</resources>

You can change colors according to your needs.

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    //your activities

</application>

hope this helps ...

0
source

TextInputLayout support ( , ). , . Theme.AppCompat. , Android, , Material .

: Theme.AppCompat, Android >= 5.0

:

<style name="AppTheme" parent="Theme.AppCompat">

.

0

Source: https://habr.com/ru/post/1654335/


All Articles