Skip to main content

Use Android Application Common Classes

----------------------------

App.java

----------------------------

http://patelprashant1991.blogspot.in/




package com.appname;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.multidex.MultiDex;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.utils.SharePrefrences;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class App extends Application {
    private static final String TAG = App.class.getSimpleName();

    // fullscreen    
    public static boolean blnFullscreenActvitity = false;


    // app folder name   
 public static String APP_FOLDERNAME = "App-name";

    // share pref name   
 public String PREF_NAME = "app-name";

    // class for the share pref keys and valyes get set    
    public static SharePrefrences sharePrefrences;

    // for the Google login    
public static GoogleApiClient mGoogleApiClient;


    // intent passing tags or string key names   
 public static String ITAG_FROM = "from";
    public static String ITAG_TITLE = "title";
    public static String ITAG_DATA= "data";

    // for the app context   
 static Context mContext;

    // for the set app fontface or type face    
static Typeface tfRoboto;


    public static String OP_APINAME = "testapi";


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        MultiDex.install(this);
    }
    // application on create methode for the create and int base values    
@Override
    public void onCreate() {
        super.onCreate();
        try {
            MultiDex.install(this);
            mContext = getApplicationContext();
            sharePrefrences = new SharePrefrences(App.this);
            getFontRoboto();
            createAppFolder();

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    private void createAppFolder() {
        try {
            String sdCardPath = Environment.getExternalStorageDirectory().toString();
            File file2 = new File(sdCardPath + "/" + App.APP_FOLDERNAME + "");
            if (!file2.exists()) {
                if (!file2.mkdirs()) {
                    System.out.println("==Create Directory "+App.APP_FOLDERNAME+"====");
                } else {
                    System.out.println("==No--1Create Directory "+App.APP_FOLDERNAME+"====");
                }
            } else {
                System.out.println("== already created---No--2Create Directory "+App.APP_FOLDERNAME+"====");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static Typeface getFontRoboto()
    {
        tfRoboto = Typeface.createFromAsset(mContext.getAssets(),"fonts/Roboto-Regular.ttf");
        return tfRoboto;
    }






    public static void showToastLong(Context context,String msg)
    {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public static  void showToastShort(Context context,String strMessage)
    {
        Toast.makeText(context,strMessage,Toast.LENGTH_SHORT).show();
    }

    public static  void showSnackBar(View view,String strMessage)
    {
        Snackbar.make(view,strMessage,Snackbar.LENGTH_SHORT).show();
    }

    public static  void showSnackBarLong(View view,String strMessage)
    {
        Snackbar.make(view,strMessage,Snackbar.LENGTH_LONG).show();
    }

    public static  void showLog(String strMessage)
    {
        Log.v("==App==", "--strMessage--"+strMessage);
    }
    public static  void showLogApi(String strMessage)
    {
        Log.v("==App==", "--strMessage--"+strMessage);
    }
    public static  void showLog(String strTag,String strMessage)
    {
        Log.v("==App==strTag=="+strTag, "--strMessage--"+strMessage);
    }


    public static void setTaskBarColored(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
        }
    }
    public static boolean isInternetAvail(Context context)
    {
        ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null)
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
        }
        return false;
    }


    public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }

        return Bitmap.createScaledBitmap(image, width, height, true);
    }



    public static String getOnlyDigits(String s) {
        Pattern pattern = Pattern.compile("[^0-9]");
        Matcher matcher = pattern.matcher(s);
        String number = matcher.replaceAll("");
        return number;
    }
    public static  String getOnlyStrings(String s) {
        Pattern pattern = Pattern.compile("[^a-z A-Z]");
        Matcher matcher = pattern.matcher(s);
        String number = matcher.replaceAll("");
        return number;
    }

    public static  String getOnlyAlfaNumeric(String s) {
        Pattern pattern = Pattern.compile("[^a-zA-Z0-9]");
        Matcher matcher = pattern.matcher(s);
        String number = matcher.replaceAll(" ");
        return number;
    }



    public  void hideKeyBoard(View view)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static  void hideSoftKeyboardMy(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }










    public static void myStartActivity(Activity activity,Intent intent)
    {
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
    }

    public static void myFinishActivity(Activity activity)
    {
        activity.finish();
        activity.overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
    }



    public static void myFinishStartActivity(Activity activity)
    {
        activity.finish();
        activity.overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
    }
}













--------------------------


SharePrefrences.java

---------------------------


package com.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.appname.App;


public class SharePrefrences {
 App appObj;

 public SharePrefrences(App obj) {
  this.appObj = obj;
 }

 public void clearPrefValues() {
  SharedPreferences.Editor pref = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE).edit();
  pref.clear();
  pref.commit();
 }

 public void setPref(String key, String val) {
  SharedPreferences.Editor pref = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE).edit();
  pref.putString(key, val);
  pref.commit();
 }

 public void saveArray(Context context, String[] array, String arrayName) {
  SharedPreferences settings = context.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE);

  SharedPreferences.Editor editor = settings.edit();

  editor.putInt(arrayName + "_size", array.length);
  
  for (int i = 0; i < array.length; i++) {

   editor.putString(arrayName + "_" + i, array[i]);

   //Log.e("arrayname==", arrayName + "_" + i + "" + array[i] + "");
  }
  editor.commit();
  
  //Log.e("Save Succeed?", editor.commit() + "");
 }

 public String loadArrayValue(String arrayName, int index) {
  SharedPreferences settings = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE);
  // int size = settings.getInt(arrayName + "_size", 0);
  /*
   * String array[] = new String[size]; for (int i = 0; i < size; i++)
   * array[i] = settings.getString(arrayName + "_" + i, null);
   */

  //Log.e("arrayname", arrayName);
  //Log.e("indesx", index + "");

  String temp = settings.getString(arrayName + "_" + index, null);
  
  //Log.e("Loaded Array Size", temp);
  return temp;
 }

 public int getArraySize(Context context, String arrayName) {
  SharedPreferences settings = context.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE);
  return settings.getInt(arrayName + "_size", 0);
 }

 public void setPref(String key, int val) {
  SharedPreferences.Editor pref = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE).edit();
  pref.putInt(key, val);
  pref.commit();
 }

 public void setPref(String key, boolean val) {
  SharedPreferences.Editor pref = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE).edit();
  pref.putBoolean(key, val);
  pref.commit();
 }

 public void setPref(String key, Float val) {
  SharedPreferences.Editor pref = appObj.getSharedPreferences(
    appObj.PREF_NAME, Context.MODE_PRIVATE).edit();
  pref.putFloat(key, val);
  pref.commit();
 }

 public String getStringPref(String key) {
  SharedPreferences pref = appObj.getSharedPreferences(appObj.PREF_NAME,
    Context.MODE_PRIVATE);
  return pref.getString(key, "");
 }

 public int getIntPref(String key) {
  SharedPreferences pref = appObj.getSharedPreferences(appObj.PREF_NAME,
    Context.MODE_PRIVATE);
  return pref.getInt(key, 0);
 }

 public boolean getBooleanPref(String key) {
  SharedPreferences pref = appObj.getSharedPreferences(appObj.PREF_NAME,
    Context.MODE_PRIVATE);
  return pref.getBoolean(key, true);
 }

 public float getFloatPref(String key) {
  SharedPreferences pref = appObj.getSharedPreferences(appObj.PREF_NAME,
    Context.MODE_PRIVATE);
  return pref.getFloat(key, 0);
 }

 public String getValueString(Context context, String Key) {
  SharedPreferences settings = context.getSharedPreferences(
    appObj.PREF_NAME, 0);

  return settings.getString(Key, "");
 }

 public void setValueString(Context context, String Key, String Value) {
  SharedPreferences settings = context.getSharedPreferences(
    appObj.PREF_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putString(Key, Value);

  editor.commit();
 }

}

----------------------------
      Thank you...
----------------------------


Comments

Popular posts from this blog

Laravel Free Admin Panel UI and Code

Thanks for share  https://github.com/the-control-group/voyager V oyager - The Missing Laravel Admin Made with  ❤️  by  The Control Group Website & Documentation:  https://voyager.devdojo.com/ Video Tutorial Here:  https://voyager.devdojo.com/academy/ Join our Slack chat:  https://voyager-slack-invitation.herokuapp.com/ View the Voyager Cheat Sheet:  https://voyager-cheatsheet.ulties.com/ Laravel Admin & BREAD System (Browse, Read, Edit, Add, & Delete), supporting Laravel 6 and newer! Want to use Laravel 5? Use  Voyager 1.3 Installation Steps 1. Require the Package After creating your new Laravel application you can include the Voyager package with the following command: composer require tcg/voyager 2. Add the DB Credentials & APP_URL Next make sure to create a new database and add your database credentials to your .env file: DB_HOST=localhost DB_DATABASE=homestead DB_USERNAME=homestead DB...

How To Install And Setup Laravel Latest Version On Windows 10 Using XAMPP

How to install Laravel on Windows using XAMPP Laravel, one of the best PHP framework is getting more popular among developers all around the world. The Model-View-Controller architecture and blade templating engine made Laravel simple and powerful. Here we are going to discuss the steps of installation and setup of Laravel 5.7 on Windows 10 using XAMPP. Installation Follow the steps below to start installing Laravel 5.7 on Windows 10: 1. Install XAMPP XAMPP is the most popular PHP development environment.XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to use. XAMPP can be easily installed using the link below. https://www.apachefriends.org/download.html Laravel 5.7 requires PHP >= 7.1.3. Download the latest version (7.3.3/PHP 7.3.3). I have shown some screenshots of the installation steps below. 2. Start Apache and MySQ...
Share Application - Any social  app  Facebook,Twitter,Instagram,Youtube,Google+....etc.                                      Pate Prashant    Android Code For share any application. Share code for Video,Image,Text.  share on (Twitter,Facebook,Instagram,Google+,Youtube) Some packeges of application name Application Name                                                Package Name Twitter                         ...