Esempio n. 1
0
    @Override
    protected void onPostExecute(List<Action> actions) {
      if (actions == null) {
        statusLabel.setText("Unable to get actions");
        return;
      }

      // all is ok, replace start pane
      LinearLayout contentPane = (LinearLayout) findViewById(R.id.contentPane);
      contentPane.removeView(startPane);

      LinearLayout buttonsLayout = new LinearLayout(getApplicationContext());
      buttonsLayout.setGravity(Gravity.CENTER);

      TableLayout buttonsTable = new TableLayout(getApplicationContext());
      TableRow currentTableRow = new TableRow(getApplicationContext());

      TableRow.LayoutParams buttonMarginParams =
          new TableRow.LayoutParams(
              TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
      int marginPx =
          (int)
              TypedValue.applyDimension(
                  TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
      buttonMarginParams.setMargins(marginPx, marginPx, marginPx, marginPx);

      for (final Action action : actions) {
        ImageButton actionButton = new ImageButton(getApplicationContext());
        int drawableId = getResources().getIdentifier(action.icon, "drawable", getPackageName());
        actionButton.setImageDrawable(getResources().getDrawable(drawableId));
        actionButton.setContentDescription(action.description);
        actionButton.setLayoutParams(buttonMarginParams);
        actionButton.setBackgroundResource(R.drawable.button);
        actionButton.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                new PerformActionAsyncTask().execute(action.name);
              }
            });

        currentTableRow.addView(actionButton);
        if (currentTableRow.getChildCount() == 3) {
          buttonsTable.addView(currentTableRow);
          currentTableRow = new TableRow(getApplicationContext());
        }
      }

      if (currentTableRow.getChildCount() > 0) {
        buttonsTable.addView(currentTableRow);
      }

      buttonsLayout.addView(buttonsTable);

      LinearLayout.LayoutParams params =
          new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
      contentPane.addView(buttonsLayout, params);

      runStatusUpdateTimer();
    }