Show Dynamic ImageButtons one at a time as their backgrounds

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Show Dynamic ImageButtons one at a time as their backgrounds

Postby storm_to » Fri Jul 16, 2010 10:36 pm

Beginner with Android (1st app) and Beginner with Java, previous experience was only C,C++ and Python

There that may explain any stupidity that is to follow!

Problem:
From main app screen I click a button that takes me to a new screen (separate Activity/Intent)
This new screen immediately displays three local content buttons and spawns a new thread that starts loading online images to be used for dynamic buttons... ( Don't freak out, I AM using a Handler :) )
I see the 3 local buttons immediately but the online buttons are not shown one at a time as they are loaded (I see progress in Logcat), but are all shown at the end after the last one has been loaded.

More Info:
Layout.xml description in pseudo code
Code: Select all
<scroll view>
    <relative layout>
        <table>
             <table row>
                  <local content button 1>
                  <local content button 2>
                  <local content button 3>
</close all>


onCreate sets content view to R.Layout.xml
calls a function that sets button backgrounds and onClickListeners etc.
AND does this:
Code: Select all
new OnlineContentThread(asyncRefresh).start();


And here's the sub class def
Code: Select all
   private class OnlineContentThread extends Thread {
      Handler handler;
      public OnlineContentThread(Handler h) {
         handler = h;
      }

      public void run(){
         Log.d(TAG, "Running ASYNC THREAD!");
         
         //Check if we're online
         ONLINE = Client.checkConnectivity(activity);
         if ( !ONLINE ) offlinePrompt();
         else {
            //Connect to airosgroup.com and get available APPETIZERS content
             contentList = Client.listFiles( getString(CATEGORY) );
             
             Message msg;
             Bundle bundle;
             for (String[] i:contentList){
                bundle = new Bundle();
                bundle.putStringArray("CONTENT", i);
                msg = handler.obtainMessage();
                msg.setData(bundle);
                handler.sendMessage(msg);   
             }
         }
      }
   }


And here's the handler def:
Code: Select all
Handler asyncRefresh = new Handler(){
      public void handleMessage(Message msg){
         Bundle threadBundle = msg.getData();
         content = threadBundle.getStringArray("CONTENT");
         drawDynamicContent(content);   
      }
   };


Now here's explanation of drawDynamicContent(content); because it's super long and confusing, even to me and I wrote it...

content is just a string URL pointing to images to be used
drawDynamicContent(content) creates a new tableRow under the table defined in Layout.xml that already has 1 row with 3 buttons in it. I only create a new row once every 3 buttons (using global couter)
Then I create a new imageButton
take the string URL and call Client.createDrawableFromUrl(url) that return Drawable object. I take this Drawable and set it as a background for the button.
define onClick function
do TableRow.adView(button)
Again once every 3 buttons I to table.adView(tableRow)

Question:
Why do all buttons show up in the end and not as they are loaded.
Or at least one row at the time, since I don't do the table.addView every time
I tried using .invalidate() on buttons, tableRows and table but no luck.
I don't even know what .invalidate() does but I seen it in plusminus' pizza timer example and thought it may apply in my case :lol:

Any insight would be greatly appreciated.

Cheers!
storm_to
Freshman
Freshman
 
Posts: 8
Joined: Thu Jul 08, 2010 4:24 pm

Top

Re: Show Dynamic ImageButtons one at a time as their backgro

Postby nicholas.hauschild » Sat Jul 17, 2010 12:16 am

Calling invalidate() on a View will request a redraw of that View.

I am wondering, since you did not post the method most important, are you calling invalidate() on each button as the image is available? That is, once you have successfully downloaded the image from whatever site, are you applying it to the button and then calling invalidate() on that button? I believe that is what you want to do, assuming you are not...

Good Luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Show Dynamic ImageButtons one at a time as their backgro

Postby storm_to » Mon Jul 19, 2010 3:14 pm

Thanks for your reply Nick.
Now that I someone did confirm that invalidate() is the right approach, I tried to invalidate the following, ALL WITH NO SUCCESS!

1.) Invalidate just the button

2.) Invalidate button, tableRow, table

3. Invalidate table, tableRow, button (just in case order does matter)

Nothing worked. Here is the drawDynamicContent() source:

Code: Select all
private void drawDynamicContent(String[] contentList)
    {       
       /* Find Table layout defined in main.xml */
        TableLayout tableLayout = (TableLayout)findViewById(R.id.RECIPE_TABLE);
        //TableLayout tll = (TableLayout)findViewById(R.id.RECIPE_TABLE);
        if (buttonPosition%3==0)
        {
           buttonPosition=0;
           rowNum++;
           tableRowButtons = new TableRow(this);
           tableRowButtons.setPadding(0, 5, 0, 0);
           tableRowButtons.setLayoutParams(new LayoutParams(
                           LayoutParams.WRAP_CONTENT,
                           LayoutParams.WRAP_CONTENT));
           tableRowLabels = new TableRow(this);
            tableRowLabels.setLayoutParams(new LayoutParams(
                             LayoutParams.WRAP_CONTENT,
                             LayoutParams.WRAP_CONTENT));
           
        }
            String contentName;
            String contentText;
            Drawable contentImage=null;
           
            contentName=contentList[0].split(".html")[0];
            final String ccontentName=contentList[0].split(".html")[0];
            contentText=contentName.replace("_", " ");
          try {
             contentImage=null;
             contentImage = Client.getDrawableFromUrl(Client.ROOT+"/"+getString(CATEGORY)+"/images/"+contentName+".jpg");
          } catch (Exception e) { Log.e(TAG,e.getMessage()); }
            Log.d(TAG,"DRAWABLE URL : "+ Client.ROOT+"/"+getString(CATEGORY)+"/images/"+contentName+".jpg");

           /* Create a Button to be the row-content. */
           ImageButton imageButton = new ImageButton(this);              
           imageButton.setBackgroundDrawable(contentImage);
           LayoutParams lp = new LayoutParams(WIDTH,HEIGHT);
           lp.setMargins(5, 5, 5, 5);
           imageButton.setLayoutParams(lp);
              
           imageButton.setOnClickListener(new OnClickListener(){
              public void onClick(View arg0) {
                  Toast.makeText(context, "L O A D I N G . . .", Toast.LENGTH_SHORT).show();
                  Intent i = new Intent(CategoryView.this,RecipeView.class);
                  i.putExtra("URL", Client.ROOT+"/"+getString(CATEGORY)+"/"+ccontentName+".htm");
                  startActivity(i);
               }
                  
               });
           /* Add Button to row. */
           tableRowButtons.addView(imageButton);           
            if (rowNum%2!=0){
               tableRowButtons.setBackgroundColor(Color.rgb(0xEA,0xDF,0xDD));}
            else {
               tableRowButtons.setBackgroundColor(Color.WHITE);}
           
            if (buttonPosition%3==0){
               /* Add row to TableLayout. */
               tableLayout.addView(tableRowButtons, new TableLayout.LayoutParams(
                          LayoutParams.WRAP_CONTENT,
                          LayoutParams.WRAP_CONTENT));
            }
           
           /* Create a Button to be the row-content. */
           TextView textViewLabel = new TextView(this);
           textViewLabel.setLines(3);
           textViewLabel.setMinLines(2);
           textViewLabel.setMaxWidth(WIDTH);
              
           String loopName=contentList[0].split(".html")[0];
           contentText=loopName.replace("_", " ");
           textViewLabel.setText(contentText);
           textViewLabel.setLayoutParams(new LayoutParams(
                              LayoutParams.WRAP_CONTENT,
                              LayoutParams.WRAP_CONTENT));
           textViewLabel.setGravity(Gravity.CENTER|Gravity.TOP);
           textViewLabel.setMinLines(2);
           /* Add Button to row. */
           tableRowLabels.addView(textViewLabel);
           
           
            if (rowNum%2!=0){
               tableRowLabels.setBackgroundColor(Color.rgb(0xEA,0xDF,0xDD));}
            else {
               tableRowLabels.setBackgroundColor(Color.WHITE);}
           
            if (buttonPosition%3==0){
               /* Add row to TableLayout. */         
               tableLayout.addView(tableRowLabels,new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
               
            }
            buttonPosition++;
           
            tableLayout.invalidate();
            tableRowLabels.invalidate();
            tableRowButtons.invalidate();
            textViewLabel.invalidate();
            imageButton.invalidate();
    }


Any insight is greatly appreciated!

.
storm_to
Freshman
Freshman
 
Posts: 8
Joined: Thu Jul 08, 2010 4:24 pm

Re: Show Dynamic ImageButtons one at a time as their backgro

Postby storm_to » Fri Jul 23, 2010 7:43 pm

Should I be using AsyncTask instead of Thread?

I tried implementing AsyncTask but results are even worse
Getting a black screen for the entire duration of backgroundDownload()
Screen with all content loads in the very end after all data was downloaded.

Help! Anyone?
storm_to
Freshman
Freshman
 
Posts: 8
Joined: Thu Jul 08, 2010 4:24 pm

Re: Show Dynamic ImageButtons one at a time as their backgro

Postby storm_to » Tue Jul 27, 2010 5:23 pm

bump
storm_to
Freshman
Freshman
 
Posts: 8
Joined: Thu Jul 08, 2010 4:24 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 9 guests