• Monday, December 26, 2016

    Android - Creating a circle button widget with text inside.

    Syed Masoom Rizvi

    How to create a circle/round button in android?

    Step1: Create a drawable xml in res/drawable folder with name "button_round.xml"

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#60d8ff" android:width="5dp" /> <solid android:color="#007aff"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector>


    Step 2: Use this drawable in normal android button background to make it circular.
    <Button android:id ="@+id/button1" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_round" android:padding="15dp" />

    Tuesday, December 13, 2016

    Android - Get all available storage path in android like internal storage and sdcard.

    Syed Masoom Rizvi
    Use this class to get all available storage on your android:

    public class StorageUtils {
    
        private static final String TAG = "StorageUtils";
    
        public static class StorageInfo {
    
            public final String path;
            public final boolean readonly;
            public final boolean removable;
            public final int number;
            StorageInfo(String path, boolean readonly, boolean removable, int number) {
                this.path = path;
                this.readonly = readonly;
                this.removable = removable;
                this.number = number;
            }
    
            public String getDisplayName() {
                StringBuilder res = new StringBuilder();
                if (!removable) {
                    res.append("Internal Storage");
                } else if (number > 1) {
                    res.append("SD card " + number);
                } else {
                    res.append("SD card");
                }
                if (readonly) {
                    res.append(" (Read only)");
                }
                return res.toString();
            }
        }
    
        public static List<StorageInfo> getStorageList() {
    
            List<StorageInfo> list = new ArrayList<StorageInfo>();
            String def_path = Environment.getExternalStorageDirectory().getPath();
            boolean def_path_removable = Environment.isExternalStorageRemovable();
            String def_path_state = Environment.getExternalStorageState();
            boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
                    || def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
            boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    
            HashSet<String> paths = new HashSet<String>();
            int cur_removable_number = 1;
    
            if (def_path_available) {
                paths.add(def_path);
                list.add(0, new StorageInfo(def_path, def_path_readonly, def_path_removable, def_path_removable ? cur_removable_number++ : -1));
            }
    
            BufferedReader buf_reader = null;
            try {
                buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
                String line;
                while ((line = buf_reader.readLine()) != null) {
    
                    if (line.contains("vfat") || line.contains("/mnt") || line.contains("sdcardfs")) {
                        Log.d(TAG, line);
                        StringTokenizer tokens = new StringTokenizer(line, " ");
                        String mPath = tokens.nextToken(); //device
                        String mount_point = tokens.nextToken(); //mount point
                        if (paths.contains(mount_point)) {
                            continue;
                        }
                        String unused = tokens.nextToken(); //file system
                        List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
                        boolean readonly = flags.contains("ro");
    
                             if (!line.contains("/mnt/secure")
                                    && !line.contains("/mnt/asec")
                                    && !line.contains("/mnt/obb")
                                    && !line.contains("/dev/mapper")
                                    && !line.contains("tmpfs")) {
                                 File testFile  = new File(mount_point);
                                 if(testFile.isDirectory() && testFile.listFiles() != null) {
                                     paths.add(mount_point);
                                     list.add(new StorageInfo(mount_point, readonly, true, cur_removable_number++));
                                 }
                            }
                    }
                }
    
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                if (buf_reader != null) {
                    try {
                        buf_reader.close();
                    } catch (IOException ex) {}
                }
            }
            return list;
        }
    }

    Friday, November 11, 2016

    8. Java - Java Modifiers

    Syed Masoom Rizvi
    Java Modifiers Tree

    What is Java Modifier?

    Java Modifier is a keyword used for changing the behavior of variable or class in term of its usage.
    Java Modifier is categorized in two parts:-

    1. Access Modifier
    2. Non Access Modifier 

    How can we use a modifier?

    Simple usage of modifier:

    modifier variableName;

    where modifier is a keyword i.e. public, private, protected, static, final etc...


    What is Java Access Modifier?

    Java Access Modifier is like a security key which gives different access to classes, variables, methods or constructor .  In simple word, it controls the access level and it use for security reason.
    The four Java Access Modifier are:
    1. default :-> It gives access to package (no need to write it).
    2. public :-> Give access to the world.
    3. private :-> Give access to the class only.
    4. protected :-> Give access to package and all sub classes.
    Example:

    Tuesday, November 1, 2016

    7. Java - Data Types and Variable

    Syed Masoom Rizvi
    java data type

    What is datatype?
    A variable is the name that point to a specific information in computer memory(RAM).
    Every information has specific datatype. And each datatype require different storage size in RAM. Therefore we declare variable with a datatype to allocate different storage size with its type. 

    Why we require datatype?
    • For allocating different storage size in ram, because ram size is limited.
    • To different between its type.
      Explanation: If we declare a variable with String a = "5"  and we want to add 12 to variable a. Then the value of a become 512. To remove this problem datatype is useful too.

    Basic syntax for writing a variable with a data type is:

    datatype myVariableName;

    There are two kind of data types in Java:
    • Primitive Data Type
    • Reference/Object type.

    Primitive data type:

    int: it is used for integral values.
    • Allocated size: 32 bit.
    • Minimum value:  - 2,147,483,648 (-2^31).
    • Maximum value: 2,147,483,647(inclusive) (2^31 -1).
    • Default value:  0 (zero).


    byte: It is used to save space in large arrays, a byte is 4 times smaller than int.
    • Allocated size: 8 bit.
    • Minimum value: -128 (-2^7).
    • Maximum value: 127 (inclusive)(2^7 -1).
    • Default value: 0 (zero).


    short: It is also same as int, but it is 2 times smaller than an int.
    • Allocated size: 16 bit.
    • Minimum value: -32,768 (-2^15).
    • Maximum value: 32,767 (inclusive) (2^15 -1).
    • Default value: 0 (zero).


    long: it is used for a wider range value. 
    • Allocated size: 64 bit.
    • Minimum value: -9,223,372,036,854,775,808(-2^63).
    • Maximum value: 9,223,372,036,854,775,807 (inclusive)(2^63 -1).
    • Default value: 0L (zero). {In long datavalue, datatype should contains L as a suffix.}

    float: it is used for precise values.
    • Allocated size: 32-bit IEEE 754 floating point (Single Precision).
    • Float data type is never used for precise values such as currency.
    • Float is mainly used to save memory in large arrays of floating point numbers
    • Default value: 0.0f(zero). {In double datavalue, datatype should contains f as a suffix.}

    double: it is used for decimal values.
    • Allocated size: 64-bit IEEE 754 floating point (Double Precision).
    • This data type is generally used as the default data type for decimal values, generally the default choice.
    • Double data type should never be used for precise values such as currency.
    • Default value: 0.0d (zero). {In double datavalue, datatype should contains d as a suffix.}

    boolean: It is used for simple flags true/false condition.
    • Allocated size: 1 bit.
    • There is only two value true or false.
    • Default value: false.

    char: it is used to store a character value.
    • Allocated size: 16 bit.
    • Minimum value: '\u0000' (or  0{zero}).
    • Maximum value: '\uffff' (or 65,535 inclusive).
    • It is used to store any character.
    Reference/Object data type: It is used to allocate storage space for a object in computer memory (RAM).
    • Class objects and various type of array variables come under reference datatype.
    • Default Value: null.
    • Reference variable points to an object(which is instance of a class) in the computer memory.
    • Example:
      Car myCar = new Car();

      Here Car is a class, myCar is a variable and new is a keyword used to create a new copy of class as a object.

    Deep and more detail in Java datatype.

    Java Literals
    A Java Literal is a source-code representation of fixed value. Without any computation, literal can be assigned to a variable.
    It can assign to any primitive datatype variable.


    byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.

    Prefix 0(zero) is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For Example:

    int decimal = 123;
    int octal = 0124;
    int hexa = 0x32;

    String Literal in Java is specified with a back slash ("\") symbol and a character after this symbol. They can directly write in string like this: "Hello\nWorld".

    These are String Literal in Java (which are same as in other language.):
    NotationDescription
    \bBackspace
    \dddOctal Character
    \fFormfeed
    \nFor writing a new line.
    \rCarriage return
    \sSpace
    \tTab
    \'Single Quote
    \"Double Quote
    \\Back Slash
    \uxxxxHexadecimal unicode character, where is x has some integer value.

    Monday, October 31, 2016

    Start Learning Java from Beginner to Expert

    Syed Masoom Rizvi

    Class is the collection of variables i.e. fields and methods. And a program is collection of classes.
    The basic code for declaring a class in Java is:


    class MyClass{

    //Your codes go here.

    }

    When declaring a class, the name of class should be start with capital letter and cannot contains space or special character. If you want to write "My first class" then you should have to write it in Camel Case like: "MyFirstClass".

    CamelCase is very good approach in programming because it is human readable.


    Thursday, February 11, 2016

    Android - How to show javascript alert in WebView

    Syed Masoom Rizvi

    How to show javascript alert in WebView?

    In android, WebView does not show javascript alert dialog in activity. Here I give a solution that can easily handle the alert dialog with user customization. 

    Write the following code under activity after initiating the WebView.


    WebView wv= (WebView) findViewById(R.id.webView);
    final Context mc=this;