Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to make TextView text bold when pressed or focussed

I have a text view in my layout. My requirement is when I press or focus it, the text should be bold. Otherwise it should be with the normal font. How can I implement it?

like image 867
Anju Avatar asked Feb 14 '11 04:02

Anju


People also ask

How do I make TextView bold in specific text?

TextView with textSize=”32sp” and textStyle=”bold”. Right: Multi style text. Text with ForegroundColorSpan , StyleSpan(ITALIC), ScaleXSpan(1.5f), StrikethroughSpan. HTML tags are easy solutions for simple problems, like making a text bold, italic, or even displaying bullet points.

How do I make text bold in TextView Android programmatically?

Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .

How do I make text bold on Android?

Double tap the text you want to format. Tap Format, then choose a formatting option like bolding, italics, or changing the font color.


2 Answers

Use below code

TextView name=((TextView)findViewById(R.id.TextView01));


name.hasFocus();
        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus)
                    ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD);
                else
                    ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT);

            }
        });

        name.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD);
                return false;

} });

like image 121
Pinki Avatar answered Sep 22 '22 17:09

Pinki


I actually created a small class to make this a lot easier: ClickTextView. You can copy-paste it into your project and Android Studio should fix it.

public class ClickTextView extends android.support.v7.widget.AppCompatTextView {

    Rect rect;

    public ClickTextView(Context context) {
        super(context);
        init();
    }

    public ClickTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClickTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setClickable(true);
        rect = new Rect();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_HOVER_ENTER){
            this.setTypeface(Typeface.DEFAULT_BOLD);
            getHitRect(rect);
        }else if (action == MotionEvent.ACTION_MOVE){
            boolean inView = rect.contains(getLeft() + (int) event.getX(), getTop() + (int) event.getY());
            this.setTypeface(inView ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
        }else if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL
                || action == MotionEvent.ACTION_HOVER_EXIT
                || action == MotionEvent.ACTION_OUTSIDE) {
            this.setTypeface(Typeface.DEFAULT);
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        if (focused) this.setTypeface(Typeface.DEFAULT_BOLD);
        else this.setTypeface(Typeface.DEFAULT);
    }
}

It also provides a solution for the case that a user clicks your view, but drags his finger outside of the view bounds. If released there, Android will not register a click, so the text shouldn't be bold either.

Feel free to use it wherever you like.

like image 41
mDroidd Avatar answered Sep 24 '22 17:09

mDroidd