Sunday 10 January 2016

Compare two array of different length



Here is the code to compare two json array and add to one
conside
notiarray is a json array with json length 10
jarray is a json array with json length 5

boolean found;  
int i=0;
for ( i = 0; i < notiarray.length(); i++)
{
  found = false;
  for (int k = 0; k < jarray.length(); k++){
      if(notiarray.getJSONObject(i).getString("notification_id").equalsIgnoreCase(jarray.getJSONObject(k).getString("key")))
      {
        System.out.println(jarray.getJSONObject(k).getString("key")+ " is equal to " + notiarray.getJSONObject(i).getString("notification_id"));
        found = true;
        break;
      }
  }
  if (!found){
 
      messagelist.add(mn);
  }

}

Friday 27 November 2015

Native Page splitter like Newspaper and Magazine for android

PageSplitter.java class

package com.sharathyadhav.utils;
import android.text.DynamicLayout;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class PageSplitter {
    private final int pageWidth;
    private final int pageHeight;
    private final float lineSpacingMultiplier;
    private final float lineSpacingExtra;
    private final List<CharSequence> pages = new ArrayList<CharSequence>();
    private SpannableStringBuilder mSpannableStringBuilder = new SpannableStringBuilder();

    public PageSplitter(int pageWidth, int pageHeight, float lineSpacingMultiplier, float lineSpacingExtra) {
        this.pageWidth = pageWidth;
        this.pageHeight = pageHeight;
        this.lineSpacingMultiplier = lineSpacingMultiplier;
        this.lineSpacingExtra = lineSpacingExtra;
    }

    public void append(CharSequence charSequence) {
        mSpannableStringBuilder.append(charSequence);
    }

    public void split(TextPaint textPaint) {
        DynamicLayout staticLayout = new DynamicLayout(
                mSpannableStringBuilder,
                textPaint,
                pageWidth,
               null,
                lineSpacingMultiplier,
                lineSpacingExtra,
                true
        );
        int startLine = 0;

        while(startLine < staticLayout.getLineCount()) {
        // for(startLine =0; startLine< staticLayout.getLineCount(); startLine++) {
            int startLineTop = staticLayout.getLineTop(startLine);
            int endLine = staticLayout.getLineForVertical(startLineTop + pageHeight);
            int endLineBottom = staticLayout.getLineBottom(endLine);
            int lastFullyVisibleLine;
            if(endLineBottom > startLineTop + pageHeight){
                lastFullyVisibleLine = endLine - 1;
            }
            else{
                lastFullyVisibleLine = endLine;
                }
            int startOffset = staticLayout.getLineStart(startLine);
            Log.e("log", "######## :lastFullyVisibleLine::::   " + lastFullyVisibleLine);
            int endOffset = staticLayout.getLineEnd(lastFullyVisibleLine);
            pages.add(mSpannableStringBuilder.subSequence(startOffset, endOffset));
            startLine = lastFullyVisibleLine + 1;
        }
    }

    public List<CharSequence> getPages() {
   
    
        return pages;
    }

}



Wednesday 14 October 2015

Handling touch events in view pager to hide the action bar or tool bar android

Hi guys
Just had a rough time dealing with the above topic and at last found a prompt solution

check out

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
    // Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
boolean gone = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);

    viewpager.setOnTouchListener(new View.OnTouchListener() {


        private float pointX;
        private float pointY;
        private int tolerance = 50;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE: 
                return false;
            case MotionEvent.ACTION_DOWN:
                pointX = event.getX();
                pointY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                if(sameX && sameY){
                        //The user "clicked" certain point in the screen or just returned to the same position an raised the finger

                    if(gone == false){
                        bWallpaperButton.setVisibility(View.GONE);
                        bDownloadButton.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        bWallpaperButton.setVisibility(View.VISIBLE);
                        bDownloadButton.setVisibility(View.VISIBLE);
                        gone = false;
                    }
                }
            }
            return false;

        }
    });


}

}

Friday 9 October 2015

View pager direction right to left-arabic

Guys , I just happened to work with a arabic project in which i have paging and as well i have to move from right to left so each time i do paging i have to add element to the left side of the viewpager (ie )i have to add to the zeroth element and even if i to add i have to setcurrentitem to the element, if i do so ,while doing paging it slides to the set page which is worst animation u could say ,

after thnking and exploring i just happened to see the magic line which helped me a lot so , i thought of sharing it here
viepager.setCurrentItem(5,false);


the most helpful library for doing paging is pull to refresh by 



Thursday 8 October 2015

Android hexa codes for gradient

Android hexa codes for gradient
Android uses Hex ARGB values, which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the Alpha Channel. You must convert your decimal opacity values to a Hexdecimal value. Here are the steps:
Alpha Hex Value Process
  1. Take your opacity as a decimal value and multiply it by 255. So, if you have a block that is 50% opaque the decimal value would be .5. For example: .5 x 255 = 127.5
  2. The fraction won't convert to hex, so you must round your number up or down to the nearest whole number. For example: 127.5 rounds up to 128; 55.25 rounds down to 55.
  3. Enter your decimal value in a decimal to hexadecimal converter, like thishttp://www.binaryhexconverter.com/decimal-to-hex-converter, and convert your values
  4. If you only get back a single value, prefix it with a zero. For example, if you're trying to get 5% opacity and your going through this process you'll end up with the hex value of D. Add a zero in front of it so it appears as 0D.
That's how you find the alpha channel value. I've taken the liberty to put together a list of values for you. Enjoy!
Hex Opacity Values
  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00



And usage in xml is in this way


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="90"
    android:endColor="#00ffffff"
    android:startColor="#40000000"
    android:centerColor="#1A000000" />

<corners android:radius="0dp" />
</shape>



Wednesday 7 October 2015

View pager showing the next and previous page

Hi guys

i was woking on a project which have loads of fragments and the main constraint was having half of the other fragment view





To show preview of left and right pages set the following two values
  1. viewpager.setClipToPadding(false)
  2. viewpager.setPadding(left,0,right,0)
If you need space between two pages in the viewpager then add viewpager.setPageMargin(int)


 The code is available in git hub



Enjoy!!!!

Friday 25 September 2015

Creating singleton class and use it across your application- android

Here you can find the singleton class which u can use it across your app as a global variable, i use this usually passing the arraylist

package com.example.testSingleton;

/**
 * Created with Sharath yadhav.
 * Date: 13/09/2015
 * Time: 10:36
 */



public class Singleton {
    private static Singleton mInstance = null;
    private String mString;
    private ArrayList<yourclass> menudeatail;
    public ArrayList< yourclass > getMenudeatail() {
return menudeatail;
}
public void setMenudeatail(ArrayList< yourclass > menudeatail) {
this.menudeatail = menudeatail;
}
public static Singleton getInstance(){
        if(mInstance == null)
        {
            mInstance = new Singleton();
        }
        return mInstance;
    }
    public String getString(){
        return this.mString;
    }
    public void setString(String value){
        mString = value;
    }
}


How to use this:



Singleton.getInstance().setMenudeatail(your arraylist);


To retrieve it
Singleton.getInstance().getMenudeatail();