Thursday, July 26, 2012

Play song in Emulator




Project Name     :  Song

Package Name   :  selva.song

Version              :  1.5 ( Supports 1.5 and above versions)


main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="31dp"
        android:layout_marginTop="50dp"
        android:text="Play" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
            android:layout_marginLeft="80dp"
        android:layout_marginTop="50dp"
        android:text="stop" />

</RelativeLayout>


 SongActivity.class


package selva.song;

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SongActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button play=(Button) findViewById(R.id.button1);
        Button stop=(Button) findViewById(R.id.button2);

        final MediaPlayer mp=MediaPlayer.create(getApplicationContext(), R.raw.song);

        // here i used song.mp3.. You can add audio file under res/raw/
       // create raw folder under res folder then add song
 
        play.setOnClickListener(new View.OnClickListener()
        {
        
           @Override
           public void onClick(View v)
             {
                      // TODO Auto-generated method stub
        
              mp.stop();
                  try
                     {
                     mp.prepare();
                    }
                  catch (IllegalStateException e)
                     {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                     }
                  catch (IOException e)
                     {
                           // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             mp.start();
            
         }
     });
       
     stop.setOnClickListener(new View.OnClickListener()
        {
        
         @Override
         public void onClick(View v)
         {
             // TODO Auto-generated method stub
        
             mp.stop();
    
         }
     });
    }
}



song.mp3 


























OUTPUT: 















click play button. You will hear the song.

click stop button. song playing will be stopped.




Simple Seekbar In Android



click here to download source code


Project Name     :  seekbar

Package Name   :  selva.seekbar

Version              :  1.5 ( Supports 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="94dp" >

        <requestFocus />
    </EditText>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="38dp" />

</RelativeLayout>



 SeekbarActivity.class



package selva.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class SeekbarActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        SeekBar sb=(SeekBar) findViewById(R.id.seekBar1);
        final EditText et=(EditText) findViewById(R.id.editText1);
       
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser)
            {
            //---change the font size of the EditText---
         
            et.setText(String.valueOf(progress));
            }
            });
       
    }
}


OUTPUT  : 


























move seekbar


























click here to download source code



Monday, July 23, 2012

Check URL is available or not



click here to download source code

package name   :  selva.ip

project Name    :   Ip

Version             :   1.5 ( Supports 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>




 IpActivity.java



package selva.ip;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class IpActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
                 try
                   {

                           URL url = new URL("http://androidprogramz.in/" );
                         //URL url = new URL("http://www.nofoundwebsite.com/" );
                             executeReq(url);
                             Toast.makeText(getApplicationContext(), "Webpage is available!", Toast.LENGTH_SHORT).show();
        
                   }
                 catch(Exception e)
                     {
                             Toast.makeText(getApplicationContext(), "oops! webpage is not available!", Toast.LENGTH_SHORT).show();
                     }


    }
   
    private void executeReq(URL urlObject) throws IOException
    {
        HttpURLConnection conn = null;
        conn = (HttpURLConnection) urlObject.openConnection();
        conn.setReadTimeout(30000);//milliseconds
        conn.setConnectTimeout(3500);//milliseconds
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        // Start connect
        conn.connect();
        InputStream response =conn.getInputStream();
        Log.d("Response:", response.toString());
    }

}



Output :









change unavailable url  in  IpActivity.java

then run it.














































click here to download source code



Sliding panel from bottom to top



click here to download source code

package name  :  selva.sliding

project  name   :  Sliding3

version             :  1.5 ( Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <Button
        android:id="@+id/show1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"  
        android:background="@drawable/collapse"/>
 

        <selva.sliding.Sliding
            android:id="@+id/sliding1"
            android:orientation="vertical"
            android:layout_marginLeft="120dp" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         
            android:gravity="left"
            android:padding="1px"
            android:background="#ffffff">
                <CheckBox android:id="@+id/check1"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                        android:text="Option1" />
                <CheckBox android:id="@+id/check2"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                         android:text="Option2" />
                <CheckBox android:id="@+id/check3"
                        android:textColor="#FFFFFF"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         android:text="Option3" />
        </selva.sliding.Sliding>
   
</LinearLayout>
 


 Sliding2Activity.java


package selva.sliding;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class Sliding2Activity extends Activity
{
      CheckBox c1,c2,c3;
        int key=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Sliding popup = (Sliding) findViewById(R.id.sliding1);
        popup.setVisibility(View.GONE);

                final Button btn=(Button)findViewById(R.id.show1);
                btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                if(key==0){
                                        key=1;
                                        popup.setVisibility(View.VISIBLE);
                                        btn.setBackgroundResource(R.drawable.collapse);
                                }
                                else if(key==1){
                                        key=0;
                                        popup.setVisibility(View.GONE);
                                        btn.setBackgroundResource(R.drawable.collapse);
                                }
                        }
                });

        }
    }
 

 Sliding.java

package selva.sliding;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class Sliding extends LinearLayout
{
private Paint innerPaint, borderPaint ;
public Sliding(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Sliding(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}
public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}
@Override
protected void dispatchDraw(Canvas canvas) {
RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
super.dispatchDraw(canvas);
}
}

  Output :






click icon 














































 













































click here to download source code


Sunday, July 22, 2012

Sliding Panel from right to left



click here to download source code


package Name  :  selva.sliding

project Name    :  Sliding3

Version             :  1.5 ( Supports 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <Button
        android:id="@+id/show1"
        android:layout_width="wrap_content"
        android:layout_marginTop="250dp"
        android:layout_height="wrap_content"
        android:background="@drawable/prev_item" />
 

        <selva.sliding.Sliding
            android:id="@+id/sliding1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="200dp"
            android:gravity="left"
            android:padding="1px"
            android:background="#ffffff">
                <CheckBox android:id="@+id/check1"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                        android:text="Option1" />
                <CheckBox android:id="@+id/check2"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                         android:text="Option2" />
                <CheckBox android:id="@+id/check3"
                        android:textColor="#FFFFFF"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         android:text="Option3" />
        </selva.sliding.Sliding>
   
</LinearLayout>
 


 Sliding2Activity.java


package selva.sliding;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class Sliding2Activity extends Activity
{
      CheckBox c1,c2,c3;
        int key=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Sliding popup = (Sliding) findViewById(R.id.sliding1);
        popup.setVisibility(View.GONE);

                final Button btn=(Button)findViewById(R.id.show1);
                btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                if(key==0){
                                        key=1;
                                        popup.setVisibility(View.VISIBLE);
                                        btn.setBackgroundResource(R.drawable.prev_item);
                                }
                                else if(key==1){
                                        key=0;
                                        popup.setVisibility(View.GONE);
                                        btn.setBackgroundResource(R.drawable.prev_item);
                                }
                        }
                });

        }
    }

 

 Sliding.java



package selva.sliding;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class Sliding extends LinearLayout
{
private Paint innerPaint, borderPaint ;
public Sliding(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Sliding(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}
public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}
@Override
protected void dispatchDraw(Canvas canvas) {
RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
super.dispatchDraw(canvas);
}
}



Output:














































click icon
















































click here to download source code


Sliding Panel From Top to Bottom



Click Here To Download Source Code

Package Name   :    selva.sliding

Project Name     :    Sliding2

Version              :    1.5 ( Supports 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
        <selva.sliding.Sliding
            android:id="@+id/sliding1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="1px"
            android:background="#ffffff">
                <CheckBox android:id="@+id/check1"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                        android:text="Option1" />
                <CheckBox android:id="@+id/check2"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                         android:text="Option2" />
                <CheckBox android:id="@+id/check3"
                        android:textColor="#FFFFFF"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         android:text="Option3" />
        </selva.sliding.Sliding>

    <Button android:id="@+id/show1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/expand"/>
</LinearLayout>
 

 Sliding2Activity.java

package selva.sliding;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class Sliding2Activity extends Activity
{
      CheckBox c1,c2,c3;
        int key=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Sliding popup = (Sliding) findViewById(R.id.sliding1);
        popup.setVisibility(View.GONE);

                final Button btn=(Button)findViewById(R.id.show1);
                btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                if(key==0){
                                        key=1;
                                        popup.setVisibility(View.VISIBLE);
                                        btn.setBackgroundResource(R.drawable.expand);
                                }
                                else if(key==1){
                                        key=0;
                                        popup.setVisibility(View.GONE);
                                        btn.setBackgroundResource(R.drawable.expand);
                                }
                        }
                });

        }
    }
 



 Sliding.java

package selva.sliding;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class Sliding extends LinearLayout
{
private Paint innerPaint, borderPaint ;
public Sliding(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Sliding(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}
public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}
@Override
protected void dispatchDraw(Canvas canvas) {
RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
super.dispatchDraw(canvas);
}
}


Output : 














































click expand icon

 








Click Here To Download Source Code





Sliding Panel from Left To Right



Click Here to download Source Code

Package Name   :  selva.sliding

Project Name     :  Sliding1

Version              :  1.5  ( Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

        <selva.sliding.Sliding
            android:id="@+id/sliding1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="1px"
            android:background="#ffffff">
                <CheckBox android:id="@+id/check1"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                        android:text="Option1" />
                <CheckBox android:id="@+id/check2"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                         android:text="Option2" />
                <CheckBox android:id="@+id/check3"
                        android:textColor="#FFFFFF"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         android:text="Option3" />
        </selva.sliding.Sliding>

    <Button android:id="@+id/show1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/next_item"/>
</LinearLayout>


 Sliding1Activity.java


package selva.sliding;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class Sliding1Activity extends Activity
{
      CheckBox c1,c2,c3;
        int key=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Sliding popup = (Sliding) findViewById(R.id.sliding1);
        popup.setVisibility(View.GONE);

                final Button btn=(Button)findViewById(R.id.show1);
                btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                if(key==0){
                                        key=1;
                                        popup.setVisibility(View.VISIBLE);
                                        btn.setBackgroundResource(R.drawable.next_item);
                                }
                                else if(key==1){
                                        key=0;
                                        popup.setVisibility(View.GONE);
                                        btn.setBackgroundResource(R.drawable.next_item);
                                }
                        }
                });

        }
    }
 


 Sliding.java



package selva.sliding;



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class Sliding extends LinearLayout
{
private Paint innerPaint, borderPaint ;
public Sliding(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Sliding(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}
public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}
@Override
protected void dispatchDraw(Canvas canvas) {
RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
super.dispatchDraw(canvas);
}
}


Output :






click next icon











































Click Here to download Source Code