Pop Ups(Toast, AlertDialog, Layout) Tutorial for Android

Toast, AlertDialog Box

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

public class MyPopUps {

	public static void ShowToast(Context context, String msg){
		Toast.makeText( context,
                		msg,
                		Toast.LENGTH_LONG ).show()
                		;
	}

	public static void ShowAlertBox(Context context, String msg){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("OK",null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, int msg){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("OK",null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, String msg, String neutralActionName, OnClickListener neutralClickListener){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("Positive Action - OK",null)
									.setNeutralButton("Neutral Action - Save", neutralClickListener)
									.setNegativeButton("Negative Button - Cancel", null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, int msg, String neutralActionName, OnClickListener neutralClickListener){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("Positive Action - OK",null)
									.setNeutralButton("Neutral Action - Save", neutralClickListener)
									.setNegativeButton("Negative Button - Cancel", null)
									;
		failureDialog.show();
	}

}

Calling Functions

MyPopUps.ShowAlertBox(Splash.this, "Neutral Alert", "Save",
				  	new android.content.DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog,int which) {
							MyPopUps.ShowToast(Splash.this, "Saved");
						}
				  	}
				  );

MyPopUps.ShowAlertBox(Splash.this, R.string.app_name);

Layout

We can also make customized popups by using Relative Linear Layouts and setting their Visible property Gone and visible as required.

RelativeLayout rltInfoDialog;
rltInfoDialog = (RelativeLayout) findViewById(R.id.rlt_InfoDialog);
rltInfoDialog.setVisibility(View.GONE);
// ------------------------- //
public void hideInfoBox(){
	rltInfoDialog.setVisibility(View.GONE);
	tvTitle.setText("");
	tvDescription.setText("");
}
// ------------------------- //
public void showInfoBox(string title, string description){
	rltInfoDialog.setVisibility(View.VISIBLE);
	tvTitle.setText(title);
	tvDescription.setText(description);
}
// ------------------------- //
Call showInfoBox() and hideInfoBox() methods
where required to show and hide info box.

Custom Dialog
See https://mfarhan133.wordpress.com/2010/10/14/custom-dialog-tutorial-for-android/

Posted in Android, Tutorials | Tagged , , , , , | Leave a comment

Using Audio-Video Files Tutorial For Android

Audio Files

Here is the source code how to use(via app & online uri) the audio file in android

try {
   AudioManager audioManager;
   MediaPlayer mediaPlayer;
   audioManager = (AudioManager) 
                  this.getSystemService(this.AUDIO_SERVICE);
   mediaPlayer=MediaPlayer
                   .create(this, Uri.parse("some online url"));      
   mediaPlayer=MediaPlayer
                   .create(this, R.raw.alarm1);
   int streamVolume=audioManager
                   .getStreamVolume(AudioManager.STREAM_MUSIC);
   mediaPlayer.setVolume(streamVolume, streamVolume);
   mediaPlayer.start();
   Thread.sleep(3000);
   mediaPlayer.stop();
} catch (Exception e) {	}

Video Files

Here is the source code how to use(via online uri) the video file in android

private VideoView mVideoView;
mVideoView = (VideoView) findViewById(R.id.surface_view);
try {
    final String path = mPath.getText().toString();
    if (path == null || path.length() == 0) {
        Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
        Toast.LENGTH_LONG).show();
    } else {
        if (path.equals(current) && mVideoView != null) {
	     mVideoView.start();
	     mVideoView.requestFocus();
	     return;
         }
    current = path;
    //mVideoView.setVideoPath(R.raw.vid);
    mVideoView.setVideoPath(getDataSource(path));
    mVideoView.start();
    mVideoView.requestFocus();
    }
} catch (Exception e) {}

Run the code on mobile.

here is the complete source code.

Posted in Android, Tutorials | Tagged , , , | Leave a comment

Android Helpful links

Android Cheat Sheet

http://speckyboy.com/2011/01/14/mobile-cheatsheets-for-iphone-android-and-blackberry-app-development/

WP Blogs

http://androidandandroid.wordpress.com/

http://w2davids.wordpress.com/beginning-android-programming/

http://independentlyemployed.co.uk/android-tutorials/

http://www.krvarma.com/category/posts/android/ (Gestures, telephony manager etc)

Useful Liks: http://sagemobile.wordpress.com/2010/09/30/useful-links/

External Links

Android: http://developer.android.com/index.html

Android Academy (source codes): http://www.androidacademy.com/3-tutorials

http://www.warriorpoint.com/blog/category/android/

Individual Entities Tutorials

Android APK Build Process http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package-apk-from-the-command-line-using-the-sdk-tools-continuously-integrated-using-cruisecontrol/

Supporting Multiple Screens: http://developer.android.com/guide/practices/screens_support.html#testing

UnZipping Files: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)

Using Menus, Preferences, Animations http://www.androidacademy.com/3-tutorials/43-hands-on/167-coolsplash-an-interactive-splash-toy-for-android-apps

Common Tasks How to do them in Android: http://developer.android.com/guide/appendix/faq/commontasks.html

Telephony Manager: http://www.krvarma.com/posts/android/using-phonestatelistener-to-listen-to-state-change-in-android/

Broadcasting: http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

GPS: http://www.javacodegeeks.com/2010/09/android-location-based-services.html,    http://developer.android.com/guide/topics/location/obtaining-user-location.html

Camera: http://marakana.com/forums/android/examples/39.html

Flip View: http://www.androidpeople.com/android-viewflipper-example/

Custom Scroll View (Great Tutorial) https://github.com/ysamlan/horizontalpager/blob/master/src/com/github/ysamlan/horizontalpager/HorizontalPager.java

Styles: http://stackoverflow.com/questions/3078081/setting-global-style-for-views-in-android

Android AndEngine

Examples(Source Code) http://code.google.com/p/andengineexamples/

Documentation http://denravonska-javadoc.googlecode.com/hg/doc/index.html?overview-summary.html

Posted in Android Links, Other Links | Tagged , | Leave a comment

Reusing Layout (Include-Merge Tag) Tutorial For Android.

This tutorial is about how to reuse the same layouts which is used in many layouts(xml files) for example most of the apps have footer whose layout repeats.It is done by using include and merge tags.

<include> tag basically means ‘take that file and paste it’s contents here’

<merge > The layout which we have to use must be enclosed under merge tag, so that we can include layouts from other xmls.

You can download its complete source code from here .

footer.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
	<LinearLayout android:id="@+id/LinearLayout01"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/Button11"
			android:text="Button11"></Button>
		<Button android:layout_below="@id/Button01"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:id="@+id/Button12" android:text="Button12"></Button>
	</LinearLayout>
</merge>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<RelativeLayout android:id="@+id/RelativeLayout02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_centerInParent="true">
		<Button android:id="@+id/Button01" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Next Activity   "></Button>
	</RelativeLayout>
	<RelativeLayout android:id="@+id/RelativeLayout03"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_alignParentBottom="true">
		<include layout="@layout/footer" />
	</RelativeLayout>
</RelativeLayout>

Splash.java

public class Splash 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.Button01).setOnClickListener(this);
		findViewById(R.id.Button11).setOnClickListener(this);
		findViewById(R.id.Button12).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Button01:
			startActivity(new Intent(this, Act2.class));
			break;
		case R.id.Button11:
			Toast.makeText(this, "Button 11 - Activity 1 ....",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.Button12:
			Toast.makeText(this, "Button 12 - Activity 1 ....",
					Toast.LENGTH_SHORT).show();
			break;
		}
	}

}
You can see in the code that Button11 and Button12 have their separrate onClicklisteners on two seperate activities. Also to make common their actions see my Blog.

http://stackoverflow.com/questions/2732682/simple-example-of-merge-and-include-usage-in-android-xml-layouts

http://developer.android.com/resources/articles/layout-tricks-merge.html


free counters

Posted in Android | Tagged , | 1 Comment

Generate Google Maps API Key Tutorial For Android

First of all locate the SDK debug certificate located in the default folder of
“C:\Documents and Settings\<username>\.android\debug.keystore”.
The filename of the debug keystore is “debug.keystore”.
For simplicity, copy this file (debug.keystore) and paste in C drive. (Path of file will become “C:\debug.keystore”).

Using the debug keystore, you need to extract its MD5 fingerprint using the Keytool.exe application included with your JDK installation. This fingerprint is needed to apply for the free Google Maps key. You can usually find the Keytool.exe from the “C:\Program Files\Java\<JDK_version_number>\bin”

Open Command Prompt, change your current directory to

“C:\Program Files\Java\<JDK_version_number>\bin”

and apply the following command on command prompt to extract the MD5 fingerprint.
keytool.exe -list -alias androiddebugkey -keystore “C:\debug.keystore” -storepass android -keypass android


Copy the MD5 certificate fingerprint and navigate your web browser to http://code.google.com/android/maps-api-signup.html . Follow the instructions on the page to complete the application and obtain the Google Maps key.

NOTE:

If you are using Emulator then it is necessary to use the key which is generated using jdk installed on the same PC on which the Emulator is running. On mobiles this condition not applied.

Other Important Links

http://mobiforge.com/developing/story/using-google-maps-android

Posted in Android | Tagged , | Leave a comment

AutoComplete Text View Tutorial For Android

This tutorial is about how to show Auto Complete text in Android.

Here its source code.

HelloAutoComplete.java

public class HelloAutoComplete extends Activity {
static final String[] COUNTRIES = new String[] {
“Afghanistan”, “Albania”, “Algeria”,”Zimbabwe”
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextViewtextView=(AutoCompleteTextView)findViewById(R.id.autocomplete_country);
ArrayAdapter<String>adapter=newArrayAdapter<String>(this,R.layout.list_item,COUNTRIES);
textView.setAdapter(adapter);
}

}
Posted in Android | Tagged , , | 2 Comments

Physics Behind Swing

Its always interesting for the cricket lovers to know whats the actual science behind the swing of ball specially reverse swing. Mostly players get the swing skills by practice but if they know what the actual science behind it they can enhance these skills and bowl according to weather and air conditions can create a lot of impact.

The all science behind in/out swing is Bernoulli’s Eauation which is give below.

OR

where P and V are pressure and velocities respectively and they are inversely proportional to each other according to the equation. Higher the velocity lesser will be the pressure to keep left side equal to constant and vice versa, keep in mind that the ball always deflect towards the low pressure. Bowlers keep the ball one side shiny and the other side rough . When bowler bowls the ball and ball follow its trajectory air move less obstructly from shiny side compare to rough side, air velocity will be higher on rought side means pressure will be low and ball deflect towards it called swing. Reverse the sides for opposite swing.

Reverse swing mostly concerned with the oldness of ball and its heavier and lighter side. If ball is not heavier at one side reverse swing is not possible. But after getting required ball features for reverse swing it still requires a lot of practice and acurate arm angle to reverse swing the ball. That is the reason bowlers scratch the ball at one side to get the reverese swing called ball tempering which is against ICC rules and bowlers who focused on reverse swing got arm injury occasionally.

Other Important Links.

http://www.cricinfo.com/magazine/content/story/258645.html

http://blog.dawn.com/2010/08/16/physics-behind-swing

http://www.wiziq.com/tutorial/30955-Physics-in-the-world-of-cricket

Posted in Articles/News, Sci-Tech | Tagged , | Leave a comment