0

In the edit text, the user should select whether what they type should be bold or italics or normal. In my case when a user selects bold the entire edit text changes to bold and if he again selects italics the entire edit text becomes italics. When the user selects bold types something, it should be bold and when he selects italics or normal the words that he types after that should be italics or normal, not the previous one which is bold. also when I print the input in edit text it is printed as normal even it is given as bold.

I have my code at the bottom

<EditText
    android:id="@+id/check_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="check"
    />

this is where I have set the edit text to bold or italics

bold.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.BOLD);
    }
});
italics.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.ITALIC);
    }
});
normal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.NORMAL);
    }
});
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        result.setText(editCheck.getText().toString());
    }
    });

How can I implement this

Eren ÇELİK
  • 102
  • 1
  • 8
Siva perumal
  • 87
  • 1
  • 10
  • And what is exactly your problem? As I have seen in your code when you do the setText to the result it is printing as normal. When you change the typeface it is changing eventhough you have written text? – Brank Victoria Nov 21 '18 at 14:20
  • when I click bold entire text changes, even text that I have typed before,I want the text typed after selecting bold should only become bold. And in the result it should be printed as it typed in the edittext. In my code I didnt know how to implement that. – Siva perumal Nov 21 '18 at 14:29
  • This `when I click bold entire text changes` do you mean that changes to bold? Or another text? – Brank Victoria Nov 21 '18 at 14:30
  • every word in the edittext changes to bold, but what I want is only the text that I start typing after the bold button is clicked should be bold. – Siva perumal Nov 21 '18 at 14:31

2 Answers2

2

If you want different font styles in your text view you can set it from HTML. Take a look to this post:

Is it possible to have multiple styles inside a TextView?

So in your case you can save the text entered in HTML format depending on what the user clicked to put <b> for bold, <i> for italics and so.

This will also help you to print it in the result textView, because you will print it from the HTML.

EDIT: For solve the problem that all your text is getting bold or italic, you must edit your onclick events to know the type of what you are writing. And also use TextWatcher to know what is the inserted text. For example:

int typeFaceSelected = Typeface.NORMAL //private global var with NORMAL as default value
String resultedText = ""; //private global var, this will give to your textview the result of the edittext in HTML
bold.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.BOLD
    }
});
italics.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.ITALIC
    }
});
normal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.NORMAL
    }
});

//You can edit the text you add
editCheck.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {}

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String sAux = s.toString().subString(start, start + count);

            switch(typeFaceSelected)
            {
                case Typeface.NORMAL:
                    resultedText = sAux;
                break;
                case Typeface.ITALIC:
                    resultedText = "<i>" + sAux + "</i>";
                break;
                case Typeface.BOLD:
                    resultedText = "<b>" + sAux + "</b>";
                break;
            }
            editCheck.setText(Html.fromHtml(resultedText));
        }
});

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        result.setText(HTML.fromHTML(resultedText));
    }
    });

The above code was based on this Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged

Also, I haven't test it so I'm not sure if its working or not, but at least I think it is enough to get the idea.

Brank Victoria
  • 1,447
  • 10
  • 17
  • even when I use html and the bold button is clicked, even the previously typed words are changed to bold. actually what I want is only when the user selects bold it should be bold from that word and not the previous word. – Siva perumal Nov 21 '18 at 15:26
  • I know what you're saying, but in this case. You must change your code. This means that you have to change the logic inside your onclick events. Let me edit my answer so it would be clearer for you – Brank Victoria Nov 21 '18 at 15:55
  • ok even I thought that would be the mistake but the logic is what I am not getting. – Siva perumal Nov 21 '18 at 16:14
  • @Sivaperumal take a look to the edition of the answer so you can understand what I am talking about. – Brank Victoria Nov 21 '18 at 16:32
  • it didnt work but yes it gave me an idea of how to do this, thanks. I will try it – Siva perumal Nov 23 '18 at 13:13
0

Try to set property android:textStyle="bold|italic" to your EditText in specific xml file

Muhammad Haroon
  • 210
  • 2
  • 11