1

I am making a Note Making App which has a feature of adding Bold text via a button. When the bold button is clicked the bold mode should be turned on i.e the text taken from user in EditText should now be Bold. On the next Click on the bold button the bold mode should be turned off i.e text taken from the user should now be of Normal type.

I saw this post. Change future text to bold in EditText in Android But the answer in that post did not work out. When I first click the button it takes bold input but when I clicked the button again to take Normal text as input it was still taking bold text as input.

Here is the code: TextArea.java

package com.example.syed.andtexteditor;


import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatEditText;
import android.text.Spannable;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.widget.EditText;


public class TextArea extends AppCompatEditText {

    public static final int TYPEFACE_NORMAL = 0;
    public static final int TYPEFACE_BOLD = 1;
    public static final int TYPEFACE_ITALICS = 2;
    public static final int TYPEFACE_BOLD_ITALICS = 3;

    private int currentTypeface;
    private int lastCursorPosition;
    private int tId;


    public TextArea(Context context) {
        super(context);
        lastCursorPosition = this.getSelectionStart();
    }

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


    public int gettId() {
        return tId;
    }

    public void settId(int tId) {
        this.tId = tId;
    }

    public void changeTypeface(int tfId) {
        currentTypeface = tfId;
        lastCursorPosition = this.getSelectionStart();
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        Spannable str = this.getText();
        StyleSpan ss;
        int endLength = text.toString().length();

        switch(currentTypeface) {
            case TYPEFACE_NORMAL:
                ss = new StyleSpan(Typeface.NORMAL);
                break;
            case TYPEFACE_BOLD:
                ss = new StyleSpan(Typeface.BOLD);
                break;
            case TYPEFACE_ITALICS:
                ss = new StyleSpan(Typeface.ITALIC);
                break;
            case TYPEFACE_BOLD_ITALICS:
                ss = new StyleSpan(Typeface.BOLD_ITALIC);
                break;
            default:
                ss = new StyleSpan(Typeface.NORMAL);
        }

        str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

MainActivity.java:

package com.example.syed.andtexteditor;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {
    boolean boldClicked=false;
    int typefaceStyle = TextArea.TYPEFACE_NORMAL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button boldButton = (Button)findViewById(R.id.bold_button);
        boldButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextArea t =  (TextArea) findViewById(R.id.textInput);
                if (!boldClicked)
                    boldClicked = true;
                else if (boldClicked)
                    boldClicked = false;
                if (boldClicked) {
                    typefaceStyle = TextArea.TYPEFACE_BOLD;
                    t.changeTypeface(typefaceStyle);
                }
            }
        });
    }
}
logdev
  • 292
  • 6
  • 22

1 Answers1

0

add this line to your layout's Editext

android:textStyle="bold"

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29