Posts

Showing posts from June, 2018

Patterns and Regular Expressions

In Java usually validate with some specific characters, we should use Regular Expressions.

Same way in Android, here we use Patterns.

More info about the Patterns: Click here

In android EditText, we can use custom input filter to perform this.

This is for maximum input digits before and after decimal pointer.

CustomInputFilter.Java

public class CustomInputFilter implements InputFilter {
    int maxDigitsBeforeDecimalPoint;
    int maxDigitsAfterDecimalPoint;

    public CustomInputFilter(int maxDigitsBeforeDecimalPoint, int maxDigitsAfterDecimalPoint) {
        this.maxDigitsBeforeDecimalPoint = maxDigitsBeforeDecimalPoint;
        this.maxDigitsAfterDecimalPoint = maxDigitsAfterDecimalPoint;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        StringBuilder builder = new StringBuilder(dest);
        builder.replace(dstart, dend, source
                .subSequence(start, end).toString());
        if (!builder.toStri…

Webview Tricks and Tips

To enable back button should be not close the application and web pages navigation only,

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
               webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


Webview settings:

        WebSettings webSetting = sWebview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDisplayZoomControls(false);
        //Set whether the DOM storage API is enabled.
        webSetting.setDomStorageEnabled(true);
        sWebview.setWebChromeClient(new WebChromeClient());
        sWebview.setWebViewClient(new MyWebViewClient());

To interact between Java and Javascript:

WebAppInterface.Java

import android.app.Activity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

public class WebAppInterface {
    Activity activity;
    ResultInterface resultInterface;
    WebView webView…

Get the Local IP address of your device

Have a great day.


Set the permission in Manifest.xml

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

Then just use below code:

 public String getLocalIpAddress() {
        String firstIP = "192.168.0.1";
        ArrayList<String> ipList = new ArrayList<String>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        Log.d(TAG, inetAddress.getHostAddress().toString());
                        ipList.add(inetAddress.getHostAddress().toString()…