Sunday, 30 April 2017

Android: Capture screen as snapshot

This blog post is regarding how to capture your android screen as snapshot. This may be one of your requirements during the course of your application development. It is very simple to implement this functionality as follows:

1. On the click of your button or at the event at which you want to capture your screen just use the below method:

void onYourButtonClick() {
        try {
            LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
            // pass your main layout id
            File screenshot = storeScreenShot(layout, "Screen_shot_" + new Date().getTime() + ".jpg");
            // you need to first save the captured image
        } catch (Exception ex) {
            ex.getMessage();
        }
    }

2. Method to save your file:

public static Bitmap getScreenShot(View view) {
        View screenView = view;
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public static File storeScreenShot(View v, String fileName) {
        Bitmap bm = getScreenShot(v);
        final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SC";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.d("TAG", "Oops! Failed to create directory");
            }
        }
        File file = new File(dirPath, fileName);
        try {
            FileOutputStream fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

3. So the file (screenshot) created in step one is your captured screen image file.
You can use this file as per your needs.


That's all.
Share and comment if any issues.

Sunday, 9 April 2017

Android: Snackbar example

Snackbar is one of the most interesting component introduced in Material Design. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them.
Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.
They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity.
Snackbars can contain an action which is set via setAction(CharSequence, View.OnClickListener).

Also to be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via addCallback(BaseCallback).


Implementing Snackbar is very simple which can be done as follows:

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "Snackbar is visible.", Snackbar.LENGTH_LONG)
        .setAction("DONE", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Snackbar dismissed.", Snackbar.LENGTH_SHORT);
                snackbar1.show();
            }
        });

snackbar.show();


One point to be noted here is that, while making a Snackbar I have used the CoordinatorLayout as the parent layout for Snackbar. Due to this CoordinatorLayout our Snackbar has the ability to be dismissed when swiped.


Tha's all.
Share and comment if any issues.