Wednesday 26 December 2012

Android Checkbox Tutorial

Android Checkbox Tutorial

In this tutorial am going to show how to use checkbox in android.Basically the checkbox is used for receive user input.Lets find how it works in android.

STEP BY STEP

1.Create an Android Project


2.Create a strings.xml file in res/values folder.
    Copy and paste the given bellow code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">CheckboxExample</string>
    <string name="linux_box">Android</string>
    <string name="macos_box">Apple</string>
    <string name="windows_box">Windows</string>
    <string name="display_label">Display</string>
    <string name="menu_settings">Settings</string>
</resources>



3.In this project we are adding three checkbox and when any of the checkbox   
   is selected,the corresponding message will be shown in a toast.

4.Open the main.xml file and copy and paste the bellow code.


<?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="Android iTuts"
        android:paddingLeft="130px"
        android:textAppearance="?  android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your choice"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/checkandroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <CheckBox
        android:id="@+id/checkapple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple" />

    <CheckBox
        android:id="@+id/checkwindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />


    
</LinearLayout>


5.Open the main Activity file and put the given bellow code.


package com.roney.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckboxActivity extends Activity 
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
CheckBox android=(CheckBox) findViewById(R.id.checkandroid);
android.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Android", Toast.LENGTH_LONG).show();
}
}
});
        
CheckBox apple=(CheckBox) findViewById(R.id.checkapple);
        
apple.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Apple", Toast.LENGTH_LONG).show();
}
}
});
CheckBox windows=(CheckBox) findViewById(R.id.checkwindows);
        
windows.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Windows", Toast.LENGTH_LONG).show();
}
}
});
}
}


6.Ouput is shown bellow





If you need to run this application in the android smartphone


Wednesday 19 December 2012

Android Toast Tutorial

Android Toast Tutorial

In this tutorial,i am going to show how to create a simple toast display when a button is clicked.This tutorial is one of the simplest tutorial in the android.


Toast is used to show the notification or message when an action occur.


STEP BY STEP


1.Create a project 


2.In this tutorials when a button is clicked,a toast will show the message.
   Select the main.xml file from res/layout.

Copy and paste the bellow code.

<?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/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Toast" />

</LinearLayout>


3.Select the main activity file
    Copy and pste the given bellow code.


package com.roney.toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ToastActivity extends Activity 
{
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) 
{
    Toast.makeText(getApplicationContext(),
                   "Button is clicked",            Toast.LENGTH_LONG).show();

}
});
}
}

4.Toast.makeText is used to declare the message to display


5.The output is given bellow





If you need to run this application in the android smartphone

Sunday 9 December 2012

Android WebView Tutorial

Android WebView Tutorial

In this tutorial,i am going to teach you how to create an android application to redirect directly to the website which is provided in the project by simply selecting the app shortcuts.


STEP BY STEP

1.Create a new Android Project.



2.Select the main.xml file in the res/layout.
   
   Then select the WebView   
   Component from the Composite as like shown in the bellow figure.






3.Drag and drop the WebView component to the main.xml

   After that the main.xml will looks like as given bellow.



4.Then the main.xml code has been like given bellow.

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

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


5.Select the MainActivity class file.Copy paste the given bellow code.

package com.roney.web;

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

public class SiteviewActivity extends Activity
{
/** Called when the activity is first created. */
    
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.androidituts.blogspot.com");
}
}


Change the required changes in the package name as well as the web url which you need to view.


6.WebView requires INTERNET Permission



  • To add the permission manually.Take AndroidManifest.xml
  • Then take Permissions-->Add-->Usess Permissions-->Select the required   permissions from the combo box provided.

Then the AndroidManifest.xml  code wiil as given bellow.


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  package="com.roney.web"

  android:versionCode="1"

  android:versionName="1.0" >

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

  <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" >
      <activity
          android:name=".SiteviewActivity"
          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>


7.Run the application



If you need to run this application in the android smartphone


Hope you understand how it works.

" Ask your doubts and comments please "

Wednesday 5 December 2012

How to run an Android Project in a Android phone

How to run an Android Project 
in a Android phone

In this tutorial,i am going to show how to run the android project application in a android smart phone.First of all,the developer need to know where the project is saving in the system.


Normally the workspace of the android project of Eclipse will be in  the 
My Document file itself in Windows XP and in User folder in Windows 7.


First step is to find the workspace of Android Project.It is easy to find by searching the path from the Eclipse.


         If you find the workspace folder.Select the ANDROID  project which you want to execute in the smartphone.

Consider the steps
   Hera consider the bellow example.

1.Selecting the project named helloworld from workspace is shown bellow.




2.In the helloworld folder.Select the bin folder





3.In the bin folder,helloworld.apk file is kept as like in the bellow figure.



4.Copy and paste the helloworld.apk file into your smartphone by using  
     bluetooth or wire transfering.


5.After transering the file,the file can be taken where it is copied earlier.
     If you use bluetooth for file transfering the file will be in the Bluetooth    
     folder in the MyFiles folder in the android smartphone.


6.Select the file.After selecting the user interface provide an option to
     Instal or Cancel.


7.Select Instal to instal  the application in the smart phone.


8.After instalation,the shortcut will be available in the icons in the phone.


9.In this case,the icon called helloworld with default android icon will be  
     available along with the other shortcuts.


10.Select the icon.Then the output will be shown as like in the emulator in 
       the Eclipse.


Hope you all understand.

"Please mention comments and queries"


Tuesday 4 December 2012

Android Detect Internet Connection Status


Android Detect Internet Connection Status


     In most of the application running in android needs internet.So it is very important in every application to check whether the internet is available or not.


STEP BY STEP

1.Create a new  Android Project.
2.After creating the project,add the required permissions in your             
    AndroidManifest.xml file.

  • To access the internet we need internet permission.
  • To detect network status we need ACCESS_NETWORK_STATE permission.
Copy paste the bellow code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.detectinternetconnection"
 android:versionCode="1"
 android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name" >
<activity
  android:name=".AndroidDetectInternetConnectionActivity"
  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>
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>


  • To add the permission manually.Take AndroidManifest.xml
  • Then take Permissions-->Add-->Uses Permissions-->Select the required permissions from the combo box provided.

3.Create a class file named as ConnectionDetector.java and apply the
   following code.


package com.example.detectinternetconnection;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;public class ConnectionDetector {private Context _context;public ConnectionDetector(Context context){this._context = context;}public boolean isConnectingToInternet(){ConnectivityManager connectivity = ConnectivityManager) _context              .getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity != null){ NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null)for (int i = 0; i < info.length; i++)if (info[i].getState() == NetworkInfo.State.CONNECTED){return true;}}return false;}}

4.Whenever user needs to check the internet connection,we use the function 
called isConnectingToInternet()

ConnectionDetector cd = new ConnectionDetector(getApplicationContext()); Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false

5.In this tutorial,i am placing a button to show the internet status by a click.


6.Open the main.xml file and copy the bellow code. 
<?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="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Detect Internet Status" /> <Button android:id="@+id/btn_check" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Check Internet Status" android:layout_centerInParent="true"/> <RelativeLayout>


7.Finally paste the given bellow code to your MainActivity.class file

package com.roney.detectinternetconnection; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; public class AndroidDetectInternetConnectionActivity 
extends Activity { // flag for Internet connection status Boolean isInternetPresent = false; // Connection detector class ConnectionDetector cd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnStatus = (Button) findViewById(R.id.btn_check); // creating connection detector class instance cd = new ConnectionDetector(getApplicationContext()); /** *Check Internet status button click event * */ btnStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get Internet status isInternetPresent = cd.isConnectingToInternet(); // check for Internet status if (isInternetPresent) 
{ // Internet Connection is Present // make HTTP requests showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection", "You have internet connection", true); } else { // Internet connection is not present // Ask user to connect to Internet showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection", "You don't have internet connection.", false); } } }); } /** * Function to display simple Alert Dialog * @param context - application context * @param title - alert dialog title * @param message - alert message * @param status - success/failure (used to set icon) * */ public void showAlertDialog(Context context, String title, String message, Boolean status) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Setting Dialog Message alertDialog.setMessage(message); // Setting alert dialog icon alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); // Setting OK Button alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); // Showing Alert Message alertDialog.show(); } }

8.Output is shown bellow.







If you need to run this application in the android smartphone


Hope you understand how it works.

" Ask your doubts and comments please "


THIS IS FEATURED POST 1 TITLE

THIS IS FEATURED POST 1 TITLE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam

Quas molestias excepturi
THIS IS FEATURED POST 2 TITLE

THIS IS FEATURED POST 2 TITLE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam

Impedit quo minus id
THIS IS FEATURED POST 3 TITLE

THIS IS FEATURED POST 3 TITLE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam

Voluptates repudiandae kon
THIS IS FEATURED POST 4 TITLE

THIS IS FEATURED POST 4 TITLE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam

Mauris euismod rhoncus tortor