In AA+AppCompat, I try to change below code in Activity to AA-style.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_done, menu);
MenuItem menuItem = menu.findItem(R.id.itemDone);
View menuView = MenuItemCompat.getActionView(menuItem);
mButtonQuestionPost = (Button) menuView.findViewById(R.id.buttonMenuDone);
mButtonQuestionPost.setOnClickListener(this);
return super.onCreateOptionsMenu(menu);
}
First, changed head of Activity as below. The menu was shown as expected.
@EActivity(R.layout.activity_question_post)
@OptionsMenu(R.menu.menu_done)
public class QuestionPostActivity extends FragmentActivity {...
And, try to do button click method as below but nothing fired. I changed @Click
with @OptionsItem
or changed attributes this and that but no luck.
@Click(R.id.buttonMenuDone)
void buttonMenuDone(){
if (mQuestionPostFragment.validatePost()) {
setSupportProgressBarIndeterminate(true);
mQuestionPostFragment.postQuestion();
}
}
menu.xml is as below. Note that I'm using actionLayout
for design purpose.
<item
android:id="@+id/itemDone"
android:title="@string/done"
app:showAsAction="always"
android:menuCategory="system"
app:actionLayout="@layout/item_menu_done"
/>
item_menu_done is as below.
<Button
android:layout_width="48dp"
android:layout_height="?actionBarSize"
android:id="@+id/buttonMenuDone"
android:text="@string/done"
android:textColor="@android:color/white"
android:textSize="14sp"
android:background="?attr/actionBarItemBackground"
/>
The AA-generated file does not have MenuItemCompat.getActionView(menuItem)
, but hardly to make it with AA. Can someone please help me?
I am afraid you cannot bind a listener with @Click
to a menu action view, since Activity.findViewById
cannot find that view inside the menu items. What you can do is injecting the menuitem, then, manually bind your listener as you already did.
@EActivity(R.layout.activity_question_post)
@OptionsMenu(R.menu.menu_done)
public class QuestionPostActivity extends FragmentActivity {
@OptionsMenuItem(R.id.menuItemDone)
MenuItem buttonMenuDone;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// this will be called by the generated subclass after it injected the menu
MenuItemCompat.getActionView(itemDone).findViewById(R.id.buttonMenuDone).setOnClickListener(this);
return true;
}
}