How to turn text into an image

Asked

Viewed 115 times

2

In my application, I have a listview that displays the database information, I want when loading the listview the String of one of the Textview to be replaced by an image. According to the displayed text, a different image will appear. It has how to do this and how it would be?

The code is only to represent what I want in the application because it doesn’t work!

    switch (textView1.getText()) {
        case textView1.getText("carro") ;            
    textView1.setBackground(R.drawable.image1);  
            break;

        case textView2.getText("moto") ;
     textView2.setBackground(R.drawable.image2);
            break;

     case textView3.getText("caminhao") ;
     textView3.setBackground(R.drawable.image3);
            break;  

1 answer

0


To put an image as background of a Textview use the following code:

textView.setBackground(getResources().getDrawable(R.drawable.image));  

If you are using API level 21 or higher use:

textView.setBackground(getResources().getDrawable(R.drawable.image, getTheme()));
  • The code I’m trying to use is this, but it’s going wrong

  • What’s the mistake ?

  • By calling the setTvImage(); the application gives error and closes, Talve I’m making the call in a wrong location or my code that doesn’t even work!

  • Not knowing what the error is hard to help. You got the reference to Textview tvImage: tvImage = (TextView)findViewById(....);?

  • Yes, I got the reference for the textView tvImage.

  • Could the problem be because you’re using the if instead of the switch case?

  • Without you posting the log error is impossible to know where the problem is.

  • I just posted the log

  • The error says tvImage is null. With the latest developments this question is becoming a totally different one. This is out of the way of the website. The correct thing to do is to ask this question as it was at the beginning (reverse it) and do another one with the code and the log. Consider voting/accepting the answer if you deem it appropriate to do so

Show 4 more comments

Browser other questions tagged

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