[Android SDK] Using butterknife for ActionBar Views

This is an example of how to inject your ActionBar Views and use them with Butterknife‘s @OnClick annotation:

ExampleActivity.java:

@OnClick(R.id.ab_detail_btn1)
 public void onBtn1Clicked(){
     Log.i(Constants.LOG_TAG, "AB Next clicked!");
 }
private void initActionBar() {
   ActionBar ab = getActionBar();
   ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.ab_detail, null);
   ab.setCustomView(vg);
   ButterKnife.inject(this, vg);
   ab.setDisplayHomeAsUpEnabled(false);
   ab.setDisplayShowTitleEnabled(false);
   ab.setDisplayShowHomeEnabled(false);
   ab.setDisplayShowCustomEnabled(true);
 }
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  initActionBar();
}

ab_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
  <ImageButton
   android:id="@+id/ab_detail_btn1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
</RelativeLayout>

With only a few code, without findViewById() calls, elegant, and maintainable 🙂

Let me know if it helps!

Have a nice day

Advertisement

[Android SDK] WearableListenerService crashes when sending event

This little nightmare has made me waste several hours. Let’s explain it.

Well, the root cause is that the only Lollipop (5.0.1) device that I own in this moment hasn’t got a proper USB driver, so I’m not able to debug step-by-step on it with ADB.

I was trying to control a ViewPager from my Wearable; There are two buttons in the clock (next, previous), and there is a viewPager on the phone.

When clicking “Next”, viewPager goes to next item, when clicking “Previous” on Wearable, phone goes to previous item.

I was wondering why the App was crashing every time I tried to modify the viewPager, but not when displaying a Toast.

The reason is simple:

WearableListenerService does not run on the UI Thread.

So, everytime I tried to do viewPager.setCurrentItem(), app crashed. Due to the lack of USB drivers, I was not able to trace this 😦

Solution: Easy

viewPager.post(new Runnable() {
    public void run() {
        if(viewPager.getCurrentItem() > 0)
            viewPager.setCurrentItem(viewPager.getCurrentItem()-1, true);
    }
});

I hate wasting time on these silly mistakes 😦 hope you guys don’t  !!

Nice coding!

[Android SDK] OnMessageReceived not called in WearableListenerService

After adding a wear module to an old app, I found that the messages sent by my wearable were not waking up my WearableListenerService implementation.

After several wasted hours, I noticed that the wear module’s package name was wrong.

This is how it was, and how I fixed it.

BAD:
[module: app] es.voghdev.myapp
[module: wear] es.voghdev.myapp.wear
GOOD:
[module: app] es.voghdev.myapp
[module: wear] es.voghdev.myapp

It finally works. Made me waste several valious hours!!

I hope that you, reader don’t fall on the same pit! 🙂 also replied this SO question.

Have a nice coding day!

[Marmalade SDK] Signing error: Can’t find your distribution signing private key at iPhone Distribution, when deploying to iOS

After updating Marmalade from 7.2 to 7.5, I just found this error when deploying to iOS:

ERROR: Signing error: Can’t find your distribution signing private key at iPhone Distribution

The reason is quite simple: My Apple Developer’s code-signing certificates (.cer, .key, .p12, .mobileprovision…) were not copied from the old to the new installation.

Just had to copy them from

C:\Marmalade\7.2\s3e\deploy\plugins\iphone\certificates

to C:\Marmalade\7.5\s3e\deploy\plugins\iphone\certificates.

After that, deployment succeeded, and .ipa file was succesfully generated 🙂

[Update] Particularly, the file that is lacking now is the distribution_identity.key. That’s exactly the private key that Deploy Tool is searching for.