Wednesday, 28 October 2015

How to open list of Browsers in Android

              
                    Here I am going to explain,how to open the list of browsers in android application?

                           


Step 1) activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AndroidDhina Web browser Tutorial" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="Open" />

</RelativeLayout>

Step 2)Mainactivity.java


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{

Button openBrowser;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

openBrowser= (Button) findViewById(R.id.button1);

openBrowser.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{

Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("https://www.google.co.in/"));

String title = "Complete Action Using";

Intent chooser = Intent.createChooser(intenttitle);
startActivity(chooser);

}
});
}

}

Happy Coding:)

Saturday, 24 October 2015

How to create alert dialog outside of an android application


         Here I am going to explain how to create an Alert Dialog outside of android application.For this I am using BroadcastReceiver,The BroadcastReceiver  triggers whenever the charger connected to the device.


Step 1)Test.java

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

public class Test extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Step 2)activity_main.xml

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina Popup Testing" />

</RelativeLayout>

                                                    

Step 3)Implementing the BroadcastReceiver

    When implementing a broadcast receiver you have to do two steps:

                    You have to create a subclass of Android’s BroadcastReceiver
                    You have to implement the onReceive() method

   
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BatteryReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) 
{

    Intent intent = new Intent(context, MainActivity.class);
    intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent .addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    intent .putExtra("value", "BatteryConnected");
    context.startActivity(intent );

}

}



Step 4)Registering a BroadcastReceiver in the manifest file

    <receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true" >
           <intent-filter>
            <action  android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            </intent-filter>
     </receiver>




Step 5)MainActivity.java
   
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import  android.os.Bundle;

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
  String newMessage = getIntent().getExtras().getString("value");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("New Message")
.setMessage(newMessage)
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() 
    {
public void onClick(DialogInterface dialog, int id)
              {
dialog.cancel();
finish();
              }
     })
.setNegativeButton("No", new DialogInterface.OnClickListener()
                {
      public void onClick(DialogInterface dialog, int id)
             {
dialog.cancel();
finish();
             }                    
   });
AlertDialog alert builder.create();
alert.show();
  }
}

Step 6)AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testpopup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />                

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Test"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity" >
        </activity>

        <receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
            <action  android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


Happy Coding :)

    

Monday, 12 October 2015

Call JavaScript function inside WebView from Android Activity


   Here I am going to explain Call JavaScript function inside WebView from Android Activity
                                     
                                                             
Step 1)MainActivity .java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity 
 {
WebView webView;
  EditText text;
  Button Show;
@Override
protected void onCreate(Bundle savedInstanceState) 
   {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity MyActivity = this;
   text=(EditText)findViewById(R.id.textValue);
   Show=(Button)findViewById(R.id.textButton);
webView= (WebView) findViewById(R.id.webview);
   getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

MyActivity .setTitle("Loading...");
MyActivity .setProgress(progress * 100);

if (progress == 100)
MyActivity .setTitle("Android Dhina");
}
});
webView.setWebViewClient(new WebViewClient());
webView.addJavascriptInterface(new MyJavaScriptInterface (this), "Android");

webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/web.html");

  Show.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) 
          {
            webView.loadUrl("javascript:callFromAndroidActivity
                                  (\""+text.getText().toString()+"\")"); }
});

public class MyJavaScriptInterface {
 Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }
   
    public void showToast(String toast){
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
   
    public void openAndroidDialog(){
     AlertDialog.Builder myDialog
     = new AlertDialog.Builder(MainActivity.this);
     myDialog.setTitle("DANGER!");
     myDialog.setMessage("You can do what you want!");
     myDialog.setPositiveButton("ON", null);
     myDialog.show();
    }

}
}


Step 2)activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
     android:orientation="vertical"
    android:layout_height="match_parent">
   
    <EditText
        android:id="@+id/textValue"
        android:textColor="@android:color/holo_blue_light"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/textButton"
        android:text="Show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

      <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Step 3)assets/web.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Android Dhina</title>

<h1>Android Dhina Webview</h1>
<p id="mytext">Dhina</p></head>
<body>

<script language="javascript">
function callFromAndroidActivity(msg)
 { 
        document.getElementById("mytext").innerHTML = msg; 
 }

</script>

</body>
</html>


Happy Coding :)

Call Java function from JavaScript over Android WebView


     Here I am going to explain how to call a java function from webview


                                                      
Step 1)MainActivity .java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity MyActivity = this;
webView= (WebView) findViewById(R.id.webview);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

MyActivity .setTitle("Loading...");
MyActivity .setProgress(progress * 100);

if (progress == 100)
MyActivity .setTitle("Android Dhina");
}
});
webView.setWebViewClient(new WebViewClient());
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/web.html");

}

public class WebAppInterface {

Context context;

WebAppInterface(Context c) {
context= c;
}

public void showToast(String toast) {
Toast.makeText(contexttoast, Toast.LENGTH_SHORT).show();
}

}

}

Step 2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>


Step 3)assets/web.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Android Dhina</title>

<h1>Android Dhina Webview</h1>
</head>
<body>
<input type="button" value="Call Function"
onClick="showAndroidToast('Hello Dhina!')" />

<script language="javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
 }
</script>

</body>
</html>


Happy Coding :)
    

How to avoid onItemSelected calls during initialization


   Here I am going to explain how to keep onItemSelected from firing off on a newly instantiated Spinner


       int check = 0;
       Spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id)

{
check check + 1;
if (check > 1) 
                              {

new LongOperation().execute("");


}
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

}

});

Convert String to Date Format


    Here I am going to explain how to convert string to date and then formatting the date to required format.
    
                                         

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView newDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

newDate=(TextView)findViewById(R.id.newDate);
String myFormat= "yyyy-MM-dd";
String finalString = "";
try {
DateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
Date date = (Date) formatter .parse("2015 Oct 09");
SimpleDateFormat newFormat = new SimpleDateFormat(myFormat);
finalStringnewFormat .format(date );
newDate.setText(finalString);
} catch (Exception e) {

}

}

}


Happy Coding :)

Wednesday, 7 October 2015

How to make a Phone call from Android activity



    Here I am going to explain how to make phone call from android activity


 Step 1)Add the below permission in your manifest file

  <uses-permission android:name="android.permission.CALL_PHONE" />


Step 2)Add the below code to activity


       Intent callIntent = new Intent(Intent.ACTION_CALL);
       callIntent .setData(Uri.parse("tel:+91-XXXXXXXXX"));
       startActivity(callIntent );


Happy Coding:)

Marquee effect in Android TextView


    Here I am  going to explain how to set Marquee effect in Android Text View.
                                           


Step 1).Mainactivity.java

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview= (TextView) this.findViewById(R.id.textview);
textview.setSelected(true);

textview.setEllipsize(TruncateAt.MARQUEE);
textview.setSingleLine(true);
}

}

Step 2)activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:text="***************Android Dhina Marquee  effect in textview ***************************** "
        android:textColor="#9bebe4" />

</RelativeLayout>

Happy Coding:)