Monday 29 June 2015

Extract  Youtube id from the url



public static String extractYTId(String ytUrl) {
    String vId = null;
    Pattern pattern = Pattern.compile(".*(?:youtu.be\\/|v\\/|u\\/\\w\\/|embed\\/|watch\\?v=)([^#\\&\\?]*).*");
    Matcher matcher = pattern.matcher(ytUrl);
    if (matcher.matches()){
        vId = matcher.group(1);
    }
    return vId;
}

The above code works for the following

http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o
http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM4nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg
http://youtu.be/0zM4nApSvMg

Friday 19 June 2015

Instagram like counter android

   
Instagram like counter for android

TextSwitcher.xml


<TextSwitcher
                    android:id="@+id/likecount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:inAnimation="@anim/slide_in_counter"
                    android:outAnimation="@anim/slide_down_counter">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="123 likes"
                        android:textColor="@color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/white" />

                </TextSwitcher>


sample.java

TextSwitcher likecount;
Button share;

likecount=(TextSwitcher) findViewById(R.id.likecount);



 share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

k++;
likecount.setText(""+k);
}
}

});

To share image to facebook twitter

Android async process  for sharing a image thru share intent

To share image  to facebook twitter and other share intents we have to download the image as a temp file and use it
//Use the async task on click


shareAsynctask task = new shareAsynctask();

task.execute();


shareAsynctask 

ProgressDialog pd
@SuppressWarnings("unused")
private class shareAsynctask extends AsyncTask<String, Void, String> {
File file;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub

super.onPreExecute();

pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();

}

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bbicon = null;
String Url;

if (null != image_url) {

bbicon = getBitmapFromURL(image_url);

} else {

bbicon = getBitmapFromURL(image_url);
}
;// BitmapFactory.decodeResource(getResources(),R.drawable.bannerd10);
String extStorageDirectory = Environment
.getExternalStorageDirectory().toString();
OutputStream outStream = null;
file = new File(extStorageDirectory, "temp.png");
try {
outStream = new FileOutputStream(file, false);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
}
return "";

}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "body");
startActivityForResult(Intent.createChooser(share, "Share Image"),
0);
pd.show();

}



}

// Get the bitmap from the below function

public static Bitmap getBitmapFromURL(String src) {

    try {
   
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 
System.out.println(Uri.encode(src,"UTF-8"));
        URL url = new URL(src.replaceAll(" ", "%20"));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        //pDialog.dismiss();
        return myBitmap;
    } catch (IOException e) {

        e.printStackTrace();
        return null;
    }
   
}



Wednesday 3 June 2015

Android : Locating the item at center position of the screen after selecting it - horizontalscrollview

Android : Locating the item at center position of the screen after selecting it -horizontalscrollview





Horizontal.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:orientation="vertical"
    tools:context=".MainActivity" >

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom"
        android:orientation="vertical" >

       <HorizontalScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/updown"
                android:layout_weight="0.8"
                android:fadingEdgeLength="0dp" >

                <LinearLayout
                    android:id="@+id/horizontalbar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/background_dark"
                    android:baselineAligned="true"
                    android:orientation="horizontal" >
                </LinearLayout>
       </HorizontalScrollView>
  </LinearLayout>
</RelativeLayout>


2.horizontal.java


package your packagename

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class horizontal extends Activity
{
HorizontalScrollView hsl;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.horizontal);
      hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
      LinearLayout l1 = (LinearLayout)findViewById(R.id.horizontalbar);
      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      final int width = size.x;

        for(int i = 0; i < 20; i++)
        {
            final Button b = new Button(this);
            b.setText("t"+i);
            
            
            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                   int scrollX = (b.getLeft() - (width / 2)) + (b.getWidth() / 2);
                   hsl.smoothScrollTo(scrollX, 0);
                }

            });
            l1.addView(b);
            
            
            
        }
     }
}

Tuesday 2 June 2015

Video for your splash activity

Here is the full code for running a video in your splash screen


1.create a folder called 'raw' in your folder 'res' and add your video to this folder


2.Just copy and paste the below code 

public class ActivitySplash extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  try {
   splashPlayer();
  } catch (Exception ex) {
   jumpMain();
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  return false;
 }

 public void splashPlayer() {
  VideoView videoHolder = new VideoView(this);
  setContentView(videoHolder);
  Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
    + R.raw.splash);
  videoHolder.setVideoURI(video);
  videoHolder.setOnCompletionListener(new OnCompletionListener() {
   public void onCompletion(MediaPlayer mp) {
    jumpMain();
   }

  });
  videoHolder.start();
  videoHolder.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    ((VideoView) v).stopPlayback();
    jumpMain();
    return true;
   }
  });
 }

 private synchronized void jumpMain() {
  Intent intent = new Intent(ActivitySplash.this, ActivityMain.class);
  startActivity(intent);
  finish();
 }
}