• 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;
        }
    }