Wednesday, 10 October 2012

Android Checkbox Example


In Android, you can use “android.widget.CheckBox” class to render a checkbox.
In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.

1. Custom String

Open “res/values/strings.xml” file, add some user-defined string.
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyAndroidAppActivity!</string>
    <string name="app_name">MyAndroidApp</string>
    <string name="chk_ios">IPhone</string>
    <string name="chk_android">Android</string>
    <string name="chk_windows">Windows Mobile</string>
    <string name="btn_display">Display</string>
</resources>
2. CheckBox
Open “res/layout/main.xml” file, add 3 “CheckBox” and a button, inside the LinearLayout.
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" >
 
    <CheckBox
        android:id="@+id/chkIos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_ios" />
 
    <CheckBox
        android:id="@+id/chkAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:checked="true" />
 
    <CheckBox
        android:id="@+id/chkWindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_windows" />
 
    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />
 
</LinearLayout>
3. Code Code
Attach listeners inside your activity “onCreate()” method, to monitor following events :
  1. If checkbox id : “chkIos” is checked, display a floating box with message “Bro, try Android”.
  2. If button is is clicked, display a floating box and display the checkbox states.
File : MyAndroidAppActivity.java
package com.abhi.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
 
public class MyAndroidAppActivity extends Activity {
 
  private CheckBox chkIos, chkAndroid, chkWindows;
  private Button btnDisplay;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
 
	addListenerOnChkIos();
	addListenerOnButton();
  }
 
  public void addListenerOnChkIos() {
 
	chkIos = (CheckBox) findViewById(R.id.chkIos);
 
	chkIos.setOnClickListener(new OnClickListener() {
 
	  @Override
	  public void onClick(View v) {
                //is chkIos checked?
		if (((CheckBox) v).isChecked()) {
			Toast.makeText(MyAndroidAppActivity.this,
		 	   "Bro, try Android :)", Toast.LENGTH_LONG).show();
		}
 
	  }
	});
 
  }
 
  public void addListenerOnButton() {
 
	chkIos = (CheckBox) findViewById(R.id.chkIos);
	chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
	chkWindows = (CheckBox) findViewById(R.id.chkWindows);
	btnDisplay = (Button) findViewById(R.id.btnDisplay);
 
	btnDisplay.setOnClickListener(new OnClickListener() {
 
          //Run when button is clicked
	  @Override
	  public void onClick(View v) {
 
		StringBuffer result = new StringBuffer();
		result.append("IPhone check : ").append(chkIos.isChecked());
		result.append("\nAndroid check : ").append(chkAndroid.isChecked());
		result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
 
		Toast.makeText(MyAndroidAppActivity.this, result.toString(),
				Toast.LENGTH_LONG).show();
 
	  }
	});
 
  }
}

4. Demo

Run the application.
1. Result :


this is how you use the checkbox feature of android.
try this and explore. 

OraNgE.

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.