Android Open Source - Resonos-Android-Framework Tool Bar Button






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.resonos.apps.library.widget;
/*w  ww  . ja va2s  .  c  o  m*/
import java.util.Locale;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.internal.view.menu.ActionMenuItemView;
import com.resonos.app.library.R;
import com.resonos.apps.library.Action;
import com.resonos.apps.library.App;

/**
 * This class creates a buttons similar to the style of the buttons from the ActionBar.
 * @author Chris
 */
public class ToolBarButton extends ActionMenuItemView {
  
  // objects
  private CharSequence mTitle;
  private View customControl = null;
  private OnPressButtonListener mListener;
  
  /**
   * Listens for button presses in this toolbar.
   */
  public interface OnPressButtonListener {
    /**
     * Called when button was pressed
     * @param tbb : the button pressed
     */
    void onButtonPressed(ToolBarButton tbb);
  }
  
  public ToolBarButton(Context context) {
    super(context);
  }
  
  public ToolBarButton(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  
  public ToolBarButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  
  /**
   * Set the generic button press listener associated with this button.
   * @param t : the ToolBar
   */
  protected void setPressListener(OnPressButtonListener l) {
    mListener = l;
  }

  @Override
    public void onClick(View v) {
    if (mListener != null)
      mListener.onButtonPressed(this);
    }
  
  /**
   * Set the icon for the toolbar button
   * @param resid : the id of the drawable
   */
  public void setIcon(int resid) {
    setIcon(resid == 0 ? null : getContext().getResources().getDrawable(resid));
  }

  @Override
    public void setIcon(Drawable icon) {
    ImageButton ib = (ImageButton) findViewById(R.id.abs__imageButton);
    ib.setImageDrawable(icon);
        if (icon != null) {
          ib.setVisibility(VISIBLE);
        } else {
          ib.setVisibility(GONE);
        }

        findViewById(R.id.abs__textButton).setVisibility(GONE);
    }

    /**
     * Set the title of this button
     * @param title : the button text, or null for no text or tooltip
     * @param visible : true to make the text visible, or false to use a long-press tooltip
     * @param icon : the resource for the button's icon
     */
    public void setTitle(CharSequence title, boolean visible, int icon) {
        mTitle = title;
        if (title != null) {
          String title2;
          if (icon == Action.ICON_NONE)
            title2 = ("  " + mTitle.toString().toUpperCase(Locale.US) + "  ");
          else
            title2 = (mTitle.toString().toUpperCase(Locale.US) + "  ");
          ((TextView)findViewById(R.id.abs__textButton)).setText(title2);
          setContentDescription(mTitle);
        }

        findViewById(R.id.abs__textButton).setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * Set the title of this button
     * @param title : the button text, or null for no text or tooltip
     * @param icon : the resource for the button's icon
     */
    public void setTitle(CharSequence title, int icon) {
    setTitle(title, title != null, icon);
    }

    @Override
    public boolean onLongClick(View v) {
        if (hasText()) {
            // Don't show the cheat sheet for items that already show text.
            return false;
        }

        final int[] screenPos = new int[2];
        final Rect displayFrame = new Rect();
        getLocationOnScreen(screenPos);
        getWindowVisibleDisplayFrame(displayFrame);

        final Context context = getContext();
        final int width = getWidth();
        final int height = getHeight();
        final int midx = screenPos[0] + width / 2;

        Toast cheatSheet = Toast.makeText(context, mTitle, Toast.LENGTH_SHORT);
        if (midx < displayFrame.width()) {
            // Show along the left; follow action buttons
          if (screenPos[1] >= (App.SCREEN_HEIGHT/2f))
              cheatSheet.setGravity(Gravity.TOP | Gravity.LEFT,
                  displayFrame.left, screenPos[1] - height);
          else
              cheatSheet.setGravity(Gravity.TOP | Gravity.LEFT,
                  displayFrame.left, screenPos[1] + height / 2);
        } else {
            // Show along the bottom center
            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
        }
        cheatSheet.show();
        return true;
    }
    
    /**
     * Dock a single custom View inside this button.
     * @param v : the View to dock.
     * @param fill : true if this button should fill the rest of the toolbar row
     */
  public void dockCustomControl(View v, boolean fill) {
    undockCustomControl();
    customControl = v;
    setFocusable(false);
    addView(customControl, new LayoutParams(
        fill ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT));
  }
    
  /**
   * Undock the custom View inside this button, if any.
   */
  public void undockCustomControl() {
    if (customControl != null) {
      removeView(customControl);
      setFocusable(true);
      customControl = null;
    }
  }

  /**
   * Set up this toolbar button based on its properties
   * @param action : the associated {@link Action}
   * @param vrt : true if this is a vertical toolbar
   */
  public void setup(Action action, boolean vrt) {
    undockCustomControl();
    setEnabled(!action.disabled);
    setClickable(!action.nonPressable);
    setPressed(action.pressed);
    if (action.customControl != null)
      dockCustomControl(action.customControl, action.customControlFill);

    Drawable d = action.getDrawable(getContext());
    if (action.icon != Action.ICON_BITMAP && d != null)
      d.setBounds(0, 0, Math.round(32*App.DENSITY), Math.round(32*App.DENSITY));
    setIcon(d);
    setId(action.id);
    setTitle(action.text, (action.wt && !vrt) || action.over, action.icon);
    setVisibility(View.VISIBLE);
    invalidate();
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java