Thursday, January 31, 2013

Android Playing Audio from a web url

Please make a project with the name AudioDemo and make a main class with the name  AudioDemo.java and have the following code


package com.jitesh.audiodemo;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class AudioDemo extends Activity implements
MediaPlayer.OnCompletionListener {
private static ProgressDialog progressDialog;
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private ImageButton replay;
private MediaPlayer mp;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
replay = (ImageButton) findViewById(R.id.replay);

play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});

pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});

stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
replay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setup();
}
});

setup();
}

@Override
public void onDestroy() {
super.onDestroy();

if (stop.isEnabled()) {
stop();
}
}

public void onCompletion(MediaPlayer mp) {
stop();
}

private void play() {
Log.d("play", "reached");
mp.start();

play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}

private void stop() {
Log.d("stop", "reached");
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);

try {
setup();
} catch (Throwable t) {
goBlooey(t);
}
}

private void pause() {
Log.d("pause", "reached");
mp.pause();

play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}

private void loadClip() {
try {
mp = MediaPlayer.create(this, R.raw.clip);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}

private void setup() {
try {
progressDialog = ProgressDialog.show(AudioDemo.this, "",
"Buffering audio...", true);
progressDialog.setCancelable(true);
mp = new MediaPlayer();
mp.setDataSource("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
mp.prepareAsync();

mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("first", "reached");
// mp.start();
progressDialog.dismiss();
}
});

} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}

private void goBlooey(Throwable t) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Exception!").setMessage(t.toString())
.setPositiveButton("OK", null).show();
}
}

the main.xml shoupd look like this

<?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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:paddingRight="4dip"
            android:src="@drawable/play" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Play"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/pause" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Pause"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/stop" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Stop"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton
            android:id="@+id/replay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/restart" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Replay"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>

do not foget to add the permission at manidest

<uses-permission android:name="android.permission.INTERNET" />
the screen shots are attached as follows



the used resources are give below as drawables.









Yo can download the audio from a link AudioDemo

Android Playing video from a web url

In android devices we can play a video from a web url in few simple steps. please make a project and have

VideoDemo.java


package com.jitesh.android.video;

import java.io.File;

import com.jitesh.android.video.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoDemo extends Activity {
private String videoPath = "http://commonsware.com/misc/test2.3gp";

private static ProgressDialog progressDialog;
String videourl;
VideoView videoView;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

videoView = (VideoView) findViewById(R.id.video);

progressDialog = ProgressDialog.show(VideoDemo.this, "",
"Buffering video...", true);
progressDialog.setCancelable(true);

PlayVideo();

}

private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(
VideoDemo.this);
mediaController.setAnchorView(videoView);

Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}

});

} catch (Exception e) {
progressDialog.dismiss();
System.out.println("Video Play Error :" + e.toString());
finish();
}

}
}

the main.xml should look like this


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <VideoView 
     android:id="@+id/video" 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    />
</LinearLayout>

never forget the permission at manifest and add a line

 <uses-permission android:name="android.permission.INTERNET" />

the screen shot isbe shown here as well





the source code can be downloaded from hereVideoPlaying

Android Custom Spinner

Custom Sinner is an another excellent example of working in a customizing way in android.
for this purpose make a project with  the main activity MainActivity.java and with the given below code

MainActivity.java


package com.jitesh.customspinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

String[] strings = { "Austria", "Belgium", "Bulgaria", "Cyprus",
"Denmark", "Estonia" };

String[] subs = { "austria", "belgium", "bulgaria", "cyprus",
"denmark", "estonia" };

int arr_images[] = { R.drawable.austria, R.drawable.belgium,
R.drawable.bulgaria, R.drawable.cyprus, R.drawable.denmark,
R.drawable.estonia };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.row,
strings));
}

public class MyAdapter extends ArrayAdapter<String> {

public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView,
ViewGroup parent) {

LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.country);
label.setText(strings[position]);

TextView sub = (TextView) row.findViewById(R.id.sub);
sub.setText(subs[position]);

ImageView icon = (ImageView) row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);

return row;
}
}
}


the activity_main.xml is given below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
      android:background="@drawable/bkg"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt"
    />
</LinearLayout>

the row.xml is as follows

<?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="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>
    <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/ic_launcher"/>
    <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="3dip"
         android:layout_marginTop="2dip"
         android:textColor="@drawable/red"
         android:textStyle="bold"
         android:id="@+id/country"
         android:text="CoderzHeaven"
         android:layout_marginLeft="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="2dip"
         android:textColor="@drawable/darkgrey"
         android:layout_marginLeft="5dip"
         android:id="@+id/sub"
         android:layout_below="@+id/country"
         android:text="Jitesh Upadhyay codes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</RelativeLayout>

the strings.xml is having following info

<resources>

    <string name="app_name">CustomSpinner</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    
     <string name="hello">CustomSpinner Demo by Jitesh!</string>
    <string name="prompt">  Select your Country  </string>
    
    <drawable name="white">#ffffff</drawable>
    <drawable name="black">#000000</drawable>
    <drawable name="green">#347C2C</drawable>
    <drawable name="pink">#FF00FF</drawable>
    <drawable name="violet">#a020f0</drawable>
    <drawable name="grey">#778899</drawable>
    <drawable name="red">#C11B17</drawable>
    <drawable name="yellow">#FFFF8C</drawable>
    <drawable name="PowderBlue">#b0e0e6</drawable>
    <drawable name="brown">#2F1700</drawable>
    <drawable name="Hotpink">#7D2252</drawable>
    <string name="select_Category">Select Category</string>
    <drawable name="darkgrey">#606060</drawable>

</resources>

following are the used resources












You can download the code CUSTOM-SPINNER

Custom radio button and checkbox in android

We can make a project for this purpose. and have MainActivity.java as a main activity of the project with the following simple code.


package com.radio.jitesh.customradiobutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

   
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

the main.xml file should look like this


<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Radio Button "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/my_ratingbar"
            android:text="JITESH UPADHYAY" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/my_ratingbar"
            android:checked="true"
            android:text="UPADHYAY JITESH" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/my_ratingbar"
            android:text="JEETU" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Custom Check Box "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/my_ratingbar"
        android:checked="true"
        android:text="JITESH" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/my_ratingbar"
        android:text="UPADHYAY" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="http://upadhyayjiteshandroid.blogspot.in/"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

also we need a my_ratingbar.xml inside



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:drawable="@drawable/red_full" />
 <item android:state_checked="false" android:drawable="@drawable/red_holo" />
</selector>




also inside frawable we need images as follows



you can download the code here as well CustomrRadio And CheckBox

Android Custom Check Box

On Eclipse and start a new Android project. Create a folder called /xml in the /res directory. Add three XML-based resources to the /res/xml folder, two that represent the drawing code for the actual checkbox and a third that is the state selector.

 checked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android=http://schemas.android.com/apk/res/android
 android:shape="rectangle">
<gradient android:startColor="#ffff0000" android:endColor="#ff000000" android:angle="270"/>
<stroke android:width="4px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>
Unchecked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<gradient android:startColor="#ff585858" android:endColor="#ff000000" android:angle="270"/>
<stroke android:width="4px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>
custom_checkbox.xml

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

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:textSize="20dip"
        android:gravity="left"
        android:padding="10dip"
        android:text="Check It!" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:gravity="left"
        android:padding="10dip"
        android:text="Here is a checkbox..." />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="horizontal">
     <CheckBox
         android:id="@+id/checkbox1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:button="@xml/custom_checkbox"/>
     <TextView
         android:id="@+id/textview1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="14dip"
         android:gravity="center"
         android:padding="10dip"
         android:text="(unchecked)" />
 </LinearLayout>
 <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textSize="16dip"
         android:gravity="left"
         android:padding="10dip"
         android:text="And here is another..." />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="horizontal">
     <CheckBox
         android:id="@+id/checkbox2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:button="@xml/custom_checkbox"/>
     <TextView
         android:id="@+id/textview2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="14dip"
         android:gravity="center"
         android:padding="10dip"
         android:text="(unchecked)" />
 </LinearLayout>
</LinearLayout>
Main.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.checkbox1).setOnClickListener(this);
        findViewById(R.id.checkbox2).setOnClickListener(this);
    }

 
 public void onClick(View v) {
  TextView tv;
  if (v.getId()==R.id.checkbox1) {
   tv = (TextView)findViewById(R.id.textview1);
  } else {
   tv = (TextView)findViewById(R.id.textview2);
  }
  if (((CheckBox)v).isChecked()) {
   tv.setText("(checked)");
  } else {
   tv.setText("(unchecked)");
  }
 }
}

Wednesday, January 30, 2013

Custom Progress View on Android Webview

This is just an another lookup of the previous blog with some additional stuff.

please copy the given below code to WebPageLoader.java


package com.jitesh.webpageloader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebPageLoader extends Activity {
WebView webview;
private String url;
ProgressBar pd = null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

pd = (ProgressBar) findViewById(R.id.web_view_progress_bar);

webview = (WebView) findViewById(R.id.web_view);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setSupportZoom(true);

webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100 && pd.getVisibility() == ProgressBar.GONE) {
pd.setVisibility(ProgressBar.VISIBLE);
}
pd.setProgress(progress);
if (progress == 100) {
pd.setVisibility(ProgressBar.GONE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://upadhyayjiteshandroid.blogspot.in/");
}

}

and the main.xml is as follows


<?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" >

    <ProgressBar
        android:id="@+id/web_view_progress_bar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="2dip" >
    </ProgressBar>

    <WebView
        android:id="@+id/web_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </WebView>

</LinearLayout>



the code is available at the link WebLoader part-2