Showing posts with label Examples. Show all posts
Showing posts with label Examples. Show all posts

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 "

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 "


Tuesday, 27 November 2012

Android Menu Tutorial


Android Menu Tutorial


In this tutorial we are going to learn how to create android menus.In android applications menu is the most important user interface provides some action for the particular views.

         Here we are going to create a 6 menu items.While clicking a single menu a toast message will show.

Step by step actions

1.Create a new android project.

File==>New==>Android Project==>named as AndroidMenusActivity

2.Now create a XML file under res/layout folder and named as menu.xml

3.Open menu.xml and put the bellow code in it.

<?xml version="1.0" encoding="utf-8"?>
   
    <item android:id="@+id/menu_bookmark"
          android:icon="@drawable/icon_bookmark"
          android:title="Bookmark" />
    <item android:id="@+id/menu_save"
          android:icon="@drawable/icon_save"
          android:title="Save" />
    <item android:id="@+id/menu_search"
          android:icon="@drawable/icon_search"
          android:title="Search" />
    <item android:id="@+id/menu_share"
          android:icon="@drawable/icon_share"
          android:title="Share" />
    <item android:id="@+id/menu_delete"
          android:icon="@drawable/icon_delete"
          android:title="Delete" /> 
    <item android:id="@+id/menu_preferences"
          android:icon="@drawable/icon_preferences"
          android:title="Preferences" />
</menu>


4.Now open your main Activity class file,ie.AndroidMenuActivity.java 
and put the following code in it.

package com.androidhive.androidmenus;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    // Initiating Menu XML file (menu.xml)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected",
Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected"
Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected",
Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected"
Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected",
Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected",
Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }   
}

5.Run your project and in the emulator.Click the button named MENU.Then the
final 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