0

How can I use a custom actionbar layout with AndroidAnnotations?

The code that works:

@EActivity(R.layout.activity_register_classes)
public class RegisterClassesActivity extends ActionBarActivity {

    @AfterViews
    void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(R.layout.fragment_save_cancel_action_bar);

        findViewById(R.id.id_cancel).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(getBaseContext(), "Cancelando...", Toast.LENGTH_SHORT).show();
           }
        });
    }
}

But the following snippet does not:

@EActivity(R.layout.activity_register_classes)
public class RegisterClassesActivity extends ActionBarActivity {

    @AfterViews
    void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(R.layout.fragment_save_cancel_action_bar);
    }

    @Click(R.id.id_save)
    void cancel() {
        Toast.makeText(getBaseContext(), "Cancel...", Toast.LENGTH_SHORT).show();
    }
}

I am using the version 3.0.1 of the library. Android API 19

Claudemiro
  • 131
  • 1
  • 5

1 Answers1

1

I see this questions has some time, I hope to be helpfull now. So I got in this problem too cause Android Annotations documents is very poor. Actually I have to solve this twice cause my first solution didnt work with the first tap, I will explaing this in code.

1.- The first is to create a menu, you have to do that inside res/menu, lets name it "myactivity_menu.xml" and it should look something like this (it should have being created by default when you created the Activity):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"    tools:context=".MyActivity">
    <item android:id="@+id/thisIsAnId"
        android:icon="@drawable/your_drawable"
        android:title="@string/your_string"
        app:showAsAction="always"/> />
</menu>

The important in this code is to give it an id (you are gonna need it), and if you want to have a good looking icon, upload an icon to your drawable folder as any other asset (no sure but I think string is optional).

2.- Now go to your activity and call the menu using AndroidAnnotations this way:

@EActivity(R.layout.my_activity)
@OptionsMenu(R.menu.myactivity_menu)

Is that simple just add @OptionsMenu under the @EActivity and thats it, your menu should now be visible.

How ever, here comes a great point, so how do I do something with the button or icon in the menu? Is a very good question cause the AndroidAnnotations doc is not clear about this, the examples are very poor and can lead you to confusion. So this is what we have to do:

3.- Here we are going to set a finder for our menu item:

   @OptionsItem(R.id.thisIsAnId)
    void whatEverToDoWithMyMenuItem() {
    }

This needs to have a void method below, I think this is the only proper indication in the AndroidAnnotations doc for this.

This code can be placed under @AfterViews.

However, this stil not clear, cause AndroidAnnotation change the Android logic derive from onItem... So what happen if I have to set a listener?

My mistake here was to use the normal Android logic inside the void method we just created. That will not work fine.

4.- So what is so hard in this point is we have to simply add whatever we want to do, cause we allready have the listener:

    @OptionsItem(R.id.thisIsAnId)
    void whatEverToDoWithMyMenuItem() {
      Toast toast = Toast.makeText(getApplicationContext(), "Sample Text",       
      Toast.LENGTH_LONG);
      toast.show();
    }

The trick here is the listener for the click is allready set when we did @OptionsItem(R.id.thisIsAnId) so we can just do whatever we want inside the void method below.

If for some reason you use an Alert inside the voide method and get an error please see this questions, my got fixed by using this

Link 1

Link 2

If anyone from AndroidAnnotations is reading this, please improve your docs.

Community
  • 1
  • 1
cutiko
  • 9,887
  • 3
  • 45
  • 59
  • I am an AndroidAnnotations developer. What is missing from our docs? – WonderCsabo Sep 03 '15 at 11:04
  • Thanks for caring I have suffer a lot. I think the most friendly structure is the WordPress Codex use. First, define what is the code about. After that, a couple of examples. Finally a you might be looking for this section. I would emphasize the examples. With out going far the problem for understanding how to use the @OptionItem is that is not explained that is allready a listener. In others cases like using a service is never explained in more details. You have to make fool proof docs. Literally write "This should be place in X file, if is not created, create it now. Place it under Y" – cutiko Sep 03 '15 at 20:28
  • Thanks for the feedback! I think the wiki is pretty clear on the menu subject: "`@OptionsItem` marks methods that receives menu selection events". Please note AA is intended for developer who already familiar with the Android API. So it is trivial that `@OptionsItem` is using a listener. But you can always check out the generated code, this is a really great thing in AA - no magic. Bear in my the wiki is written by AA developers, not plain users, so we may skip some info which is only trivial to us. If you have suggestions, do not hesitate to share them in the chat room or mailing list. – WonderCsabo Sep 03 '15 at 21:34
  • Please, thanks you for your attention. And now I understand. Well then I would like to suggest you to please refocus AA to every audience. The reason is very simple, AA is bether for rookies than the Android API, is much more simpler and much more cleaner. I would look in to the channels you mention. Again, thanks you so much. Hope you can improve your wiki for noobs like me, it will help a lot of beginner developers. – cutiko Sep 04 '15 at 15:41