Skip to content Skip to sidebar Skip to footer

Increase Textview Underline Height In Android

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

Solution 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.
 */publicclassCustomViewextendsView {

    // Label textprivateString viewText;
    // Underline Thicknessprivate float underlineThickness;

    // Paint for drawing custom viewprivatePaint viewPaint;

    publicCustomView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        viewPaint = newPaint();
        // Get the attributes specified in attrs.xml using the name we includedTypedArray 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();
        }

    }

    @OverrideprotectedvoidonDraw(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

    }

    publicStringgetViewText() {
        return viewText;
    }

    publicvoidsetViewText(String viewText) {
        this.viewText = viewText;
        invalidate();
        requestLayout();
    }

    public float getUnderlineThickness() {
        return underlineThickness;
    }

    publicvoidsetUnderlineThickness(float underlineThickness) {
        this.underlineThickness = underlineThickness;
        invalidate();
        requestLayout();
    }
}

attrs.xml in the res/values folder

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleablename="CustomView"><attrname="viewText"format="string" /><attrname="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"?><LinearLayoutxmlns: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"><Buttonandroid:id="@+id/change"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Change View" /><com.rachit.customview.CustomViewandroid: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;

publicclassMainActivityextendsAppCompatActivity {

    CustomView customView;
    Button change;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        change.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(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

Solution 2:

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"/>

Post a Comment for "Increase Textview Underline Height In Android"