Sunday 25 September 2011

WEB VIEW

JAVA CLASS:

package com.web;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class T_WebViewActivity extends Activity {
WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
       
        mWebView.setWebViewClient(new HelloWebViewClient());
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
   
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
XML FILE:


<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>



SPINNER

JAVA CLASS:

package com.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class T_SpinnerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
}

XML FILE:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/planet_prompt"
    />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/planet_prompt"
    />
</LinearLayout>



TABS

Create Project like:

T_TabsActivity.java


package com.tab;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class T_TabsActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, Tab1.class);

      //Initialize a TabSpec for each tab and add it to the TabHost
      spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                     res.getDrawable(R.drawable.ic_tab_icons))
                 .setContent(intent);
      tabHost.addTab(spec);

      intent = new Intent().setClass(this, Tab2.class);
      spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                        res.getDrawable(R.drawable.ic_tab_icons))
                    .setContent(intent);
      tabHost.addTab(spec);
     
      intent = new Intent().setClass(this, Tab3.class);
      spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                        res.getDrawable(R.drawable.ic_tab_icons))
                    .setContent(intent);
      tabHost.addTab(spec);


        tabHost.setCurrentTab(2);
    }
}

tab1.java,tab2.java,tab3.java


package com.tab;

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

public class Tab1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
    }
}

main.xml


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

tab1.xml,tab2.xml,tab3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Tab 1"
    />
</LinearLayout>








START ACTIVITY FOR RESULT

FIRST CLASS:



public class HelloWorld extends Activity {
/** Called when the activity is first created. */
String s;
TextView t;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView) findViewById(R.id.TextView01);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
Intent intent = new Intent(HelloWorld.this,
SecondActivity.class);
startActivityForResult(intent, 0);


}


});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String s = data.getStringExtra("name");
t.setText(s);


}


}
SECOND CLASS:



public class SecondActivity extends Activity {
EditText te;
String s;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.second);
   //TextView te=(TextView)findViewById(R.id.TextView01);
  te=(EditText)findViewById(R.id.EditText01);
  Button  b=(Button)findViewById(R.id.Button01);
  b.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
s=te.getText().toString();
Intent intent = new Intent();
intent.putExtra("name", s);
setResult(RESULT_OK, intent);
finish();

}


});
  
}
}



PASS THE VALUE ONE ACTIVITY TO ANOTHER


 FIRST CLASS:


           b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
s=te.getText().toString();
                Intent intent = new Intent(First.this,Second.class);
        intent.putExtra("name", s);
StartActivity(intent);

}


});



SECONT CLASS:


    Bundle o=getintent().getExtras();
    String s=o.getString("name");






Thursday 15 September 2011

Getting an Access the java class method through the phonegap


From the PhoneGap download earlier, we need the following two files:
  1. Android/phonegap-1.0.0.jar
  2. Android/phonegap-1.0.0.js

In the root directory of the project you created in Eclipse, create two new directories:
  1. /libs
  2. /assets /www

Now copy
  1. Android/phonegap-1.0.0.jar to /libs 
  2. Android/phonegap-1.0.0.js  to /assets/www

In Eclipse, select the project in the Package Explorer and refresh (F5) the project. The copied file will appear in the project.

Now, create index.html in your www folder and add some code like so:
<!DOCTYPE HTML>


<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>PhoneGap</title>
 <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
 <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0rc2.js"></script>
 <script type="text/javascript" charset="utf-8" src="phonegap-toast.js"></script>
 <script type="text/javascript">
var ready = function() {
   var win = function () {      
     //console.log("registered ");
   },
    fail = function() {
     alert('Fail');
   };
   navigator.toast.showLong('bar');      
 };
 
  document.addEventListener('deviceready', ready, false);  
 </script>
  </head>
  <body>
    <button onclick="navigator.toast.cancel();">cancel</button>
  </body>
</html>

(i)add master.css in the same folder asset/www/

(ii)add two more file in the same directory
       *phonegap-1.0.0rc2.js
       *phonegap-toast.js

Create two java class in the src/

1.> com.chariotsolutions/Toastify
package com.chariotsolutions;

import com.phonegap.*;
import android.os.Bundle;

public class Toastify extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}



2.> com.chariotsolutions.toast.plugin/ToastPlugin

package com.chariotsolutions.toast.plugin;

import android.util.Log;
import android.widget.Toast;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;

public class ToastPlugin extends Plugin {

    private static final String TAG = "ToastPlugin";
    private static final String LONG_TOAST_ACTION = "show_long";
    private static final String CANCEL_ACTION = "cancel";
    private static final int TOAST_MESSAGE_INDEX = 0;

    private Toast toast = null;
    private String toastMessage;
    @Override
    public PluginResult execute(String pluginAction, JSONArray data, String callbackId) {
        Log.d(TAG, pluginAction);
        if (pluginAction.equals(CANCEL_ACTION)) {

            this.ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (toast != null) toast.cancel();
                }
            });
        } else {
            try {
                toastMessage = data.getString(TOAST_MESSAGE_INDEX);
                
            } catch (JSONException e) {
                Log.e(TAG, "Required parameter 'Toast Message' missing");
                return new PluginResult(Status.ERROR);
            }

            if (pluginAction.equals(LONG_TOAST_ACTION)) {
                Log.d(TAG, "long?");
                this.ctx.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toast = Toast.makeText(ctx, toastMessage, Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
            } else {
                this.ctx.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toast = Toast.makeText(ctx, toastMessage, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
            }
        }

        return new PluginResult(Status.OK);
    }
}

Finaly add the some code in Android Manifest file like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.chariotsolutions"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
    />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden" android:name=".Toastify"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data android:name="plugin.ToastPlugin" android:value="com.chariotsolutions.toast.plugin.ToastPlugin"/>     
    </application>
</manifest>

This code definetly run