Android

Android how to create Progress dialog

 private ProgressDialog mProgressDialog;


    private void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage(getString(R.string.loading));
            mProgressDialog.setIndeterminate(true);
        }

        mProgressDialog.show();
    }

    private void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.hide();
        }
    }

 

By bm on June 3, 2016 | Android | A comment?

Handle orientation changes automatically

Set android:configChanges=”orientation|screenSize” to the Activity property in Android manifest

eg:

However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

ref : https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
By bm on June 1, 2016 | Android | A comment?

Android Open Activity

Android Open Activity from FirstActivity

    Intent intent = new Intent(this, SecondActivity.class);
   
    intent.putExtra(EXTRA_MESSAGE, someMessage);

    startActivity(intent);

If you are trying inside onclick listener better to use Activity_Name.this insted of this

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

No data to send

startActivity(new Intent(FirstActivity.this, SecondActivity.class));

 

Receiving the Intent from Second Activity

Intent intent = getIntent();

String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);

 

 

ref: https://developer.android.com/training/basics/firstapp/starting-activity.html
By bm on May 31, 2016 | Android | A comment?

Google Drive Android API – upload file to existing folder

Refer the google demo hosted in Github
for getting imageUri you can had code the image url or you can use the image picker ref: https://wiki.workassis.com/android-create-a-file-picker/

Details of BaseDemoActivity you can find in the google samples in github
for getting existing folder id (0B_cMuo4-XwcAZ3IzSG1jajFlWk0) open folder from google drive in the address bar it will show like this https://drive.google.com/drive/folders/0B_cMuo4-XwcAZ3IzSG1jajFlWk0

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class UploadImageFromGalleryActivity extends BaseDemoActivity {

    private static final String TAG = "CreateFileActivity";

    private Uri imageUri;

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(driveContentsCallback);
    }

    final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {

        @Override
        public void onResult(DriveApi.DriveContentsResult result) {

            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create new file contents");
                return;
            }

            final DriveContents driveContents = result.getDriveContents();

            // Perform I/O off the UI thread.
            new Thread() {
                @Override
                public void run() {

                    OutputStream outputStream = driveContents.getOutputStream();
                    try {
                        //getting image from the local storage
                        InputStream inputStream = getContentResolver().openInputStream(imageUri);

                        if (inputStream != null) {
                            byte[] data = new byte[1024];
                            while (inputStream.read(data) != -1) {
                                //Reading data from local storage and writing to google drive 
                                outputStream.write(data);
                            }
                            inputStream.close();
                        }

                        outputStream.close();
                    } catch (IOException e) {
                        Log.e(TAG, e.getMessage());
                    }

                    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                            .setTitle("New file")
                            .setMimeType("image/jpg")
                            .setStarred(true).build();


                    DriveApi.DriveIdResult exFolderResult = Drive.DriveApi
                            .fetchDriveId(getGoogleApiClient(), "0B_cMuo4-XwcAZ3IzSG1jajFlWk0") //existing folder id = 0B_cMuo4-XwcAZ3IzSG1jajFlWk0
                            .await();

                    if (!exFolderResult.getStatus().isSuccess()) {
                        showMessage("Cannot find DriveId. Are you authorized to view this file?");
                        return;
                    }

                    DriveId driveId = exFolderResult.getDriveId();
                    //showMessage("driveid" + driveId.getResourceId());
                    final DriveFolder folder = driveId.asDriveFolder();


                    // create a file on root folder
                    folder.createFile(getGoogleApiClient(), changeSet, driveContents)
                            .setResultCallback(fileCallback);

                }
            }.start();

        }
    };

    final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new ResultCallback<DriveFolder.DriveFileResult>() {
        @Override
        public void onResult(DriveFolder.DriveFileResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create the file");
                return;
            }
            showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
        }
    };

}

 

By bm on May 23, 2016 | Android | A comment?

Android Create a file picker

public static final int REQUEST_PICK_IMAGE = 1000;
btnOpenGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, REQUEST_PICK_IMAGE);

            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK) {
            switch (requestCode) {

                case REQUEST_PICK_IMAGE:
                    imageUri = data.getData();

                    processImage( imageUri  );

                    break;

                default:
                    break;
            }
        }
    }

 

By bm on | Android | A comment?

Android how to set a ripple effect

<TextView
    .
    .
    android:background="?attr/selectableItemBackgroundBorderless" android:clickable="true" /> 

<ImageView
    .
    .
    .
    android:background="?attr/selectableItemBackgroundBorderless" android:clickable="true" />

 

Customize Touch Feedback

Touch feedback in material design provides an instantaneous visual confirmation at the point of contact when users interact with UI elements. The default touch feedback animations for buttons use the new RippleDrawable class, which transitions between different states with a ripple effect.

In most cases, you should apply this functionality in your view XML by specifying the view background as:

  • ?android:attr/selectableItemBackground for a bounded ripple.
  • ?android:attr/selectableItemBackgroundBorderless for a ripple that extends beyond the view. It will be drawn upon, and bounded by, the nearest parent of the view with a non-null background.

Note: selectableItemBackgroundBorderless is a new attribute introduced in API level 21.

Alternatively, you can define a RippleDrawable as an XML resource using the ripple element.

You can assign a color to RippleDrawable objects. To change the default touch feedback color, use the theme’s android:colorControlHighlight attribute.

 

Ref : http://developer.android.com/training/material/animations.html

By bm on May 2, 2016 | Android XML | A comment?