How to use different fonts in the same Text field

Asked

Viewed 39 times

0

I ask for help, I am trying to build a simple program that contains a text field (Edittext) and two buttons. The idea is to write some text in my text field and then if I click the first button I want my text to change to italic and if I click the second button the text changes to bold (Bold).

I ask for help there.

I’ll post the codes:

<EditText
    android:layout_width="match_parent"
    android:layout_height="200dp" 
    android:id="@+id/et1"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="100dp" 
    android:id="@+id/italico"
    android:text="Itálico" 
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/negrito"
    android:text="Negrito"/>

And the java code:

public class MainActivity extends AppCompatActivity {

EditText t1;


Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     t1=(EditText)findViewById(R.id.texto);
     b1=(Button)findViewById(R.id.italico);
     b2=(Button)findViewById(R.id.negrito);

     b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
     //Preciso tomar o texto no campo t1 e mudá lo para itálico
         }
     });
     b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             //Preciso tomar o texto no campo t1 e mudá lo para Negrito
         }
     });
}}

Now I want to build a program that contains 3 text fields (Edittext) with the following Id text1, text2 and text3 respectively and a Button with id Take, where the program asks to type any two texts in the fields text1 and text2 and by clicking the Take button, a search is made of the texts that are in the fields text1 and text2 and write at the same time in text3 but I need the text of the text1 field printed in bold and the text of text2 in italic.

See the codes:

<EditText
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/texto1"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/texto2"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/texto3"/>


<Button
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/Take"
    android:text="Take"/>

and the Java code

@Override

protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);

    t1=(EditText)findViewById(R.id.texto1);
    t2=(EditText)findViewById(R.id.texto1);
    t3=(EditText)findViewById(R.id.texto1);


 b1=(Button)findViewById(R.id.Take);

    final Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); //negrito
    final Typeface italicTypeface = Typeface.defaultFromStyle(Typeface.ITALIC); //itálico

 b1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {


     }
 });

}

1 answer

0


To change the style of the text, you need to define a Typeface and then apply it to the desired component:

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); //negrito
Typeface italicTypeface = Typeface.defaultFromStyle(Typeface.ITALIC); //itálico

Applying to the component:

t1.setTypeface(boldTypeface);
t1.setTypeface(italicTypeface);
  • Thank you very much, it worked correctly

  • Mark the answer as correct. This helps future users with questions similar to yours.

  • Now I have a new question. Help me again. I will edit the question.

  • Help me, I’ve updated my question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.