Ejemplo n.º 1
0
  public void addSegment(String title) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    ToggleButton b = new ToggleButton(getContext());
    b.setText(title);
    b.setTextOff(title);
    b.setTextOn(title);
    b.setTextColor(Color.WHITE);
    b.setTypeface(Typeface.DEFAULT_BOLD);
    b.setMinimumWidth((int) (40 * metrics.density));
    if (mSwitches.size() == 0) {
      b.setBackgroundResource(mLeftDrawable);
      b.setEnabled(false);
      b.setChecked(true);
    } else {
      if (mSwitches.size() > 1) {
        ToggleButton temp = mSwitches.get(mSwitches.size() - 1);
        temp.setBackgroundResource(mMidDrawable);
      }

      b.setBackgroundResource(mRightDrawable);
    }

    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.weight = 0.1f;
    params.gravity = Gravity.CENTER;
    b.setLayoutParams(params);

    b.setOnCheckedChangeListener(this);

    mSwitches.add(b);
    addView(b);
  }
Ejemplo n.º 2
0
  public void setNumberOfSegments(int num) {
    mSwitches.clear();

    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    for (int i = 0; i < num; i++) {
      ToggleButton b = new ToggleButton(getContext());
      if (i == 0) {
        b.setBackgroundResource(mLeftDrawable);
        b.setEnabled(false);
        b.setChecked(true);
      } else if (i == num - 1) {
        b.setBackgroundResource(mRightDrawable);
      } else {
        b.setBackgroundResource(mMidDrawable);
      }

      b.setText("");
      b.setTextOff("");
      b.setTextOn("");
      b.setTextColor(Color.WHITE);
      b.setTypeface(Typeface.DEFAULT_BOLD);
      b.setMinimumWidth((int) (40 * metrics.density));

      LinearLayout.LayoutParams params =
          new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      params.gravity = Gravity.CENTER;
      params.weight = 0.1f;
      b.setLayoutParams(params);

      b.setOnCheckedChangeListener(this);

      mSwitches.add(b);
      addView(b);
    }
  }