1

I created a TextView with underline. It's working perfectly but I need to know how to increase the height of the underline in the TextView. I checked many but it only shows how to draw underline, but no one here in how to increase size of height in underline.

Is there any way available?

enter image description here

How can I increase the underline height?

Micho
  • 3,929
  • 13
  • 37
  • 40
  • reference this: http://stackoverflow.com/questions/19046614/how-to-underline-text-in-textview-with-some-different-color-than-that-of-text – anil Mar 10 '17 at 06:16
  • no i try this,underline color changed.but not change the height... –  Mar 10 '17 at 06:29

2 Answers2

1

You can create a custom view. It's pretty easy. You can give the underline any thickness you want.

To create a customView:

CustomView.java

package com.rachit.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * Custom View
 * Created by Rachit on 18-Apr-16.
 */
public class CustomView extends View {

    // Label text
    private String viewText;
    // Underline Thickness
    private float underlineThickness;

    // Paint for drawing custom view
    private Paint viewPaint;

    public CustomView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        viewPaint = new Paint();
        // Get the attributes specified in attrs.xml using the name we included
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomView, 0, 0);

        try {
            // Get the text and colors specified using the names in attrs.xml
            viewText = typedArray.getString(R.styleable.CustomView_viewText);
            underlineThickness = typedArray.getDimension(R.styleable.CustomView_underlineThickness, 1f);
        } finally {
            typedArray.recycle();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Draw the View

        // Drawing the text on the view
        viewPaint.setColor(Color.BLACK); // Set Text color to whatever you want
        viewPaint.setTextSize(50); // Set Text Size to whatever you want
        canvas.drawText(viewText, getX() + 5, getMeasuredHeight() / 2 + 20, viewPaint); // 5 and 20 are the left and top padding, you can customize that too.

        viewPaint.setColor(Color.BLACK); // Set Underline color to whatever you want
        canvas.drawRect(getX(), getMeasuredHeight() - underlineThickness, getX() + getMeasuredWidth(), getMeasuredHeight(),
                viewPaint); // Set the start and end points of the Underline

    }

    public String getViewText() {
        return viewText;
    }

    public void setViewText(String viewText) {
        this.viewText = viewText;
        invalidate();
        requestLayout();
    }

    public float getUnderlineThickness() {
        return underlineThickness;
    }

    public void setUnderlineThickness(float underlineThickness) {
        this.underlineThickness = underlineThickness;
        invalidate();
        requestLayout();
    }
}

attrs.xml in the res/values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">

        <attr name="viewText" format="string" />
        <attr name="underlineThickness" format="dimension" />

    </declare-styleable>
</resources>

Now you can define your view in the XML like this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rachit.customview.MainActivity">

    <Button
        android:id="@+id/change"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Change View" />

    <com.rachit.customview.CustomView
        android:id="@+id/customView"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginTop="30dp"
        custom:underlineThickness="5dp"
        custom:viewText="My View" />
</LinearLayout>

And you can even control the View programmatically:

MainActivity.java

package com.rachit.customview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    CustomView customView;
    Button change;

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

        customView = (CustomView) findViewById(R.id.customView);
        change = (Button) findViewById(R.id.change);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customView.setViewText("Wassup??");
                customView.setUnderlineThickness(1.0f);
            }
        });
    }
}

The output looks like this:

Custom View

And on clicking the button, the view can be modified programmatically:

Programmatically Modified

Rachit
  • 3,173
  • 3
  • 28
  • 45
  • it working.but i face another issue.use this method the underline will whole textview width.but i want to set underline in below text only.not extra space?is there any way here? –  Mar 10 '17 at 06:52
0

Another way is that you can add View under TextView in XML

Try this

<View
    android:layout_width="match_parent" // same as textview width
    android:layout_height="1dp"  // height that you want
    android:background="@color/black"/>
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Mehul Gajjar
  • 431
  • 5
  • 20
  • i also try this view method.but i have another problem.in portrait to lanscape the line width incresed on actual textview text size.... –  Mar 10 '17 at 05:33
  • then you can design separate layout for each – Mehul Gajjar Mar 10 '17 at 05:36
  • yes.i know that if create separate layout for each it increasing app size for adding more views like different layouts –  Mar 10 '17 at 05:38
  • but textview underline method support actual textview width on both portrait & landscape.so i need to know how to increase a underline height?. –  Mar 10 '17 at 05:41