Monday, November 16, 2015

How to add the ripple effect on any view

All you have to do is set your background to the ripple effect:
android:background="?attr/selectableItemBackground"
You can get the ripple effect on a TextView for example. (you can also try settings it on the android:foreground if background doesn't fit your use. If you do, the background has to be a solid color or @null though, so keep that in mind or using foreground won't work.)

Have fun !

Thursday, November 12, 2015

Here's how to center a background image

To customize background image scaling create a resource like this:
<xml version="1.0" encoding="utf-8"?>
 <xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/my_bg_asset" />
Then it will be centered in the view if used as background. There are also other flags:http://developer.android.com/guide/topics/resources/drawable-resource.html

Android Studio (and intelliJ) supports Sublime style multiple selections

Here is how they work:
  1. Add/remove a selection:
    1. Alt + Shift + Mouse Click
  2. Select/unselect the next occurrence:
    1. Alt + J / Shift + Alt + J (Ctrl + G / Shift + Ctrl +G) for Mac OS X)
  3. Select all occurrences:
    1. Shift + Ctrl + Alt + J (Ctrl + Cmd + G for Mac OS X)
  4. Clone caret above/below
    1. The shortcuts are not mapped by default. Simply open studio's preferences and search for clone in the keyboard mappings. I personally mapped it to Ctrl + Alt + Cmd + up and down
  5. Remove all selections:
    1. Esc
Multiple selections work nicely together with features like Code completion,Select word at caretJoin linesCopy/paste, and the others. Here’s a little demo:

Big thanks to  Andrey Cheptsov for this awesome bit of news! (source)

Saturday, September 5, 2015

How to hide the TSB(nav bar - back, home, recent) no matter what version of Android you are using

You can do it with this:
   public void HideNavbar() {
        if(Build.VERSION.SDK_INT < 19) //API 18 or below
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else { //API 19 or above
            View decorView = getWindow().getDecorView(); 
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                          | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

Thursday, April 17, 2014

How to start an Activity from within a Service

First, if you plan on doing this, please read this bit about interrupting the user: http://developer.android.com/guide/practices/seamlessness.html#interrupt

The trick is to set the intent's flag for FLAG_ACTIVITY_NEW_TASK. Also, as you probably already know, all life cycle methods like onCreate are executed on the UI thread which is required to start an Activity.

Here's an example:
  public void onCreate(){
    super.onCreate();
    Intent intent = new Intent("com.example.email.compose");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This is necessary!
    intent.putExtra("Contacts_ID", 0);
    startActivity(intent);
  }

Tuesday, July 27, 2010

How to add leading and trailing spaces in string.xml

Even if you use string formatting sometimes you still need white spaces at the beginning or the end. On these cases neither escaping with \ nor using &nbsp; or the xml:space attribute helps.
You must use the UTF character \u0020 for white spaces. Just type this succession of characters and it will be interpreted as a space. The \u part tells android that the following number represents the character in the UTF table.

Wednesday, July 8, 2009

How to protect part of a public method from being executed by others

I recently had to face this problem while I was modifying the android framework to add a security feature and I didn't want 3rd party apps to be able to mess with it.

To solve this problem I used enforceCallingOrSelfPermission.

Anything you do in your code until you call that method will be runnable by anyone who can access your method but passed this point it will through a SecurityException If neither you nor the calling process has been granted a particular permission.

Here's what it looks like:
void myMethod() {
// my code every one has access to...
myContext.enforceCallingOrSelfPermission(myPermission, myMessage);
// my super code that noone else can execute
}