Tutorials  /  JavaScript

Mastering Material Design Text Fields in Android Apps

Ccentron Redaktion · September 2024 ·3 min read ·JavaScript, Tutorial

Explore the world of Android Material Text Fields in our latest blog post! Dive into the variety of design options for text fields in your apps and learn how to create an impressive user experience with the Material Design Components Library. From standard and dense text fields to shaped and contoured variants - optimize your UI with our expert tips!

Material TextFields and TextInputLayout

The TextInputLayout class provides an implementation for Material text fields. For this, we simply use the TextInputEditText. First, we need to import the new Material components dependency and set the MaterialComponent theme in our activity.

Code
implementation 'com.google.android.material:material:1.1.0-alpha09'

An input text field by default has a filled background to attract the user's attention. A simple text field can be created as follows:

Code
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Filled Field (Default)">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Standard and Dense Text Fields

Text fields can have two height variants: standard and dense. The dense text field is slightly shorter in height.

Code
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Filled Field (Standard)">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Filled Field (Dense)"
    app:boxBackgroundColor="#20D81B60">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

Outlined Text Fields

To achieve the outlined look for text fields, apply the following style to the TextInputLayout:

Code

Similar to filled fields, there are two height variants here: standard and dense.

End Icon Modes

End icons are symbols that can be placed to the right of the text field. Three types of built-in icons are available: password_toggle, clear_text, and custom.

Code
<com.google.android.material.textfield.TextInputLayout
   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Enter Password"
    app:endIconMode="password_toggle">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

Shaped Text Fields

With ShapeAppearance, we can customize the shape of the text field. There are two built-in shapes: 'cut' and 'round'.

Code
<style name="Cut" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">12dp</item>
</style>

<style name="Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>

Conclusion

The Material Design Components Library offers a variety of options for customizing text fields in Android apps. From filled and outlined fields to custom end icons and shaped text fields, developers have numerous options to design an appealing user interface.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren