Skip to main content

Posts

How to Take Screenshot of Current activity and Share Options

Sometimes we have to share the Report or Final receipt Screen  from the Current Application  .In this scenario we take screenshot of Current activity and then share this as image file using sharing Options . So lets move to the Coding Part without wasting time . 1: public class MainActivity extends AppCompatActivity { 2: File imagePath; 3: @Override 4: protected void onCreate(Bundle savedInstanceState) { 5: super.onCreate(savedInstanceState); 6: setContentView(R.layout.activity_main); 7: getSupportActionBar().setDisplayHomeAsUpEnabled(true); 8: ImageView imageView=(ImageView)findViewById(R.id.share); 9: imageView.setOnClickListener(new View.OnClickListener() { 10: @Override 11: public void onClick(View v) { 12: Bitmap bitmap = takeScreenshot(); 13: saveBitmap(bitmap); 14: shareIt(); 15: } 16: }); 17: } 18: public Bitmap takeScreenshot(...

Android Multi Select Dialog Box

Making Multi Select Dialog box is Pretty Simple and it can be achieved by few lines of coding . So lets move to the Coding Part . Steps: 1.    Create an String Array named "items" 2.   Initialize ArrayList itemsSelected and add if item is selected or remove if not selected . 3.   Get values of selected Items in itemsSelected and use it where ever you want . 1: import android.app.AlertDialog; 2: import android.app.Dialog; 3: import android.content.DialogInterface; 4: import android.os.Bundle; 5: import android.support.v7.app.ActionBarActivity; 6: import java.util.ArrayList; 7: public class MainActivity extends ActionBarActivity { 8: @Override 9: protected void onCreate(Bundle savedInstanceState) { 10: super.onCreate(savedInstanceState); 11: setContentView(R.layout.activity_main); 12: Dialog dialog; 13: final String[] items = {" C", " JAVA", " Kotlin", " Python", ...

How to Find specific App Installed in Mobile or Not

In Android sometimes we have to determine that the Specific app is installed in mobile or Not .So here is Code snippet to find the specific package name application installed or not . public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add respective layout setContentView(R.layout.main_activity); // Use package name which we want to check boolean isAppInstalled = appInstalledOrNot("com.check.application"); // App package here if(isAppInstalled) { //This intent will help you to launch if the package is already installed Intent LaunchIntent = getPackageManager() .getLaunchIntentForPackage("com.check.application"); startActivity(LaunchIntent); Log.i("Application is already installed."); } else { // Do whatever we want to do if applicatio...

how to parse XML Programmatically in Android

In Android some times we have to parse XML data to use in Application .So here the example for Parsing the XML data . Step 1: Create XMLParser.java Class and paste the code like :- import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import java.io.IOException; import java.io.StringReader; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class XMLPa...

Auto Close Dialog Using Timer In Android

Here i will show you how to use auto close dialog after a particular time using timer class in android .So lets start with coding part :- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Auto-closing Dialog"); builder.setMessage("After 7 second, this dialog will be closed automatically!"); builder.setCancelable(true); final AlertDialog dlg = builder.create(); dlg.show(); final Timer t = new Timer(); t.schedule(new TimerTask() { public void run() { dlg.dismiss(); // when the task active then close the dialog t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report } }, 7000); // after 7 second (or 7000 miliseconds), the task will be active.                 

How to use SQLite Database to perform database operations in Android

We all know that the SQLite is use in Android to Local Storage .SQLite is an opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. Lets Understand this using Code Snippets :- First Create a Class named DatabaseHelper.Java  which extends SQLiteOpenHelper Class  public class DatabaseHelper extends SQLiteOpenHelper { public static final String DB_NAME = "AnyNameDB"; public static final String TABLE_NAME = "Key"; public static final String COLUMN_ID = "id"; public static final String COLUMN_NAME ="keyword"; public static final String COLUMN_ADD = "url"; private static final int DB_VERSION = 1; public DatabaseHelper(Context context) { super(context,DB_NAME,null,DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql =...

How to Generate QR code from Text

In this article i will share you the code snippet to generate the QR code from any Text. Before this i want to brief you about QR code. QR Code:  QR code is the trademark for a type of matrix barcode first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. So lets Start with the Coding Part : Bitmap TextToImageEncode(String Value) throws WriterException { BitMatrix bitMatrix; try { bitMatrix = new MultiFormatWriter().encode( Value, BarcodeFormat.DATA_MATRIX.QR_CODE, QRcodeWidth, QRcodeWidth, null ); } catch (IllegalArgumentException Illegalargumentexception) { return null; } int bitMatrixWidth = bitMatrix.getWidth(); int bitMatrixHeight = bitMatrix.getHeight(); int[] pixels = new int[bitMatrixWidth * bitMatrixHeight]; for (int y = 0; y ...

How to Check Internet Connectivity Programmatically

Most of Android Applications use Internet Connection for Performing their Task .So here is Some code Snippet to Check whether the App is Connected to Internet Connection Or Not ? So lets Start :- Step 1:  First we Create a Utility Class fro Checking Internet Connection  ApplicationUtility.Java public class ApplicationUtility { ConnectivityManager connectivityManager; NetworkInfo info; public boolean checkConnection(Context context) { boolean flag = false; try { connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); info = connectivityManager.getActiveNetworkInfo(); if (info.getType() == ConnectivityManager.TYPE_WIFI) { System.out.println(info.getTypeName()); flag = true; } if (info.getType() == ConnectivityManager.TYPE_MOBILE) { System.out.println(info.getTypeName()); flag = true; } ...

How to Check the Activity open First time using Shared Preference

Sometimes we want to show some App Introduction and other things as per our requirements so here i am sharing code snippets to determine that the activity open first time using Shared Preference  . So Lets Start :-  for example you want to open App Introduction of your app before going to Main Activity Class so in Main Activity Check that the Activity Opening First time or not Using these lines of Code. Thread t = new Thread(new Runnable() { @Override public void run() { // Intro App Initialize SharedPreferences SharedPreferences getSharedPreferences = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); // Create a new boolean and preference and set it to true Boolean isFirstStart = getSharedPreferences.getBoolean("firstStart", true); // Check either activity or app is open very first time or not and do action if (isFirstStart) { // ...

How to Capture or Upload Image from Gallery using File Provider

We often Pick images from Gallery to upload in android app but sometime when tried in Android version greater than Marshmallow it throw an error that it does not have permission to Uri. So here i am telling you how to use concept of File provider in Android Version after Android Marshmallow .So lets Start from beginning :- Step 1: in AndroidManifest.xml add these Permissions <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> Step 2: in Same AndroidManifest.xml file add  <provider             android:name="android.support.v4.content.FileProvider"             android:authorities="${applicationId}.provider"             android:exported="false"             android:grantUriPermissions="tr...