Open top menu

 How to used SanckBar in Android ?

They automatically fade out after enough time similar to a toast. The 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 device. You are make custom Material Design SanckBar, and set Background color programmatically.
 
You can also mention a callback intraction method using setAction() method.


snackbar.setAction("OK", new View.OnClickListener() {
    @Override
   
public void onClick(View v) {
      
     //code here…  
    }
});

And you can also mention Snackbar Auto dismiss duration.

snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);

snackbar.setDuration(Snackbar.LENGTH_LONG);

snackbar.setDuration(Snackbar.LENGTH_SHORT);
 
activity_main.xml
……………………………………………………………………………………………………………………………..
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:id="@+id/layout" >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!" />



</RelativeLayout>


……………………………………………………………………………………………………………………………..



MainActivity.java.

……………………………………………………………………………………………………………………………..

 


public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        RelativeLayout layout=(RelativeLayout)findViewById(R.id.layout) ;

        final Snackbar snackbar = Snackbar.make(layout, "Welcome to AndroidBeginnerpoint", Snackbar.LENGTH_LONG);

        snackbar.setAction("OK", new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                snackbar.dismiss();

            }

        });



        snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);

        snackbar.show();

    }

}

……………………………………………………………………………………………………………………………..



Read more

 How to delete Media File Folder in Anadroid?


Try to this method..


public void DeleteMediaFile(Uri fileUri){

    File file = new File(fileUri.getPath());

    file.delete();

    if(file.exists()){

        try {

            file.getCanonicalFile().delete();

            if(file.exists()){

               getApplicationContext().deleteFile(file.getName());

             }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}
 }


.......................................................................................... 
In this class  camera capture image and then image is change in bitmap and delete this file.
..........................................................................................



public class MainActivity extends AppCompatActivity {
 
private Uri fileUri;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String IMAGE_DIRECTORY_NAME = "Camera";
@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Button takepic=(Button)findViewById(R.id.button);

    takepic.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

         fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

         intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

         startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);


         }

    });

    gridView=(GridView)findViewById(R.id.gridView);

}
 
public Uri getOutputMediaFileUri(int type) {

    return Uri.fromFile(getOutputMediaFile(type));

}
 
private static File getOutputMediaFile(int type) {



    // External sdcard location

    File mediaStorageDir = new File(

                 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

              IMAGE_DIRECTORY_NAME);



 

    // Create a media file name

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",File mediaFile;

    if (type == MEDIA_TYPE_IMAGE) {

        mediaFile = new File(mediaStorageDir.getPath() + File.separator

                + "IMG_" + timeStamp + ".bmp");

    } else {

        return null;

    }

    return mediaFile;// returen media  file

}
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {

        if (resultCode == RESULT_OK) {
        

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
 
      // whene delete fileUri folder from android device…
 
         DeleteMediaFile(fileUri);
 
        }
    }
}
 
public void DeleteMediaFile(Uri fileUri){

    File file = new File(fileUri.getPath());

    file.delete();

    if(file.exists()){

        try {

            file.getCanonicalFile().delete();

            if(file.exists()){

               getApplicationContext().deleteFile(file.getName());

             }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}
 }
 
 
 
}
Read more

Show Capture Image in GridView Dynamically in Android?

In this blog we will implement how to take picture via Camera and how to show capture image in GridView (Widget). Explain Full Activity…

Selecteditem.xml.
…………………………………………………………………………………………
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">





    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=" Camra "

        android:id="@+id/button"

        android:drawableBottom="@drawable/camra"

        android:layout_margin="20dp"

        />

    <GridView

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:id="@+id/gridView"

        android:numColumns="6"

        android:layout_margin="6dp"/>





</LinearLayout>
……………………………………………………………………………………………………

Image_cover.xml.
……………………………………………………………………………………………………..
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" android:padding="15dp">

    <solid android:color="#ffffff"/>



    <corners

        android:bottomRightRadius="0dp"

        android:bottomLeftRadius="0dp"

        android:topLeftRadius="0dp"

        android:topRightRadius="0dp"/>



    <stroke android:width="2dip" android:color="#1084ba" />

</shape>
…………………………………………………………………………………………………………………..
activity_main.xml
………………………………………………………………………………………………….
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">





    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=" Camera "

        android:id="@+id/button"

        android:drawableBottom="@drawable/camra"

        android:layout_margin="20dp"

        />

    <GridView

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:id="@+id/gridView"

        android:numColumns="6"

        android:layout_margin="6dp"/>





</LinearLayout>
………………………………………………………………………………………………….
ColorItem.java
………………………………………………………………………………………………
public class ColorItem {

    private String ColorName;

    private Bitmap bitmap;



    public Bitmap getBitmap() {

        return bitmap;

    }



    public void setBitmap(Bitmap bitmap) {

        this.bitmap = bitmap;

    }



    public String getColorName() {

        return ColorName;

    }



    public void setColorName(String colorName) {

        ColorName = colorName;

    }

}

………………………………………………………………………………………………

 
SelectedImageAdapter.java
……………………………………………………………………………………………….
 



import android.content.Context;

import android.graphics.Color;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



import java.util.ArrayList;

import java.util.Locale;


 public class SelectedImageAdapter extends BaseAdapter {

    private Context context;



    ArrayList myList = new ArrayList();

    private ArrayList<ColorItem> arraylist;

    int resorce;



    public SelectedImageAdapter(Context context, ArrayList<ColorItem> myList) {

        this.context = context;

        this.myList = myList;

        this.arraylist = new ArrayList<ColorItem>();

        this.arraylist.addAll(myList);



    }



    @Override

    public int getCount() {

        return myList.size();

    }



    @Override

    public ColorItem getItem(int position) {



        return (ColorItem) myList.get(position);

    }



    @Override

    public long getItemId(int position) {

        return 0;

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        ColorItem speciitem = getItem(position);

        final ViewHolder holder;

        if (convertView == null) {

            LayoutInflater infalInflater = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = infalInflater.inflate(R.layout.selected_item, null);

        }

        holder = new ViewHolder();

        try {



            holder.colors = (ImageView) convertView.findViewById(R.id.colors);

            holder.colorsname = (TextView)    
convertView.findViewById(R.id.color_text);

            holder.colors.setImageBitmap(speciitem.getBitmap());

            holder.colorsname.setText(speciitem.getColorName());





        } catch (Exception e) {

            e.printStackTrace();

        }

        convertView.setTag(holder);

        return convertView;

    }



    static class ViewHolder {



        TextView colorsname, bagcount;

        ImageView colors;

    }



}

……………………………………………………………………………………………..

MainActivity.java.
……………………………………………………………………………………………….
 

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Environment;

import android.provider.MediaStore;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.GridView;

import android.widget.Toast;



import java.io.File;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Locale;



public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

    public static final int MEDIA_TYPE_IMAGE = 1;

    GridView gridView;

    // directory name to store captured images and videos

    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

    Bitmap decodedByte;

    private Uri fileUri;

    int count=0;

    ArrayList<ColorItem>   CamraTakeImgList = new ArrayList<>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button takepic=(Button)findViewById(R.id.button);

        takepic.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                captureImage();

            }

        });

        gridView=(GridView)findViewById(R.id.gridView);

    }

    private void captureImage() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);



    }

    public Uri getOutputMediaFileUri(int type) {

        return Uri.fromFile(getOutputMediaFile(type));

    }



    private static File getOutputMediaFile(int type) {



        // External sdcard location

        File mediaStorageDir = new File(

                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

                IMAGE_DIRECTORY_NAME);



        // Create the storage directory if it does not exist

        if (!mediaStorageDir.exists()) {

            if (!mediaStorageDir.mkdirs()) {

                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "

                        + IMAGE_DIRECTORY_NAME + " directory");

                return null;

            }

        }



        // Create a media file name

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",

                Locale.getDefault()).format(new Date());

        File mediaFile;

        if (type == MEDIA_TYPE_IMAGE) {

            mediaFile = new File(mediaStorageDir.getPath() + File.separator

                    + "IMG_" + timeStamp + ".jpg");

        } else {

            return null;

        }

        return mediaFile;

    }



    private void previewCapturedImage() {

        try {

            count++;

            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inSampleSize = 8;

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);

            ColorItem select = new ColorItem();

            select.setBitmap(bitmap);

            select.setColorName("Pic"+" "+count);

            CamraTakeImgList.add(select);

            SelectedImageAdapter seletimg = new SelectedImageAdapter(MainActivity.this, CamraTakeImgList);

            gridView.setAdapter(seletimg);

        } catch (NullPointerException e) {

            e.printStackTrace();

        }

    }

    // Find Device Bluetooth is Enable or Not.......................................................

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {

            if (resultCode == RESULT_OK) {

                previewCapturedImage();

            }

        }

    }

}
 

…………………………………………………………………………………………….......................


Read more



How to change Background color Bitmap image in Android?



public Bitmap EreasBackground(Bitmap c) {
    Bitmap imageWithBG = Bitmap.createBitmap(c.getWidth(), c.getHeight(), c.getConfig());
    imageWithBG.eraseColor(Color.
WHITE);  // set its background to white, or whatever color you want
   
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
   
canvas.drawBitmap(c, 0f, 0f, null); // draw old image on the background
   
c.recycle();  // clear out old image
   
return imageWithBG;
}
Read more

How to convert image to String and String to Image ?


In this blog we will explain how to convert Image to String Base64 and Base64 String encode to Image . Create Bitmap and set the image  and you have image convert in byte array.
String StrImag = "";
 
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);

StrImag = bitmapToBase64(bm);
 
 
//canvert Bitmap image to string................................ 

private String bitmapToBase64(Bitmap bitmap) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);

}
 

convert Image to String Base64 and Base64 String encode to Image.
byte[] decodedString = Base64.decode(StrImag, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

holder.imgDamage.setImageBitmap(decodedByte);
Read more