3

I have an app where need to put images to background programmatically, but it doesn't work, the code is (2 methods from BankMenuParser):

    public void getProviderFields(String file, String providerId) throws DocumentException, IOException {

        String xml = readFile(file);
        Document doc = DocumentHelper.parseText(xml);

        System.out.println("field name start");

        List list = doc.selectNodes("//root/operators/operator[@id='" + providerId + "']/fields/field/name");
        for (Iterator itr = list.iterator(); itr.hasNext();) {
//          Attribute attr = (Attribute) itr.next();
//          outList.add(attr.getValue());
            Element el = (Element) itr.next();
            Node elNode = (Node) itr.next();
//          elNode.getText();
            System.out.println("in array");
            System.out.println("field name: " + elNode.getText().toString());
        }
        System.out.println("field name end");
    }

public String getProviderImg(String file, String providerName) throws DocumentException, IOException {

    String xml = readFile(file);
    Document doc = DocumentHelper.parseText(xml);

    String img = doc.selectSingleNode("//root/menu/group/operator_id[@name='" + providerName + "']/@image").getText();

    return img;

}

public class Helpers {

    public static int getResourceId(Context context, String name, String resourceType) {
        return context.getResources().getIdentifier(name, resourceType, context.getPackageName());
    }

These methods called in Activity:

        Bundle menuExtras = getIntent().getExtras();

        BankMenuParser bmp = new BankMenuParser();
String img = bmp.getProviderImg("data/menu.xml", menuExtras.getString("provider_name"));
TextView textView = (TextView) findViewById(R.id.provider_logo);
int resid = Helpers.getResourceId(PaymentActivity.this, img, "drawable");
textView.setBackgroundResource(resid);
bmp.getProviderFields("data/provider_fields.xml", menuExtras.getString("provider_id"));

I'm interested in set the background to the image in LogCat I see the output like:

 07-13 13:27:36.178: I/System.out(677): mob_beeline.png

So, the image should be set right in all drawables I have a mob_beeline.png and layers.xml with content:

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/mob_beeline"
        android:gravity="center" />     
    </item>    
</layer-list> 
Mr Mashabela
  • 112
  • 2
  • 10
Arthur Kushman
  • 3,449
  • 10
  • 49
  • 64

1 Answers1

16
 textView.setBackgroundResource(R.drawable.mob_beeline);

See here

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • =)) If I needed this sort of easy thing I would not bring those tremendous question above, this should be done from a string programmatically - read the code more closelly, I meen U can't set the string to R.drawable."string_goes_here" – Arthur Kushman Jul 13 '12 at 14:27