title: How to take bold characters as input in EditText in Android?
body: <p>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.</p>
  
  <p>I saw this post. <a href="https://stackoverflow.com/questions/41881373/change-future-text-to-bold-in-edittext-in-android?rq=1">Change future text to bold in EditText in Android</a>
  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.</p>
  
  <p>Here is the code:
  TextArea.java</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>MainActivity.java:</p>
  
  <pre><code>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);
                  }
              }
          });
      }
  }
  </code></pre>