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;
     }
    });
    


Sunday, 6 July 2014

Android ViewPager

ViewPager is nothing but horizontal scroller for entire screens.

  1. Lets begin from xml. Add ViewPager in your xml file. You can copy following code.
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/your_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
  2. Now move to java file(Activity) create instance for ViewPager added in xml. Make sure this Activity extending FragmentActivity not Activity
    mPager = (ViewPager) findViewById(R.id.your_pager);
  3. Now create adapter for pager, create pager instance
    ViewPager mPager;
    PagerAdapter mPagerAdapter;
    public static String [] pageItem = new String[]{"Page 1","Page 2","Page 3"};
     
  4. Initialize adapter object and set adapter to ViewPager.
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
  5. Create slider class which should extend FragmentStatePagerAdapter or FragmentPagerAdapter.
    private class ScreenSlidePagerAdapter extends 
        FragmentStatePagerAdapter 
    {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
     super(fm);
    }
    
    @Override
    public Fragment getItem(int position) {
            //
     return new MyFragment(position);
    }
    
    @Override
    public int getCount() {
     return pageItem.length;
    }
    }
  6. Create fragment which will display each screen. It will be slided horizontally.
    public class MyFragment extends Fragment {
     TextView textView;
     int position = 0;
     public MyFragment(int position)
     {
      this.position = position;
     }
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            ViewGroup rootView = (ViewGroup) inflater.inflate(
                    R.layout.my_screen_fragment, container, false);
            
            textView = (TextView)rootView.findViewById(R.id.my_page_tv);
            textView.setText(activityClassObject.pageItem[position]);
            return rootView;
        }
    }
    

Tuesday, 24 June 2014

[KEY : ANDROID CUSTOM CHECKBOX, CUSTOM CHECKBOX]

Custom Checkbox Android

How to create custom checkbox. Applying stylish checkbox Android

  1. Take two images for both states checked and unchecked. In png format.
  2. Create checkbox in xml.
  3. Create new xml file assign appropriate name like "checkbox_button.xml"

    <?xml version="1.0" encoding="utf-8"?>
    <selector 
    xmlns:android="http://schemas.android.com/apk/res/android">
      <!--checked image-->
        <item android:drawable="@drawable/checkbox_checked" 
        android:state_checked="true"/>
      <!--unchecked image-->
        <item android:drawable="@drawable/checkbox" 
        android:state_checked="false"/>
    
    </selector>

  4. Add button property in check box in xml

    android:button="@drawable/checkbox_button"


Monday, 23 June 2014

[KEY : ANDROID FRAGMENT, ACTIVITY, ADD FRAGMENT TO ACTIVITY]

Add Fragment to Activity

  1. Create simple activity with xml.
  2. Go to xml file add framelayout and assign id to it as follow:

     <FrameLayout
            android:id="@+id/your_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />

  3. Create java class with name like "MyFirstFragment.java" and extend fragment.
  4. Go to activity add the following code :

    Fragment myFragment = new MyFirstFragment();
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fTransaction = fragmentManager
       .beginTransaction()
       .replace(R.id.your_frame_layout, bodyFragment)
       .addToBackStack("myprofile");
       
     fTransaction.commit();