[Android] imeOptions not working on EditText (actionNext, actionDone)

Don’t forget to add

<item name=”android:inputType”>text</item>

or

<item name=”android:inputType”>textPassword</item>

to your EditText’s style

Directly on XML:

<EditText
style=”@style/PasswordEditText”
android:id=”@+id/login_et_password”
android:layout_below=”@+id/login_tv_password”
android:inputType=”text”
android:imeOptions=”actionDone”
/>

[Android] Raizlabs/DBFlow: Warning:File for type ‘…’ created in the last round will not be subject to annotation processing.

This can be caused by two main reasons:

– Table class with no primary key ( no field is annotated with @Column @PrimaryKey )
– Table class with @Table annotation which does not extend from BaseModel

More log output that could appear:

error: cannot find symbol class <your_class>$Table

in the example it would be

error: cannot find symbol class Ant$Table

[Android] Picasso, resize() vs resizeDimen()

I had trouble changing my resize() calls to resizeDimen(), having icons displaying in different sizes.

As we can see in Picasso’s RequestCreator class , resize() specifies its size in pixels. So:

MyActivity.java
Picasso.with(this)
.load(a.getLogoUrl())
.resize(
getResources().getInteger(R.integer.header_logo_w), getResources().getInteger(R.integer.header_logo_h)) 
.into(headerLogo);
res/values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <integer name="header_logo_w">96</integer>
 <integer name="header_logo_h">@integer/header_logo_w</integer>
</resources>

Is equivalent to the following resizeDimen() call:

MyActivity.java
Picasso.with(this)
.load(a.getLogoUrl())
.resizeDimen(R.dimen.header_logo_w, R.dimen.header_logo_h)
.into(headerLogo);
res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="header_logo_h">96px</dimen>
<dimen name="header_logo_w">@dimen/header_logo_h</dimen>
</resources>

[Android SDK] My favorite Android Studio shortcut configuration

Java:

CTRL+ “-” / CTRL+ “+”: Collapse / Expand current code region

CTRL+N: generate… (New constructor, getter and setter…)

CTRL+ALT+M: Close All

CTRL+ALT+N: Close All but this (Close others)

CTRL+Shift+T: New terminal Tab (Bottom terminal of Android Studio)

CTRL+ALT+D: Debug

CTRL+F9: Build / Make project

F7: Step into

F8: Step over

F9: Continue

XML:

CTRL+Shift+I: auto-indent (also available in Java)

[Android SDK] Fragment getting rotated after camera capture

This has been a painful issue in various devices (specially Samsungs).
After taking a picture, even with the Activity locked to portrait mode, the device was forcing various rotations, destroying the whole Fragment, and all views inside.

The only solution that worked for me was:

MyCameraFragment.java:
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true); // Fixes possible rotations after taking picture with camera
 }

AndroidManifest.xml:

<activity name=".ui.activity.MyCameraActivity" configChanges="screenSize|orientation"/>

Note that MainActivity should be the activity containing the mentioned Fragment on its layout.

Hope it saves time for someone! I wasted several valious hours with this

[Android SDK] Implementing an Image Cache with Picasso

One of the coolest things about Picasso is that you can customize its behavior in a clean and easy way.

One interesting thing that one can find is the image cache.

Before starting, we have to include Picasso. For this example I’m using version 2.5.2. Inside app/build.gradle:

compile 'com.squareup.picasso:picasso:2.5.2'

Now we have to implement our own class implementing Picasso’s Cache interface. It could look like this:

public class CustomCache implements Cache {
  private Context mContext;
  private final File mCacheDir;

public CustomCache(Context mContext) {
  this.mContext = mContext;
  mCacheDir = ((MyApplication)mContext.getApplicationContext()).getPicturesDir();
 }

@Override
public Bitmap get(String key) {
  key = String.format("%s.png", transform(key) );
  File f = new File(mCacheDir, key);
  return getBitmapFromFile(f.getAbsolutePath());
}

@Override
public void set(String key, Bitmap bitmap) {
  key = String.format("%s.png", transform(key) );
  File f = new File(mCacheDir, key);
  saveBitmapToFile(bitmap, f.getAbsolutePath());
}

@Override
public int size() {
  String[] children = mCacheDir.list();
  return children != null ? children.length : 0;
}

@Override
public int maxSize() {
  return 100;
}

@Override
public void clear() {
  String[] children = mCacheDir.list();
  for (int i = 0; i < children.length; i++) {
      new File(mCacheDir, children[i]).delete();
  }
}

@Override
public void clearKeyUri(String keyPrefix) {

}

private String transform(String s) {
   return s.replaceAll("[^A-Za-z0-9]", "");
}
}

where getBitmapFromFile() and saveBitmapToFile() could be your regular implementations. At the end of the post i’ll provide two sample implementations for these methods.

The next step is to use Picasso.Builder() to make our global Picasso instance use the previous Cache implementation.

This could go in our Application class, or MainActivity’s onCreate() method:

File cacheDir = ((MyApplication)ctx.getApplicationContext()).getPicturesDir();
Cache cache = new CustomCache(ctx);
Picasso picasso = new Picasso.Builder(ctx)
 .downloader(new OkHttpDownloader(cacheDir))
 .memoryCache(cache)
 .build();
Picasso.setSingletonInstance(picasso);

From now on, when using your Picasso instance like usually:

Picasso.with(mContext)
        .load(c.getUser().getImageUrl())
        .placeholder(R.drawable.ic_launcher)
        .resize(48,48)
        .into(userThumbnail);

You’ll have all images cached inside your SD card, in the folder specified by your Cache implementation. So simple, huh?

I hope it’s been helpful for you 🙂

// TODO add getBitmapFromFile(), saveBitmapToFile() :-P