1

I want to insert images into dynamic buttons, I can get everything other than images. I want my button to display as text+image, but instead I am getting text + androidgraphics.drawable.bitmapdrawable@416e5 etc. Can anyone tell what the mistake is in my code, any help appreciated.

private class LongOperation  extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
        String data ="";

        int sizeData = 0;
        private View addButton;
        protected Bitmap myBitmap;
        private String image; 

        protected void onPreExecute() {

             Dialog.setMessage("Please wait..");
             Dialog.show();

              }

        // Call after onPreExecute method
        protected Void doInBackground(String... urls) {
                /************ Make Post Call To Web Server ***********/
          BufferedReader reader=null;
                  // Send data
                try
                {
                    // Defined URL  where to send data
                   URL url = new URL(urls[0]);


                    JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu");
                   // JSONObject e = jsonMainNode1.getJSONObject(position);

                 int lengthJsonArr = jsonMainNode1.length(); 
                   for(int i=0; i <lengthJsonArr; i++)
                  {                                     
                      JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i);

                          String Pid      = jsonChildNode.optString("pid".toString());
                          String Name     = jsonChildNode.optString("name").toString();
                          String Refid=jsonChildNode.optString("refid".toString());


                       String imagefile = jsonChildNode.getString("image_url");
                      String resName = imagefile.split("\\.")[2]; 


                        int resId = getResources().getIdentifier(resName, "drawable", getPackageName());
                      Drawable image = getResources().getDrawable(resId);


                     OutputData = Name+image;  

                      str_id = Pid;

                     LinearLayout buttonContainer=(LinearLayout)findViewById(R.id.btn_container);
                    Button button = new Button(buttonContainer.getContext());

                   button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                   ViewGroup.LayoutParams params = button.getLayoutParams();
                   //Button new width
                   params.width = 150;
                   params.height = 150;
                   button.setLayoutParams(params);
                   button.setText(OutputData);
                 //  button.setBackground(itemImage);

                   button.setTag(Refid);
                   button.setTextColor(Color.parseColor("#FEFCFF"));
                   button.setBackgroundResource(R.drawable.button_color);


                   buttonContainer.addView(button);         
ammu
  • 117
  • 2
  • 14

2 Answers2

1

Drawable image = getResources().getDrawable(resId);

You are trying to print the image which is a drawable object, So it will only display the address information as you see androidgraphics.drawable.bitmapdrawable@416e5

try OutputData = Name;

you are setting background for button as

button.setBackgroundResource(R.drawable.button_color); are you sure that the image you expect is same as button_color, It sounds like a color.

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

you should do something as the following :

OutputData = Name; 

then to add the text to the button do the following :

button.setText(OutputData);

and to add the image to the button do the following :

   btn.setBackgroundResource(resId);

and give me some feedback

Hope that helps .