Friday, 11 October 2019

Prohibit Screenshot in your android App

Prohibit Screenshot in your android App.


If you want to stop screenshot in all screen of your app then you need to create a BaseActivity and extend this activity to all your activity in which you want to stop screenshot.
Add below mentioned line in onCreate method of BaseActivity.
--------------------------- OR -----------------------------
If you want to prohibit screenshot in particular screen then add below mentioned line if specific activity.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, 
WindowManager.LayoutParams.FLAG_SECURE);

Friday, 22 April 2016

Gson Json to Arraylist

  • Copy below mentioned code and paste in GSONUTILS file
    public static <T> ArrayList<T> fromJsontoArrayList(
    String string, Class<T> model)
    {
    
     Gson gson = new GsonBuilder().create();
     T gfromat = null;
     ArrayList<T> localArrayList = new ArrayList<T>();
     try
     {
    
     JSONArray jsonInner = new JSONArray(string);
     int i = 0;
     while (i < jsonInner.length())
     {
     gfromat = gson.fromJson(jsonInner.get(i).toString(), model);
     localArrayList.add(gfromat);
     i++;
    
     }
    
     }
     catch (Exception e)
     {
      Log.e("GsonUtils","Amit exception["+e.getMessage()+"]");
     }
    
     return localArrayList;
    }
  • call this method like this fromJsontoArrayList("jsonstring",beanclass.class)

Friday, 11 March 2016

Convert file size to readable format

[KEY : convert file size, readable file size, convert in]

Convert file size to readable format

  1. Copy below method
    public static String convertSize(long size)
    {
    
    String sizeIn[] = new String[] { "KB", "MB", "GB", "TB" };
    String ret = "";
    if (size >= 1024)
    {
     int devider = 1024;
     int counter = 0;
     while (size / devider > 1024)
     {
      counter++;
      devider *= 1024;
      
     }
     ret = String.format("%.2f", (double) size / devider) + " " + sizeIn[counter];
     
     return ret;
    } else
    {
     ret = size + " bytes";
    }
    return ret;
    }
    
  2. Get file length and call above method
    convertSize(new File("file.txt").length())

Thursday, 11 June 2015

Merge Two Displayer (Universal Image Loader Android)

Merge Two Displayer (Universal Image Loader Android)

  1. Create a class named TwoDisplayer
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.ColorFilter;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PixelFormat;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Shader;
    import android.graphics.drawable.Drawable;
    import android.view.View;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.DecelerateInterpolator;
    import android.widget.ImageView;
    
    import com.nostra13.universalimageloader.core.assist.LoadedFrom;
    import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
    import com.nostra13.universalimageloader.core.imageaware.ImageAware;
    import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
    
    
    
    public class TwoDisplayer implements BitmapDisplayer
    {
     protected int cornerRadius;
     protected int margin;
    
     private int durationMillis;
     private boolean animateFromNetwork;
     private boolean animateFromDisk;
     private boolean animateFromMemory;
    
    
     public TwoDisplayer(int cornerRadiusPixels, int fadeInTime){
    
      setCorner(cornerRadiusPixels,0,"");
      setFader(fadeInTime,true,true,true);
    
     }
     public void setCorner(int cornerRadiusPixels, int marginPixels,String dummy) {
      this.cornerRadius = cornerRadiusPixels;
      this.margin = marginPixels;
     }
     public void setFader(int durationMillis, boolean animateFromNetwork,
     boolean animateFromDisk,boolean animateFromMemory) {
      this.durationMillis = durationMillis;
      this.animateFromNetwork = animateFromNetwork;
      this.animateFromDisk = animateFromDisk;
      this.animateFromMemory = animateFromMemory;
     }
     @Override
     public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom)
     {
    
      if (!(imageAware instanceof ImageViewAware)) {
       throw new IllegalArgumentException("ImageAware should
     wrap ImageView. ImageViewAware is expected.");
      }
      imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin));
      if ((animateFromNetwork && loadedFrom == LoadedFrom.NETWORK) || (animateFromDisk && loadedFrom == LoadedFrom.DISC_CACHE) ||
        (animateFromMemory && loadedFrom == LoadedFrom.MEMORY_CACHE)) {
       animate(imageAware.getWrappedView(), durationMillis);
      }
     }
    
    
     /**
      * Animates {@link ImageView} with "fade-in" effect
      *
      * @param imageView {@link ImageView} which display image in
      * @param durationMillis The length of the animation in milliseconds
      */
     public static void animate(View imageView, int durationMillis) {
      if (imageView != null) {
       AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
       fadeImage.setDuration(durationMillis);
       fadeImage.setInterpolator(new DecelerateInterpolator());
       imageView.startAnimation(fadeImage);
      }
     }
    
     //=========================RoundedDrawable=======================
     public static class RoundedDrawable extends Drawable {
      protected final float cornerRadius;
      protected final int margin;
      protected final RectF mRect = new RectF(),
        mBitmapRect;
      protected final BitmapShader bitmapShader;
      protected final Paint paint;
      public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
       this.cornerRadius = cornerRadius;
       this.margin = margin;
       bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
       mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);
       paint = new Paint();
       paint.setAntiAlias(true);
       paint.setShader(bitmapShader);
      }
      @Override
      protected void onBoundsChange(Rect bounds) {
       super.onBoundsChange(bounds);
       mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);
       // Resize the original bitmap to fit the new bound
       Matrix shaderMatrix = new Matrix();
       shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
       bitmapShader.setLocalMatrix(shaderMatrix);
      }
      @Override
      public void draw(Canvas canvas) {
       canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
      }
      @Override
      public int getOpacity() {
       return PixelFormat.TRANSLUCENT;
      }
      @Override
      public void setAlpha(int alpha) {
       paint.setAlpha(alpha);
      }
      @Override
      public void setColorFilter(ColorFilter cf) {
       paint.setColorFilter(cf);
      }
     }
    }
    
    
  2. Copy and paste above code
  3. Call from activity / adapter as below
    defaultOptions = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(false).imageScaleType(ImageScaleType.EXACTLY).resetViewBeforeLoading(true)
        .displayer(new TwoDisplayer(10, 1000))
    

Monday, 14 July 2014

Dynamically set screen orientation android

Set following code before setting activity layout as follow

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(r.layout.yourlayout);


Sunday, 13 July 2014

Remove / Disable activity header android

Just before setting contentview add the following line

requestWindowFeature(Window.FEATURE_NO_TITLE);


Thursday, 10 July 2014

Slide ViewPager on button click

You have changed ViewPager's page by sliding it. You can also change page by click on next and previous button.

  1. Add two button so called NEXT and PREV in xml.
  2. Instantiate these buttons in your activity. And create a global counter as follow

    int globalPosition = 0;

  3. Now set next click listener. as follow

    // checking if pager can scroll or not
    boolean can = mPager.canScrollHorizontally(1);
    
    if(can)
    {
     mPager.setCurrentItem(++globalPosition,true);
    }
    
    

  4. Set previous button click listener

    boolean can = mPager.canScrollHorizontally(-1);
    if(can)
    {
     mPager.setCurrentItem(--globalPosition,true);
    }
    

  5. Dont forget to update globalPosition on ViewPager page sliding.

    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
     @Override
     public void onPageSelected(int position) {
      globalPosition = position;
     }
    });