Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active December 18, 2015 19:49
Show Gist options
  • Save slightfoot/5835584 to your computer and use it in GitHub Desktop.
Save slightfoot/5835584 to your computer and use it in GitHub Desktop.
Action Button with no padding example.
package com.example.actionbutton;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class ActionButton extends ImageView
{
@SuppressLint("InlinedApi")
public static final int ATTR_STYLE = android.R.attr.actionButtonStyle;
private Menu mMenu;
private MenuItem mMenuItem;
public ActionButton(Context context)
{
this(context, null);
}
public ActionButton(Context context, AttributeSet attrs)
{
this(context, null, ATTR_STYLE);
}
public ActionButton(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setOnClickListener(mOnClick);
setOnLongClickListener(mOnLongClick);
// These override the default values in the actionButtonStyle style.
setMinimumWidth(0);
setPadding(0, 0, 0, 0);
}
public void setMenuData(Menu menu, int id)
{
mMenu = menu;
mMenuItem = menu.findItem(id);
setId(id);
setImageDrawable(mMenuItem.getIcon());
setContentDescription(mMenuItem.getTitle());
}
private View.OnClickListener mOnClick = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mMenu.performIdentifierAction(mMenuItem.getItemId(), 0);
}
};
private View.OnLongClickListener mOnLongClick = new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
Toast.makeText(v.getContext(), mMenuItem.getTitle(), Toast.LENGTH_SHORT).show();
return false;
}
};
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_share"
android:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_share"
android:title="@string/action_share"
android:actionViewClass="com.example.actionbutton.ActionButton"
/>
</menu>
package com.example.actionbutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
((ActionButton)menu.findItem(R.id.action_share)
.getActionView()).setMenuData(menu, R.id.action_share);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.action_share){
Toast.makeText(this, "Got a click", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment