Android ripple drawable

Ripple drawable - a drawable that shows a ripple effect in response to state changes.

Rounded corners rectangle


Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="@drawable/my_ripple"
        android:clickable="true" />

</FrameLayout>

my_ripple:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#e53935">
    <item>
        <shape>
            <solid android:color="#777" />
            <corners android:radius="20dp" />
        </shape>
    </item>
</ripple>

Rounded corners rectangle 2

Android handling runtime changes for multithreading

Example with server request

On configuration change, response is delivered to correct activity (or fragment):

Orientation change example

When user switched to other application, response will be cached, and when user returns back, delivered to correct activity (or fragment):

Application is not visible example

To implement this kind of logic, we just need to use publish-subscribe pattern (with Otto for example) and a little bit of magic:

Android attachable dialogfragment

Attaching dialog to view, or specific position


Call :
public class MainActivity extends Activity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        imageView = (ImageView) findViewById(R.id.main_activity_image_view);
        findViewById(R.id.main_activity_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] loc = Utils.getCenterViewLocation(imageView, MainActivity.this);
                CustomDialog.attachTo(loc).show(getFragmentManager(), CustomDialog.class.getName());
            }
        });
    }
}

View location method :
public static int[] getCenterViewLocation(View view, Activity activity) {
    Rect windowRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowRect);
    int[] loc = new int[2];
    view.getLocationOnScreen(loc);
    loc[0] = loc[0] + view.getWidth() / 2;
    loc[1] = loc[1] + view.getHeight() / 2 - windowRect.top;
    return loc;
}