0

I am making an android application and I was wondering how to achieve a coloured heading with an underline in android. The image below is what I want.

enter image description here

The code I have written so far:

<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="match_parent"
    android:orientation="vertical"
    tools:context=".AddContact" >

    <TextView
        android:id="@+id/lbl_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/heading_name" />

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="6" >

        <EditText
            android:id="@+id/edit_first_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="5"
            android:hint="@string/hint_firstname"
            android:inputType="textCapSentences" />

        <ImageButton
            android:id="@+id/addimage"
            android:layout_width="0dp"
            android:layout_height="58dp"
            android:layout_weight="1"
            android:background="@drawable/ic_social_person" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="5" >

        <EditText
            android:id="@+id/edit_last_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="4"
            android:hint="@string/hint_lastname"
            android:inputType="textCapSentences" />
    </LinearLayout>

    <TextView
        android:id="@+id/lbl_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading_number" />

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <EditText
            android:id="@+id/edit_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="2"
            android:hint="@string/hint_number"
            android:inputType="number" />

        <Spinner
            android:id="@+id/contact_number_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:id="@+id/lbl_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading_email" />

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <EditText
            android:id="@+id/edit_email"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="2"
            android:hint="@string/hint_email"
            android:inputType="textEmailAddress" />

        <Spinner
            android:id="@+id/contact_email_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:id="@+id/lbl_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading_address" />

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <EditText
            android:id="@+id/edit_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="2"
            android:hint="@string/hint_address"
            android:inputType="textPostalAddress" />

        <Spinner
            android:id="@+id/contact_address_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:id="@+id/lbl_event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading_event" />

    <LinearLayout
        android:layout_width="337dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <EditText
            android:id="@+id/edit_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="2"
            android:hint="@string/hint_date"
            android:inputType="date" />

        <Spinner
            android:id="@+id/contact_date_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Any help would be appreciated

user123456
  • 2,287
  • 3
  • 13
  • 10

4 Answers4

1

You can achieve a line with a simple colored View like this:

  <View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="1dip"
    android:background="your color" />
silvia_aut
  • 1,481
  • 6
  • 19
  • 33
0

For Underline text In your string resource file add tags to text like this..

<resource>
 <string name="my_text"><u>This is an underline</u>.</string> 

And for changing text color in your textview add this tag set your colour..

android:textColor="@android:color/holo_orange_light"
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

try this :

TextView textView = (TextView)findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
textview.settextcolor(Color.Red);

this will make help you.

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

You can do this bu=y using String resources. It supports HTML tags like<b></b>, <i></i> and <u></u>. If you want underline the text use this

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

Otherwise,

In your resource folder use this code

<resource>
<string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>

and in your code use this.

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.id.textview), ...)));
Ameer
  • 2,709
  • 1
  • 28
  • 44