0

How can I make my text view in android to look like the one in image below

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
kcee
  • 1
  • 1
  • 1

3 Answers3

1

Use a TextInputLayout

        <com.google.android.material.textfield.TextInputLayout
            app:boxBackgroundColor="@color/white"
            app:placeholderText="Name"
            app:hintEnabled="false">

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

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

enter image description here

Otherwise you can apply a MaterialShapeDrawable to your EditText (or TextView).
Define in your layout:

        <EditText
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:id="@+id/edittext"
            android:paddingStart="4dp"
            android:hint="Name"/>

Then:

    val radius = resources.getDimension(R.dimen.cornerSize8)

    val textView: EditText = findViewById(R.id.edittext)
    val shapeAppearanceModel = ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build()

    val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
    shapeDrawable.fillColor = AppCompatResources.getColorStateList(this,R.color.white)
    ViewCompat.setBackground(textView, shapeDrawable)

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

It's quite easy, you just need to set background drawable

textview_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="8dp" />
    <solid android:color="@color/white" />
</shape>

TextView:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/textview_background"
    android:gravity="center_vertical"
    android:padding="8dp" />
Mariusz Brona
  • 1,549
  • 10
  • 12
0

Simply Create a EditText in your Layout or a TextView with inputtype="Whatever you want"

like this

<EditText
     android:id="@+id/plain_text_input"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="text"
     android:text="First Name"
     android:background="@drawable/textbackground"
/>

and in drawable create textbackground.xml

drawable/textbackground.xml --

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
       <shape android:shape="rectangle">
           <solid android:color="#FFFFFF"/>
           <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

Hope it Works