typedefcoder2
typedefcoder2

Reputation: 300

Associate an image with Radio button in Android

I want to fetch Images from my URL and add it to the radio button that is further added to a radio group. However, i could not find metod such as setText to set text for radio button. I wish to add image to my radio button in similar fashion. Is there any hint/method that can do the same for me? Any hint/code example would be really helpful for me,

        RadioGroup rg = new RadioGroup (this); 
            rg = (RadioGroup) findViewById(R.id.radioGroup1);
            RadioButton rb[]= new RadioButton[children.size()];

            for (int i = 0; i < var.size(); i++) 
                {   
                Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                MovieName[i]=movieAtt.getAttributeValue( "Title" );
                MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                ShowTime[i]=movieAtt.getAttributeValue( "showtime" );

                rb[i] = new RadioButton(this);
                rb[i].setText(movieAtt.getAttributeValue("Title"));
                rg.addView(rb[i]);  

                //Calling this func to get Images
                //LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
                //rb[i].buildDrawingCache(LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" )));



public static Drawable LoadImageFromWebOperations(String url) 
{
        try 
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e) 
        {
            return null;
        }
}

Upvotes: 1

Views: 1820

Answers (1)

Tobbe
Tobbe

Reputation: 1831

Is the problem with the image or the text? As for the image, I believe this should work:

Drawable d = LoadImageFromWebOperations(movieAtt.getAttributeValue("cover"));
rb[i].setButtonDrawable(d);

Make sure to set gravity to Gravity.CENTER or your image won't be centered around the radio button.

You should find information about the text in the RadioButton reference.

Upvotes: 1

Related Questions