Wednesday, 22 October 2014

how to sort data in array list in android ?

public class MainActivity extends Activity {

ArrayList<String> array_list;
SortAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView1);
array_list = new ArrayList<String>();
array_list.add("jhsghd");
array_list.add("kisghd");
array_list.add("ajhsghd");
array_list.add("abjhsghd");
array_list.add("bjhsghd");
array_list.add("cjhsghd");

array_list.add("djhsghd");

array_list.add("ejhsghd");
array_list.add("fjhsghd");
array_list.add("gjhsghd");
array_list.add("hjhsghd");
array_list.add("ijhsghd");


 Collections.sort(array_list, StringDescComparator);

adapter = new SortAdapter(array_list);

list.setAdapter(adapter);

}



    public static Comparator<String> StringAscComparator = new Comparator<String>() {

        public int compare(String app1, String app2) {

            String stringName1 = app1;
            String stringName2 = app2;
           
            return stringName1.compareToIgnoreCase(stringName2);
        }
    };

    //Comparator for Descending Order
    public static Comparator<String> StringDescComparator = new Comparator<String>() {

        public int compare(String app1, String app2) {

            String stringName1 = app1;
            String stringName2 = app2;
           
            return stringName2.compareToIgnoreCase(stringName1);
        }
    };

class SortAdapter extends BaseAdapter {
ArrayList<String> arraydata;
LayoutInflater inflater;
ViewHolder holder;

public SortAdapter(ArrayList<String> arraylist) {
arraydata = arraylist;
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arraydata.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

class ViewHolder {

TextView text;

}

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

if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.attach, null);

holder.text = (TextView) convertView
.findViewById(R.id.textView1);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(arraydata.get(position));

return convertView;
}

}
}

Paypal integration in android ?

Just refer the following link ?

http://www.androidhub4you.com/2014/07/android-paypal-gateway-example-paypal.html

Thursday, 16 October 2014

scroll up and down animation in android ?

Scroll-down


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="1000"/>
</set>


Scroll-up


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="500"/>
</set>


call them:


relatv.setOnTouchListener(new OnTouchListener () {
 public boolean onTouch(View view, MotionEvent event) {
   if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
   
    Animation slide = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.scrollup);
button.startAnimation(slide);
button.setVisibility(View.GONE);

   
   } else if (event.getAction() == event.ACTION_UP) {
   
    button.setVisibility(View.VISIBLE);
   
    Animation slide1 = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.scrolldown);
    button.startAnimation(slide1);
   }
 
return false;
 }
});

water drop animations /circular ripple animations in android ?

package com.example.samplebutton;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Region;
import android.graphics.Shader;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

Button button;
RelativeLayout relatv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relatv=(RelativeLayout)findViewById(R.id.relatv);
// button = (Button) findViewById(R.id.lll);

MyButton butto=new MyButton(MainActivity.this);
relatv.addView(butto);


}

// ///////////////////

public class MyButton extends Button {

private float mDownX;
private float mDownY;

private float mRadius;

private Paint mPaint;

public MyButton(final Context context) {
super(context);
init();
}

public MyButton(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}

public MyButton(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
mPaint = new Paint();
mPaint.setAlpha(100);
}

@Override
public boolean onTouchEvent(@NonNull final MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
mDownX = event.getX();
mDownY = event.getY();

ObjectAnimator animator = ObjectAnimator.ofFloat(this,
"radius", 0, getWidth() * 3.0f);
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(400);
animator.start();
}
return super.onTouchEvent(event);
}

public void setRadius(final float radius) {
mRadius = radius;
if (mRadius > 0) {
RadialGradient radialGradient = new RadialGradient(mDownX,
mDownY, mRadius * 3, Color.TRANSPARENT, Color.BLACK,
Shader.TileMode.MIRROR);
mPaint.setShader(radialGradient);
}
invalidate();
}

private Path mPath = new Path();
private Path mPath2 = new Path();

@Override
protected void onDraw(@NonNull final Canvas canvas) {
super.onDraw(canvas);

mPath2.reset();
mPath2.addCircle(mDownX, mDownY, mRadius, Path.Direction.CW);

canvas.clipPath(mPath2);

mPath.reset();
mPath.addCircle(mDownX, mDownY, mRadius / 3, Path.Direction.CW);

canvas.clipPath(mPath, Region.Op.DIFFERENCE);

canvas.drawCircle(mDownX, mDownY, mRadius, mPaint);
}
}

// ///////////////////////

}

Tuesday, 7 October 2014

how to use restful service calls in android ?

public static String doPost(Context ctx, String mUrl,
List<NameValuePair> nameValuePairs, String username, String password) {

HttpParams httpParameters = new BasicHttpParams();

int timeoutConnection = 20000;

HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 20000;

HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

String response_data = null;

HttpResponse response = null;

if (username != null && password != null) {

httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),




new UsernamePasswordCredentials(username, password));

}

HttpPost postMethod = new HttpPost(mUrl);

try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

response = httpclient.execute(postMethod);

response_data = EntityUtils.toString(response.getEntity());
} catch (Exception e) {

}

return response_data;

}

}

How to get the face book groups in android ?

public static String getFacebookGroupList(String token, Context context,
String fb_id) {
InputStream is = null;
String result = "";

try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"https://graph.facebook.com/"
+ fb_id
+ "/groups?access_token="
+ token);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.i("RESULT", result);
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

How to get the facebook friend albums in android ?

public static String getFacebookFriendAlbum(String token, Context context,
String fb_id) {
InputStream is = null;
String result = "";

try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://graph.facebook.com/"+ fb_id+ "?fields=albums.limit(5).fields(id,name,cover_photo,photos.limit(10).fields(name,picture,source))&access_token="
+ token);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.i("RESULT", result);
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

delete particular file and sub files in android ?

public static void deleteFile(File f) {
String[] flist = f.list();
for (int i = 0; i < flist.length; i++) {
File temp = new File(f.getAbsolutePath() + "/" + flist[i]);
if (temp.isDirectory()) {
deleteFile(temp);
temp.delete();
} else {
temp.delete();
}
}
f.delete();
}

how to write Custom toast in android ?

public static void displayToast(Activity ctx, String title) {

LayoutInflater inflater = ctx.getLayoutInflater();
View layout = inflater.inflate(R.layout.customtoast,
(ViewGroup) ctx.findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(title);
Toast toast = new Toast(ctx);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(1500);
toast.setView(layout);
toast.show();
}

sections listview/gridview in android ?