Create QR code image from String

public Bitmap createBitmap(String str, Context context) throws WriterException {

    int wd = UiUtil.getPixelFromDp(100);
    int pinkColor = context.getResources().getColor(R.color.black);
    int whiteColor = context.getResources().getColor(R.color.white);

    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(1);

    hints.put(EncodeHintType.MARGIN, 0);


    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, wd, wd, hints);
    } catch (IllegalArgumentException iae) {
        // TODO Unsupported format
        return null;
    }

    //Change color pixel
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? pinkColor : whiteColor;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, wd, 0, 0, w, h);

    return bitmap;
}
Advertisements

Layout of Parallax Toolbar + Pull to refresh list view

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@color/white"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax">

                <include layout="@layout/curator_cover" />

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/mm_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

                <ImageView
                    android:id="@+id/ivSearch"
                    android:layout_width="@dimen/badge_menu_item_size"
                    android:layout_height="@dimen/badge_menu_item_size"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:gravity="center"
                    android:scaleType="fitCenter"
                    android:src="@drawable/search_wht"
                    tools:ignore="ContentDescription" />
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPost"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>