mardi 4 août 2015

Dynamically add ImageButtons

I have to dynamically create ImageButtons for an Array of images after a network call is completed. I currently have it working with the amount of buttons hardcoded, but the amount of buttons will be dynamically added and removed on the server.

The XML Code is below this works as its hardcoded:

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
          xmlns:tools="http://ift.tt/LrGmb4"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:weightSum="1"
          tools:background="@color/black"
android:id="@+id/dotw_list">

<ImageButton
    android:id="@+id/dotw_imageButton_1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="5dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_3"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_4"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_5"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>
</LinearLayout>

Below is the code I have used to hard code it but i need this to be dynamically changed when there are more/less items in the Array

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

    // Get extra data included in the Intent
    String message = intent.getStringExtra("network_response");
    Log.d("receiver", "Got message: " + message);

    home = data.getHomeItem();


    try {
        for (int i = 0; i < home.dotwItemArray.size(); i++) {
            System.out.println("Size of DOTW Array for the home screen is " + home.dotwItemArray.size());

            DotwItem dotwItem = home.dotwItemArray.get(i);

            if (i == 0) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_1);
                System.out.println("dotwItem1 is set");
            }
            if (i == 1) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_2);
                System.out.println("dotwItem2 is set");
            }
            if (i == 2) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_3);
                System.out.println("dotwItem3 is set");
            }
            if (i == 3) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_4);
                System.out.println("dotwItem4 is set");
            }
        }

    } catch (Exception e) {
        System.out.println("Error is: " + e + " - Exception is it: " + e.getStackTrace()[2].getLineNumber());

    }
}
};

The reason I am doing this, is because I dont know the length of the Array that I am getting until the network call is complete. The network call is initiated in the onCreate method and as you can see this is in the onReceive method, this method is initiated once the network call is completed.

I had a look at this link from StackOverflow but Im a little confused as I am trying to set the image based on the network request.

Thanks

Aucun commentaire:

Enregistrer un commentaire