Monday, June 27, 2022

Set a limit on EditText character

const val RESTRICTED_CHARS = "€<>;"
fun restrictCharacterFilter(regexString:String) = InputFilter { source, _, _, _, _, _ ->
if (source != null && regexString.contains(source)) {
""
} else null

}


textInputEditText.filters = arrayOf(restrictCharacterFilter(RESTRICTED_CHARS), 
emojiFilter(), InputFilter.LengthFilter(30))

Wednesday, August 3, 2016

Force Localisation Android

 Step 1:Create One java class which extends from 'Application'  
 public class LocaleApplication extends Application{  
   private Locale mLocale = null;  
   @Override  
   public void onConfigurationChanged(Configuration newConfig) {  
     super.onConfigurationChanged(newConfig);  
     if (mLocale != null) {  
       newConfig.locale = mLocale;  
       Locale.setDefault(mLocale);  
       getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());  
     }  
   }  
   @Override  
   public void onCreate() {  
     super.onCreate();  
    updateLocale(BuildConfig.LOCALE);  
   }  
   public void updateLocale(Locale locale) {  
     Configuration config = getBaseContext().getResources().getConfiguration();  
     if (!config.locale.equals(locale)) {  
       mLocale = new Locale(locale.getLanguage(),locale.getCountry());  
       Locale.setDefault(mLocale);  
       config.locale = mLocale;  
       getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());  
       // reload challenge and achievement strings - these were created with the previous locale  
     }  
   }  
 }  
 Step 2: Mention this class name in Android manifest file under <application> tag  
 <application  
   android:name="LocaleApplication"  
   android:allowBackup="true"  
   android:icon="@mipmap/ic_launcher"  
   android:label="@string/app_name"  
   android:supportsRtl="true"  
   android:theme="@style/AppTheme">  
 Step 3: Take appropriate resource folders like values-es-rCO,values-el etc.   
 Step 4: Add below steps in build.gradle for resource configurations.  
 defaultConfig {  
   applicationId "com.example.ankitpr1.forcelocalisation_android"  
   minSdkVersion 15  
   targetSdkVersion 23  
   versionCode 1  
   versionName "1.0"  
   buildConfigField 'java.util.Locale', 'LOCALE_SPANISH_LATAM', 'new java.util.Locale(\"es\",\"US\")'  
   buildConfigField 'java.util.Locale', 'LOCALE_Greek', 'new java.util.Locale(\"el\")'  
 }  
 productFlavors {  
   esus{  
     applicationId 'com.colombia.us'  
     resConfigs 'es-rCO'  
     buildConfigField 'java.util.Locale', 'LOCALE', 'new java.util.Locale(\"es\",\"CO\")'  
     versionCode 3  
     versionName "3.0"  
     // ! any change in Locale, should be applied in defaultConfig as well  
   }  
   Greek {  
     applicationId 'com.test.greek'  
     resConfigs 'el'  
     buildConfigField 'java.util.Locale', 'LOCALE', 'new java.util.Locale(\"el\")'  
     versionCode 2  
     versionName "2.1"  
     l  
   }  
 }  
 //List all flavours in project to be used for check current flavour against others  
 productFlavours.all {  
   flavour ->  
     def flavourName = String.valueOf(flavour.name)  
     defaultConfig.buildConfigField 'String', 'Flavour_' + flavourName , '\"' + flavourName + '\"'  
 }  

Tuesday, March 1, 2016

Build variant type according to Locale

 Add below code under android--defaultConfig in build.gradle file  
 buildConfigField 'java.util.Locale', 'LOCALE_SPANISH_LATAM', 'new java.util.Locale(\"es\",\"US\")'  
 productFlavors {  
   esus{  
     applicationId 'com.praveena.test.us'  
     resConfigs 'es-rUS'  
     buildConfigField 'java.util.Locale', 'LOCALE', 'new java.util.Locale(\"es\",\"US\")'  
   }  
 }  
 By using this we can select the build variant type while running.  

Sunday, December 6, 2015

Web Service Call using Angulat JS


By using below code base we can do the web service call in Angular JS

 var req = {  
   method: 'GET',  
   url: LOGIN_URL,  
   params: {  
     registrationId: document.getElementById("username").value,  
     dob: document.getElementById("dob").value  
   }  
 }  
 $http(req).success(function(data, status, headers, config) {  
   var result = data;  
   if (result.status == 2) {  
     localStorage.permId = document.getElementById("username").value;  
     localStorage.applicantName = result.applicantName;  
     localStorage.Dob = result.Dob;  
     localStorage.mobileno = result.mobileno;  
     localStorage.Gender = result.Gender;  
     window.location.href = 'profile.html';  
   } else {  
     $scope.loading = false;  
     alert(data.returnObj2);  
     //Alert.render(data.returnObj2);  
   }  
 }).error(function() {  
   $scope.loading = false;  
   alert("Service is not available");  
   //AlertLogin.render();  
   //Alert.render("Service is not available");  
 });  

Thursday, December 3, 2015

Save Layout as image,Pdf to sdcard android


      private void saveLayoutAsPDFToSdcard() {  
           String PDF_FILE = "mnt/sdcard/invoice.pdf";  
           try {  
                View content = rootView.findViewById(R.id.ll_upperLayout);  
                content.setDrawingCacheEnabled(true);  
                content.setDrawingCacheEnabled(true);  
                // this is the important code :)   
                // Without it the view will have a dimension of 0,0 and the bitmap will be null       
                content.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),   
                      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
                content.layout(0, 0, content.getMeasuredWidth(),content.getMeasuredHeight());   
                content.buildDrawingCache(true);  
                Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());  
                content.setDrawingCacheEnabled(false);  
             Document document = new Document();  
             PdfWriter.getInstance(document, new FileOutputStream(PDF_FILE));  
             document.open();  
             ByteArrayOutputStream stream = new ByteArrayOutputStream();  
             bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);  
             byte[] byteArray = stream.toByteArray();  
             addImage(document,byteArray);  
             document.close();  
                 Log.v("PRAVEENA","====saved==");  
           } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                Log.v("PRAVEENA","====failed==107"+e.toString());  
                e.printStackTrace();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                Log.v("PRAVEENA","====failed 11111=="+e.toString());  
                e.printStackTrace();  
           } catch (DocumentException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
            }  
      private void addImage(Document document, byte[] byteArray) {  
           Image image = null;  
           try   
        {  
          image = Image.getInstance(byteArray);   
        }   
        catch (BadElementException e)   
        {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
        }  
        catch (MalformedURLException e)   
        {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
        }  
        catch (IOException e)   
        {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
        }  
         // image.scaleAbsolute(150f, 150f);  
         try   
         {  
          document.add(image);  
        } catch (DocumentException e) {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
        }  
      }  
 private void saveLayoutAsPNGToSdcard() {  
           try {  
                View content = rootView.findViewById(R.id.ll_upperLayout);  
                content.setDrawingCacheEnabled(true);  
                content.setDrawingCacheEnabled(true);  
                // this is the important code :)   
                // Without it the view will have a dimension of 0,0 and the bitmap will be null       
                content.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),   
                      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
                content.layout(0, 0, content.getMeasuredWidth(),content.getMeasuredHeight());   
                content.buildDrawingCache(true);  
                Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());  
                content.setDrawingCacheEnabled(false);  
 //               Bitmap bitmap = content.getDrawingCache();  
                File file,f = null;            
                if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))   
                  {   
                     file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");  
                     if(!file.exists())  
                    {  
                     file.mkdirs();  
                     }   
                     f = new File(file.getAbsolutePath()+file.separator+ "filename"+".png");  
                  }  
                 FileOutputStream ostream = new FileOutputStream(f);                    
                 bitmap.compress(CompressFormat.PNG, 10, ostream);  
                 ostream.close();  
                 Log.v("PRAVEENA","====saved==");  
           } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                Log.v("PRAVEENA","====failed==107"+e.toString());  
                e.printStackTrace();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                Log.v("PRAVEENA","====failed 11111=="+e.toString());  
                e.printStackTrace();  
           }  
            }   
To save Layout as pdf to sdcard we need itextpdflib

Friday, September 4, 2015

Display Owners information without unlocking your Phone

We cans see the owners information without unlocking your phone,This will be helpful in emergency cases.

Procedure to set the owners infomation using android mobiles

Goto Settings -->Security -->Owners Info --> Enable show owners info on lock screen and enter information there like emergency contact numbers etc.

Wednesday, May 6, 2015

Android Studio shortcuts like Eclipse

As far we are good to use the shortcut controls in eclipse. 
Now we are migrating to Android studio where the shortcuts are different while comparing Android studio and Eclipse.

 Please follow the below steps to enable the eclipse shortcut controls in Android studio if you want to use the same eclipse controls in Android Studio.
  1. Click on File menu
  2. Navigate to “Settings”
  3. Click on Keymap section which is available in left side panel
  4. You can see the keymaps dropdown on the right side panel. Select “Eclipse” from 
  5. Finally, save the settings by clicking on OK button.
Now you will be able to use the same eclipse controls in Android studio.

 Goto Settings -->Security -->Owners Info --> Enable show owners info on lock screen and enter information there like emergency contact numbers etc.