Change the style of an Android control panel button

I am now changing the view on my ActionBar MenuItem like this:

 MenuItem menuItem = menu.add(title); TextView tv = new TextView(context); tv.setTypeface(myTypeface); tv.setText("hello"); menuItem.setActionView(tv); 

However, this disables the button function, which I assume, because I changed the ActionView . Anyway, can I do this while retaining the button function?

0
source share
2 answers

Here's an example of creating a custom MenuItem for an ActionBar .

First one . Create a View that will serve as your MenuItem .

ActionLayoutExample

 /** * A {@link RelativeLayout} that serves as a custom {@link MenuItem} */ public class ActionLayoutExample extends RelativeLayout { /** The MenuItem */ private TextView mHello; /** * Constructor for <code>ActionLayoutExample</code> * * @param context The {@link Context} to use * @param attrs The attributes of the XML tag that is inflating the view */ public ActionLayoutExample(Context context, AttributeSet attrs) { super(context, attrs); // Ensure the MenuItem is accessible CheatSheet.setup(this); } /** * {@inheritDoc} */ @Override protected void onFinishInflate() { super.onFinishInflate(); // Initialize the TextView mHello = (TextView) findViewById(android.R.id.text1); mHello.setTypeface(myTypeface); mHello.setText("Hello"); } } 

Second one . Create a layout that you really apply to MenuItem .

action_layout_example

 <your.package.name.ActionLayoutExample xmlns:android="http://schemas.android.com/apk/res/android" style="?android:attr/actionButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:contentDescription="@string/hello_world" > <TextView android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </your.package.name.ActionLayoutExample> 

It is important to include the style="?android:attr/actionButtonStyle" to ensure that the layout correctly reflects the ActionBar elements. It is also important to include android:contentDescription in your layout. Typically, when you press MenuItem with an icon for a long time, a tooltip is displayed indicating what MenuItem . In your case, you need to take an extra step to make sure that this tooltip is still available.

A great helper for this is the Roman Nurik CheatSheet . You can see how I use it in the action layout constructor.

You specifically asked about choosing MenuItem . To do this, make sure you include the android:background="?android:attr/selectableItemBackground" and android:clickable="true" attributes.

Third . Use the android:actionLayout attribute to customize the layout and inflate the menu, usually within an Activity or Fragment .

your_menu

 <item android:id="@+id/action_typeface" android:actionLayout="@layout/action_layout_example" android:showAsAction="ifRoom" android:title="@string/hello_world"/> 

Using MenuItem

In onCreateOptionsMenu call MenuItem.getActionView and on View.onClickListener . This is because onOptionsItemSelected will not be called for a custom View

 final View example = menu.findItem(R.id.action_typeface).getActionView(); example .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Do something } }); 

results

enter image description here

+7
source

try it

 tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //handle } }); 
0
source

All Articles