0

I tried to update a TextView after button is clicked. I used the Strings.xml to help me out and named it as "justice". Unfortunately when I click the button, apparently that all text in "justice" is added twice. One with bold fonts and another without bold fonts.

I tried added "%s", but it doesn't work either. I used Android Studio 3.3.0.

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   tView = (TextView) findViewById(R.id.textView1);
   clickhere = (Button) findViewById(R.id.button1);

   clickhere.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
          String display = String.format(getString(R.string.justice, "%s"));
          tView.setText(display);
       }
   });
}

I expect to see only one copy of "justice" when I click the button. Does this bug comes from Android Studio or something is wrong with my code??? Someone, help me please. Thank you.

Quinn
  • 7,072
  • 7
  • 39
  • 61
Wege
  • 1
  • 3

3 Answers3

0

You should be able to set the text like this:

tView.setText(getString(R.string.justice));
Quinn
  • 7,072
  • 7
  • 39
  • 61
  • Nah.... It is just the same. It added all the text of "justice" twice. One with bold and second without bold style. VERY-VERY WEIRD. – Wege Feb 07 '19 at 21:26
  • is it possible you have 2 separate text fields in your layout file? It makes no sense that the same text field has bold and non-bold text if you are setting it this way – Quinn Feb 07 '19 at 21:27
  • Yeah, it works now. My bad. I forgot that I add two TextView in layout file. Now the problem is how come the Text doesn't have any bold style while I added a few in text ??? – Wege Feb 07 '19 at 21:31
  • you don't set bold with in android. You have to set the textStyle in the xml file like in this question: https://stackoverflow.com/questions/4792260/how-do-you-change-text-to-bold-in-android – Quinn Feb 07 '19 at 21:33
0
public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   // clean up code :)
   tView = findViewById(R.id.textView1);
   clickhere = findViewById(R.id.button1);

   clickhere.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
          tView.setText(R.String.justice);
       }
   });
}
Amirhf
  • 380
  • 4
  • 16
  • your answer is the most correct, sir. By doing it directly, now the bold style ( Text ) is working again. Thanks. – Wege Feb 07 '19 at 22:15
0

First of all, check your strings.xml and make sure you do not have duplicate strings in justice. If it does not, try the following

 public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   tView = (TextView) findViewById(R.id.textView1);
   clickhere = (Button) findViewById(R.id.button1);

   clickhere.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
          String display = getString(R.string.justice);
          tView.setText(display);
       }
   });
}

By the way, to get the reference to view objects, you do not need explicit casting. The following is also fine.

tView = findViewById(R.id.textView1);
clickhere = findViewById(R.id.button1);
Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33