Home » Android » android – How to show * symbol againist the mandatory fields in a Activity

android – How to show * symbol againist the mandatory fields in a Activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a requirement to display * symbol against the mandatory fields in a Activity. Can you please suggest how can I accomplish that. Any help would be appreciated.

How to&Answers:

I’d say that * as a marker for a required field does not follow native Android theme. A combination of setHint and setError would look more natural for an Android app.

Answer:

Just take a Relative Layout and on the Right side of the EditText set the ImageView with * image source like this,

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <EditText android:id="@+id/edit_text" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="5dip"
  android:text="Hello World"
  />

  <ImageView android:layout_toRightOf="@id/edit_text" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignTop="@id/edit_text"
  android:src="@drawable/star"/>

</RelativeLayout>

Output:

enter image description here

Answer:

The following should work:

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="verdana"
    android:text="FirstName"
    android:textColor="#000"
    android:drawableRight="@drawable/star" />

<EditText
    android:id="@+id/editTextFirstName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:hint="FirstName" />

Answer:

Why not to try something like below

txt.setText(Html.fromHtml("mm<sup>2</sup>"));