Wednesday, 26 June 2013

Android ActionBar Tab Example 1

Here in This example i'm going to show how to display Tab's in ActionBar.

Project Name: ActionBarWithTab
Java files       : MainActivity.java


ScreenShots:
----------------

MainActivity.java:
package com.ram.actionbarwithtab;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements TabListener {
Tab tab1, tab2, tab3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

tab1 = actionBar.newTab();
tab1.setText("camera");
tab1.setTabListener(this);
actionBar.addTab(tab1);

tab2 = actionBar.newTab();
tab2.setText("contacts");
tab2.setTabListener(this);
actionBar.addTab(tab2);

tab3 = actionBar.newTab();
tab3.setText("gallery");
tab3.setTabListener(this);
actionBar.addTab(tab3);

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:

Toast.makeText(getApplicationContext(), "tab1 selected",
Toast.LENGTH_LONG).show();
break;

case 1:
Toast.makeText(getApplicationContext(), "tab2 selected",
Toast.LENGTH_LONG).show();
break;

case 2:
Toast.makeText(getApplicationContext(), "tab3 selected",
Toast.LENGTH_LONG).show();
break;
}

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}




Monday, 24 June 2013

ActionBar Menu

ScreenShots:



strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBar</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="updateItemText">Update</string>
    <string name="DeleteItemText">Delete</string>

</resources>

Note: Create following main.xml file under menu folder.
main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/updateitem"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        android:title="@string/updateItemText"/>
    <item
        android:id="@+id/delitem"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        android:title="@string/DeleteItemText"/>

</menu>

activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>


MainActivity.java:
package com.ram.actionbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

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

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delitem:
Toast.makeText(getApplicationContext(), "Delete Selected",
Toast.LENGTH_SHORT).show();
break;

case R.id.updateitem:
Toast.makeText(getApplicationContext(), "Update Selected",
Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}






SMSListener in Android

ScreenShot:
----------------

activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>


MainActivity.java:
package com.ram.smslistener;

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

public class MainActivity extends Activity {

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

}


SMSListener.java:

package com.ram.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSListener extends BroadcastReceiver {
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
String from;
String msg;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// retrieve the SMS message received
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
from = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
msg = msgs[i].getMessageBody().toString();
str += "\n";

Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}

}

}

}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.smslistener"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.smslistener.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".SMSListener" android:enabled="true" >
                <intent-filter android:priority="1000">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>


    </application>

</manifest>




Saturday, 15 June 2013

Publishing AdMob ads in Android Applications

Turn ON and Turn OFF Airplane Mode in Android

ScreenShots:


activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:onClick="airplaneModeOn"
        android:textOff="Airplane Mode OFF"
        android:textOn="Airplane Mode ON" />

</RelativeLayout>

MainActivity.java:
package com.ram.airplanemodedemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

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

@SuppressLint("InlinedApi")
private static boolean isAirplaneModeOn(Context context) {

return Settings.System.getInt(context.getContentResolver(),
Global.AIRPLANE_MODE_ON, 0) != 0;

}

@SuppressLint("NewApi")
public void airplaneModeOn(View view) {
try {

android.provider.Settings.System.putInt(getContentResolver(),
Global.AIRPLANE_MODE_ON,
isAirplaneModeOn(getApplicationContext()) ? 0 : 1);

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isAirplaneModeOn(getApplicationContext()));
sendBroadcast(intent);
} catch (Exception e) {
Toast.makeText(this, "Exception occured during Airplane Mode ON", Toast.LENGTH_LONG)
.show();
}
}
}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.airplanemodedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.airplanemodedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




Wednesday, 12 June 2013

Audio Recording Example in Android

ScreenShots:

strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Audio Capture</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="startBtn">Start Recording</string>
    <string name="stopBtn">Stop Recording</string>
    <string name="playBtn">Play </string>

</resources>

activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_startRecordBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="@string/startBtn" />

    <Button
        android:id="@+id/button_stopRecordBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_startRecordBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="@string/stopBtn" />

    <Button
        android:id="@+id/button_playBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_stopRecordBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:text="@string/playBtn" />

</RelativeLayout>


MainActivity.java:
package com.ram.audiocapture;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
private Button startRecBtn, stopRecBtn, playBtn;
private MediaRecorder recorder = null;
String FILE_PATH_NAME = null;
private MediaPlayer m = null;

public MainActivity() {
FILE_PATH_NAME = Environment.getExternalStorageDirectory()
.getAbsolutePath();
FILE_PATH_NAME += "/recordsample.3gp";

}

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

startRecBtn = (Button) findViewById(R.id.button_startRecordBtn);

stopRecBtn = (Button) findViewById(R.id.button_stopRecordBtn);

playBtn = (Button) findViewById(R.id.button_playBtn);

startRecBtn.setOnClickListener(this);
stopRecBtn.setOnClickListener(this);
playBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.button_startRecordBtn:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(FILE_PATH_NAME);
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
break;

case R.id.button_stopRecordBtn:
recorder.stop();
recorder.reset();  
recorder.release();
recorder = null;
break;
case R.id.button_playBtn:

m = new MediaPlayer();
try {
m.setDataSource(FILE_PATH_NAME);
m.prepare();

} 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();
}

m.start();

break;
}
}

@Override
protected void onPause() {
if (recorder != null) {
recorder.release();
recorder = null;
}
if (m != null) {
m.release();
m = null;
}

super.onPause();
}

}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.audiocapture"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.audiocapture.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>