Posts

Convert Collection to Array

1. Convert Set to Array of String           final HashMap<String, String> orderingOptionMap = new HashMap<String, String>();          orderingOptionMap.put("1", "Name: A-Z");          orderingOptionMap.put("2", "Name: Z-A");          orderingOptionMap.put("3", "Age: Ascending");          orderingOptionMap.put("4", "Age: Descending");          String[] orderingOption =   orderingOptionMap.keySet().toArray(new String[orderingOptionMap.size()]);           

String constant java class(created- StringConstant.java) vs string resource (strings.xml)?

Below are few points to consider before defining any string in any of the file- 1. Organize based on what makes sense!     Like, put string constants that highlights database connection credentials, table name, column name or any internal used constant in Java class where else the strings that are displayed to the user should go into strings.xml file. In short, strings.xml holds DISPLAY strings. 2. Constant declared in strings.xml file will be available only to context. Hence, whenever you want to use them make sure you have context to access them. Like in Adapters.

Check If table exist or not

​To check if table exists or not , below is the code snippet-    import java.sql.Connection;    import java.sql.DatabaseMetaData;    import java.sql.ResultSet;     public class Main {        public static void main(String[] argv) throws Exception {          Connection c = null;          DatabaseMetaData dbm = c.getMetaData();          ResultSet rs = dbm.ge​​tTables(null, null, "employee", null);          if (rs.next()) {            System.out.println("Table exists");           } else {            System.out.println("Table does not exist");           }        }     } For Android internal memory, SQLLite -      public boolean doesTableExists(Stri...

Difference between dp, dip, sp, px

Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi). Screen density is grouped as low, medium, high and extra high. Resolution is the total number of pixels in the screen. dp: Density Independent Pixel , it varies based on screen density. In 160 dpi screen, 1 dp = 1 pixel. Except for font size, use dp always. dip: dip == dp. In earlier Android versions dip was used and later changed to dp. sp: Scale Independent Pixel , scaled based on user’s font size preference. Fonts should use sp. Always use dp and sp only. sp for font sizes and dp for everything else.

Read/Write Images

To read Image from Gallery- I worked on ImageButton to get Image from gallery: private void uploadImageClicked() {     new Thread(new Runnable() {         Intent intent = new Intent(Intent.ACTION_PICK);         public void run() {             openGallery(intent);         }     }).start(); } private void openGallery(Intent intent) {     intent.setType("image/*");       startActivityForResult(intent, 0); } The above piece of code open the Gallery to select the Image as desired. Here, we create an INTENT in order to access Gallery and hence, set the path as "image/*" and call startActivityForResult(intent, 0) so as to start the activity that utilize intent. To write Image from Gallery-     @Override     public void onActivityResult(int requestCode, int resultCode, Intent data) {       ...

Picasso Vs Glide

Below link gives great explaination between them- https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Multiple Inheritance is not supported in Java

Below diagram explains is called Diamond Problem because of the inheritance structure.       A foo()           / \          /   \ foo() B C foo()          \   /           \ /         D foo() Multiple inheritance does cause casting, constructor chaining,etc problems. Also, multiple inheritance leads to complexity. Hence, Java do not support multiple inheritance. But recently, in JAVA 8, this problem is handled in different way, that is, the compiler will throw exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.