Sunday, 23 September 2012

Sending SMS in Android


In Android, you can use SMS MANAGER API or device’s Built-in SMS application to send a SMS message. In this tutorial, we show you two basic examples to send SMS message :
  1. SmsManager API
     SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
  2. Built-in SMS application
     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
     sendIntent.putExtra("sms_body", "default content"); 
     sendIntent.setType("vnd.android-dir/mms-sms");
     startActivity(sendIntent);
Of course, both need SEND_SMS permission.
<uses-permission android:name="android.permission.SEND_SMS" />
1. SmsManager Example
Android layout file to textboxes (phone no, sms message) and button to send the SMS message.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Phone Number : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <EditText
        android:id="@+id/editTextPhoneNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:phoneNumber="true" >
    </EditText>
 
    <TextView
        android:id="@+id/textViewSMS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter SMS Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <EditText
        android:id="@+id/editTextSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="5"
        android:gravity="top" />
 
    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
 
</LinearLayout>
File : SendSMSActivity.java – Activity to send SMS via SmsManager.
package com.abhi.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class SendSMSActivity extends Activity {
 
 Button buttonSend;
 EditText textPhoneNo;
 EditText textSMS;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  buttonSend = (Button) findViewById(R.id.buttonSend);
  textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
  textSMS = (EditText) findViewById(R.id.editTextSMS);
 
  buttonSend.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
 
     String phoneNo = textPhoneNo.getText().toString();
     String sms = textSMS.getText().toString();
 
     try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
    Toast.makeText(getApplicationContext(), "SMS Sent!",
       Toast.LENGTH_LONG).show();
     } catch (Exception e) {
    Toast.makeText(getApplicationContext(),
     "SMS faild, please try again later!",
     Toast.LENGTH_LONG).show();
    e.printStackTrace();
     }
 
   }
  });
 }
}
File : AndroidManifest.xml , need SEND_SMS permission.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abhi.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="10" />
 
    <uses-permission android:name="android.permission.SEND_SMS" />
 
    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SendSMSActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
See demo :


This is how the SMS app can be created in your android mobile.

Use your own apps and hav fun


OraNgE.

Saturday, 22 September 2012

Android Textbox Example


In Android, you can use “EditText” class to create an editable textbox to accept user input.
This tutorial show you how to create a textbox in XML file, and demonstrates the use of key listener to display message typed in the textbox.

1. EditText

Open “res/layout/main.xml” file, add a “EditText” component.
File : res/layout/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" >
 
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
        <requestFocus />
 
    </EditText>
 
</LinearLayout>
2. EditText Listener
Attach a key listener inside your activity “onCreate()” method, to monitor following events :
  1. If “enter” is pressed , display a floating box with the message typed in the “EditText” box.
  2. If “Number 9″ is pressed, display a floating box with message “Number 9 is pressed!”.
File : MyAndroidAppActivity.java
package com.abhi.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
 
public class MyAndroidAppActivity extends Activity {
 
 private EditText edittext;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 addKeyListener();
 }
 
 public void addKeyListener() {
 
 // get edittext component
 edittext = (EditText) findViewById(R.id.editText);
 
 // add a keylistener to keep track user input
 edittext.setOnKeyListener(new OnKeyListener() {
 public boolean onKey(View v, int keyCode, KeyEvent event) {
 
  // if keydown and "enter" is pressed
  if ((event.getAction() == KeyEvent.ACTION_DOWN)
   && (keyCode == KeyEvent.KEYCODE_ENTER)) {
 
   // display a floating message
   Toast.makeText(MyAndroidAppActivity.this,
    edittext.getText(), Toast.LENGTH_LONG).show();
   return true;
 
  } else if ((event.getAction() == KeyEvent.ACTION_DOWN)
   && (keyCode == KeyEvent.KEYCODE_9)) {
 
   // display a floating message
   Toast.makeText(MyAndroidAppActivity.this,
    "Number 9 is pressed!", Toast.LENGTH_LONG).show();
   return true;
  }
 
  return false;
 }
 });
}
}

3. Demo

Run the application.
1. Type something inside the textbox, and press on the “enter” key :


This is sample TextBox with a Toast Message.

OraNgE.

Android WebView

Android WebView Example 

Android’s WebV allows you to open an own windows for viewing URL or custom html markup page.
In this tutorial, you will create two pages, a page with a single button, when you clicked on it, it will navigate to another page and display URL “google.com” in WebView component.

1. Android Layout Files

Create two Android layout files – “res/layout/main.xml” and “res/layout/webview.xml“.
File : res/layout/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" >
 
    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to http://www.google.com" />
 
</LinearLayout>
File : res/layout/main.xml – WebView example
<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
2. Activity
Two activity classes, an activity to display a button, another activity display the  Web view with predefined   URL.
File : MainActivity.java
package com.abhi.android;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
	private Button button;
 
	public void onCreate(Bundle savedInstanceState) {
		final Context context = this;
 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		button = (Button) findViewById(R.id.buttonUrl);
 
		button.setOnClickListener(new OnClickListener() {
 
		  @Override
		  public void onClick(View arg0) {
		    Intent intent = new Intent(context, WebViewActivity.class);
		    startActivity(intent);
		  }
 
		});
 
	}
 
}
File : WebViewActivity.java
package com.abhi.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
 
public class WebViewActivity extends Activity {
 
	private WebView webView;
 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
 
		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl("http://www.google.com");
 
	}
 
}

3. Android Manifest

here its required INTERNET permission, add below into mainfest file
<uses-permission android:name="android.permission.INTERNET" />
File : AndroidManifest.xml – See full example.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abhi.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="10" />
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WebViewActivity"
            android:theme="@android:style/Theme.NoTitleBar" />
 
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
4. Demo
By default, just display a button.
Click on button, WebView is display.
TRY THI SAMPLE EXAMPLE OF WebView  and Enjoy Android , also u can create your own app which links"google search engine" or any sites which you use frequently. 
OraNgE 

Sunday, 16 September 2012

User Interface Controls

In this tutorial, we show you how to display a normal button, add a click listener, when user click on the button, open an URL in your Android’s internet browser.

1. Add Button

Open “res/layout/main.xml” file, add a button.
File : res/layout/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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button - Go to google.com" />
 
</LinearLayout>
2. Code Code
Attach a click listener to the button.
When user click on it, open mobile browser and display URL : http://www.google.com
File : MyAndroidAppActivity.java
package com.Abhi.android;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
 
public class MyAndroidAppActivity extends Activity {
 
	Button button;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		addListenerOnButton();
 
	}
 
	public void addListenerOnButton() {
 
		button = (Button) findViewById(R.id.button1);
 
		button.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
 
			  Intent browserIntent = 
                            new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
			    startActivity(browserIntent);
 
			}
 
		});
 
	}
 
}
when you run this code you will get the android emulator and an button option when you click that you will be      redirected to google.com.
Have fun with android
Tc. 

Sunday, 9 September 2012

Example of Custom ArrayAdapter

In this example, we show you how to create 4 items in the listview, and use a custom arrayadapter to display different images base on the “item name” in the list.


Android Layout file
File : res/layout/list_mobile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >
 
    <ImageView
        android:id="@+id/logo"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/windowsmobile_logo" >
    </ImageView>
 
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30px" >
    </TextView>
 
</LinearLayout>
2.3 Custom ArrayAdapter
Create a class extends ArrayAdapter and customize the item display in the getView() method.
package com.abhi.android.adaptor;
 
import com.abhi.android.R;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MobileArrayAdapter extends ArrayAdapter<String> {
 private final Context context;
 private final String[] values;
 
 public MobileArrayAdapter(Context context, String[] values) {
  super(context, R.layout.list_mobile, values);
  this.context = context;
  this.values = values;
 }
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  LayoutInflater inflater = (LayoutInflater) context
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
  View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
  TextView textView = (TextView) rowView.findViewById(R.id.label);
  ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
  textView.setText(values[position]);
 
  // Change icon based on name
  String s = values[position];
 
  System.out.println(s);
 
  if (s.equals("WindowsMobile")) {
   imageView.setImageResource(R.drawable.windowsmobile_logo);
  } else if (s.equals("iOS")) {
   imageView.setImageResource(R.drawable.ios_logo);
  } else if (s.equals("Blackberry")) {
   imageView.setImageResource(R.drawable.blackberry_logo);
  } else {
   imageView.setImageResource(R.drawable.android_logo);
  }
 
  return rowView;
 }
}
2.4 ListView
ListView, but use above custom adapter to display the list.
package com.abhi.android;
 
import com.mkyong.android.adaptor.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
 
public class ListMobileActivity extends ListActivity {
 
 static final String[] MOBILE_OS = 
               new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
  setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
 
 }
 
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
 
  //get selected items
  String selectedValue = (String) getListAdapter().getItem(position);
  Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
 
 }
 
}

2.5 Screen shots



for the image icons you need to add the particular images in resource drawable particulars.

Gud Luck.
Any queries pls do comment !!!!