Increase the volume!!!

If you're running a linux system and having real trouble in hearing the sound perfectly, maybe you wanted to increase the sound beyond what the system allows (i.e more than 100%). This is where, the "setvolume.py" comes in :)

You use the script like so:
First of all, make the script executable with the command: "chmod +x setvolume.py"
Then "setvolume 70" to set the volume to 70% or "setvolume 150" to set the volume to 150%.

Besides, if you're using a terminal, you know, grabbing the mouse is a pain sometimes. You can use this script to change the system volume through the terminal (and possibly making it globally executable from any location) :D

Why wait, grab the script from here: https://github.com/sazid/setvolume

Using PreferenceScreen with a transparent background

If you've ever tried to use a PreferenceScreen inside a PreferenceFragment which has a transparent background (or it's parent activity has transparent background), you should have noticed that, instead of replacing the fragment, PreferenceScreen adds the fragment which makes it overlap with the previous fragment (i.e top fragment's content is visible on top of the fragment just below it). So, here's a quick solution for this issue:


Go "Home" onNewIntent()!

After fiddling around for quite a long time, I've finally figured out how to pop the back stack or go to the top fragment or the top activity when the home button is pressed (provided that you are inside the app).


Tip: Content Provider

Do not close the database connection inside a ContentProvider as it's managed by the system itself. Otherwise, you can choose to crash the app :p

Creating square layouts in RecyclerView items

There are a couple of different ways to create a square child layout in RecyclerView. One way is, using the View.post(...) method where, you set the height of the view to it's width. This involves, laying out the view first and then setting their width/height which in turn incurs a bit of performance. Besides, run-time changes of child views inside RecyclerView doesn't work that well (as of writing this) and cause stuttering scrolling. So, a much more simpler way is to use a SquareFrameLayout! Copy the code (or grab the file from the gist) below to your project and you're all set!


Use the layout like:

<android.sz.common.widgets.SquareFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your views -->

</android.sz.common.widgets.SquareFrameLayout>

Launch google quick search box

Here's a little helper function to launch the Google quick search box in android ;)
NOTE: This may stop working in a future update to the Google app, be sure to test it out first.
/**
 * Launch the Google quick search box overlay activity, otherwise, fall back to
 * launching the device default assist app.
 */
private void launchQuickSearch(Context context) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    try {
        // Explicit intent to launch the google quick search box
        intent.setClassName("com.google.android.googlequicksearchbox",
                "com.google.android.search.queryentry.QueryEntryActivity");

        // If there's no associated activity, then try launching any other available app
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            intent.setAction(Intent.ACTION_ASSIST);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            }
        }
    } catch (ActivityNotFoundException e) {
        Log.e(TAG, "Google search activity not found");
        Log.i(TAG, "Trying to launch default assist app.");
        intent = new Intent(Intent.ACTION_ASSIST);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
}


@Override
public void onClick(View v) {
    launchQuickSearch(MainActivity.this);
    // launchQuickSearch(v.getContext());
}

Life tip

Learn to learn :)

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...