Android back button event with example

Android back button event with example

Android device back button is to navigate from current activity to previous activity. In that case we are using below code for handing event on back button.

Declare variable in class :

1. boolean doubleBackToExitPressedOnce = false;

2. put code after onCreate method on an activity.

@Override
 public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }

Regex Character Classes with example

Regex Character Classes with example

Regex Character Classes :

[abc] : a,b or c characters only

[^abc] : Any character except a, b or c character.

[a-zA-Z] : a through z or A through Z, inclusive range.

[a-d[m-p]] : a through d or m through p

[a-z&&[def]] : d, e or f intersection.

[a-z&&[^bc]] : a through z except b and c

[a-z&&[^m-p]] : a through z and not m through p


4. Regex qualifiers :

X? : X occurs once or not at all.

X+ : X occurs once or more times.

X* : X occurs 0 or more times.

X{n} : X occurs n times only.

X{n,} : X occurs n or more times.

X{y,z} : X occurs at least y times but leass than z times.

import java.util.regex.*;

class RegexExample4{
public static void main(String args[]){

System.out.println("? quantifier ....");
System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time)
System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (a m and n comes more than one time)
System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?", "am"));//false (a or m or n must come one time)

System.out.println("+ quantifier ....");
System.out.println(Pattern.matches("[amn]+", "a"));//true (a or m or n once or more times)
System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a comes more than one time)
System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a or m or n comes more than once)
System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z and t are not matching pattern)

System.out.println("* quantifier ....");
System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (a or m or n may come zero or more times)

}
What is regex in Java and How to use it.

What is regex in Java and How to use it.

Java Regex :

Java Regex and regular expression is an API which define pattern for searching or manipulating strings.

It provides following classes and interface for regular expression.

(i)   MatcherResult Interface
(ii)  Matcher class
(iii) Pattern class
(iv)  PatternSyntaxExeption class

Matcher class implements MatcherResult Interface which contains following methods.

(i) boolean matches() : test whether the regular expression matched the pattern.

(ii) boolean find() : find the next expression that matches the pattern.

(iii) boolean find(int start) : find the next expression that matches the pattern from the given start number.

Pattern Class : it is compiled version of a regular expression.

(i) static Pattern compile(String regex) : compile the given regex and return instance of Pattern class.

(ii) Matcher matcher(CharSequence input) : creates the matcher that matches the given input.

(iii) String Pattern() : return the regex pattern.

(iv) static boolean matches(String regex, CharSequence input) : it works as the combination of the compile and matcher methods.

Ex :

import java.util.regex.*;

public class RegexExample{

public static void main(String ar[]){

Pattern p = Pattern.compile(".s");
Matcher m = p.matcher("as");

boolean b1 = m.matches();

// OR

boolean b2 = Pattern.compile(".s").matcher("as").matches();

// OR

boolean b3 = Pattern.matches(".s","as");


}
What is Java Bean with Example

What is Java Bean with Example

Java Bean : Java bean is a class which contains all the properties are private and contains getter/setter methods corresponding to that.

A public non-argument constructor is present in it.

It implements Serializable interface which have no methods defined in it.

Bean is basically used for serialization of data and maintain its state during transfer.

public class Employee implements Serializable{

   private int id;
   private String name;  
   private int salary;

   public Employee() {}

   public Employee(String name, int salary) {
      this.name = name;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName( String name ) {
      this.name = name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}
How to check internet is connected  or not in android

How to check internet is connected or not in android

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class NetworkCheck {

    public static boolean hasInternetConnection(Context context)
    {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetwork != null && wifiNetwork.isConnected())
        {
            return true;
        }
        NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetwork != null && mobileNetwork.isConnected())
        {
            return true;
        }
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected())
        {
            return true;
        }
        return false;
    }
}

put below permissions in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Replace html contents using jQuery

Replace html contents using jQuery

$('li').html('New HTML');  // this replaced all list items on your page with specified contents.

$('div').html('New Div Contents');  // this replaced all div contents on your page with specified contents.

$('body').html('New Body Contents'); // this replaced all contents of body part of your page.

$('p').html('New Paragraph contents'); // this replaced all paragraphs on your page with specified contents. 
What is $(document).ready() ?

What is $(document).ready() ?

What is $(document).ready() ?

Before you can safely use jQuery to do anything to your page, you need to ensure that the page is in a state where its can be manipulated.

like as :

$(document).ready(function(){

alert("Page is ready for manipulation.");
});


$(document) is used for creating object of our page document.

ready() is used for indication for page is in ready state.