Want to learn about how to implement bottom navigation in Flutter app that can run on Android and iOS seamless ? Checkout my article here.
Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts
Flutter code recipe for Hero animation
Checkout my latest post on Flutter code recipe for Hero animation here.
Flutter code recipe for AnimatedOpacity widget
Checkout my latest post on Flutter code recipe for AnimatedOpacity widget here.
Android Model-View-Presenter (MVP) Design Pattern
Checkout my post on Android Model-View-Presenter (MVP) Design Pattern here
Requesting Audio permission at Runtime
Check out how to implement runtime permissions for an Audio recorder sample app in this post.
Getting started with developing Android Apps in Kotlin
Kotlin is official language for Android Apps development. Checkout my post about Getting started with developing Android apps in Kotlin.
Scheduling Repeating Local Notifications using Alarm Manager
Learn about Scheduling Repeating Local Notifications using Alarm Manager in this post.
Adding Menu Items in Navigation Drawer Dynamically
Checkout my post about Adding Menu Items in Navigation Drawer Dynamically here.
How did I integrated a launcher screen in an Android App under 5 minutes
Checkout my latest blog post about Android App Launcher Screen here.
Why Butterknife doesn't work for Android Libraries
Have you tried using Butterknife library to bind Android's xml views for an Android library ? And ran into dreaded this error message:
You're not alone ! Many people like myself tried using wonderful Butterknife in their Android Libraries like they do in their regular phone and tablet projects. Actually, Butterknife is not intended to be used for Android libraries project because R class in library project is not final.
Here's the explanation from Jake himself:
Recent version of Butterknife library (8.5.1) supports library projects as well. Checkout out this link to configure library projects to start using Butterknife in your library projects as well.
Attribute must be constant
You're not alone ! Many people like myself tried using wonderful Butterknife in their Android Libraries like they do in their regular phone and tablet projects. Actually, Butterknife is not intended to be used for Android libraries project because R class in library project is not final.
Here's the explanation from Jake himself:
Values on the[UPDATE]Rclass are notfinalin library modules which makes this library unusable on those modules. This is a limitation of Java, and something this library has decided not to work around. See #2.
Recent version of Butterknife library (8.5.1) supports library projects as well. Checkout out this link to configure library projects to start using Butterknife in your library projects as well.
WebViewOverlay Widget Android Library
I needed a widget that can load a url in WebView in a full-screen closable overlay/modal. I wanted to re-use this new, shiny widget in my other projects as well. So, I decided to upload WebViewOverlay library in a central artifact repository. I chose JCenter because its one of the largest artifact repository for Java and Android libraries and has good integration with Android Studio IDE.
WebViewHelper Library in Android
Recently, I've fiddling around a long lived Widget in Android "WebViews". I created a open source library WebView Helper to help with automatic URL validations and connivence methods to enable and disable JavaScript in a WebView. Don't forget to checkout out this post.
Source code is available on Github here.
Source code is available on Github here.
RecyclerView sample code examples
If you're looking for sample code to get started with RecyclerView in Android, then this post is for you !
Here's link to my github repo to demonstrate Linear and Grid Layout using RecyclerView.
Here's link to my github repo to demonstrate Linear and Grid Layout using RecyclerView.
Convert Canvas into Bitmap and Saving image in Gallery
Have you ever thought of drawing something on Canvas and sharing it with others ? If yes, then this post is for you :)
So, you know that you can draw your stuff on a Canvas view. If you want to share your work with others, you may want to save it as an image. This post shows how to convert a Canvas view into a bitmap. First "Play" menu-item will show a programmatically drawn text on canvas view. "Save" icon will save it in gallery as well as in external SD card.
Details:
First create Custom View called MyCanvas class:
Second, reference MyCanvas from MainActivity class to show it inside ImageView. I've two menu-items, one is for showing the bitmap and another is to take save action :
Here's link to the github repo: https://github.com/ptyagi911/MyCanvasApp
So, you know that you can draw your stuff on a Canvas view. If you want to share your work with others, you may want to save it as an image. This post shows how to convert a Canvas view into a bitmap. First "Play" menu-item will show a programmatically drawn text on canvas view. "Save" icon will save it in gallery as well as in external SD card.
Details:
First create Custom View called MyCanvas class:
package com.teach.mycanvasapp; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class MyCanvas extends View { public MyCanvas(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint pBackground = new Paint(); pBackground.setColor(Color.WHITE); canvas.drawRect(0, 0, 512, 512, pBackground); Paint pText = new Paint(); pText.setColor(Color.BLACK); pText.setTextSize(20); canvas.drawText("This is a sample canvas image", 100, 100, pText); } }
Second, reference MyCanvas from MainActivity class to show it inside ImageView. I've two menu-items, one is for showing the bitmap and another is to take save action :
package com.teach.mycanvasapp; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.media.MediaScannerConnection; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class MainActivity extends AppCompatActivity { ImageView imageView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.show_canvas) { View v = new MyCanvas(getApplicationContext()); Bitmap bitmap = Bitmap.createBitmap(500/*width*/, 500/*height*/, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); v.draw(canvas); imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageBitmap(bitmap); return true; } else if (id == R.id.save_canvas) { saveImageToGallery(); return true; } return super.onOptionsItemSelected(item); } public void saveImageToGallery() { Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); //Writing image to SD card. //It could also be saved to internal storage. // That way, we don't need to have extra permissions File dir = new File("/sdcard/tempfolder/"); try { if (!dir.exists()) { dir.mkdirs(); } File output = new File(dir, "tempfile.jpg"); if (!output.exists()) output.createNewFile(); OutputStream os = null; os = new FileOutputStream(output); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); //Scans image and save it in gallery MediaScannerConnection.scanFile(this, new String[] { output.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.d("Test", "Image saved in gallery!"); } } ); } catch (Exception e) { Log.d("Test", "Exception: " + e.getMessage()); } } }
Here's link to the github repo: https://github.com/ptyagi911/MyCanvasApp
Setting environmental variables for Android development in Mac OSX
Setting environmental variables or paths to Android SDK tools in ~/.bash_profile sounds so simple ! But its not quite standard when it comes to Mac OSX. In linux, you can simply use export command in ~/.bash_profile and your're done. But in Mac OSX, you have couple of options like bash.rc, bash_profile, /etc/profile and environment.plist etc. I was so confused when all I wanted was to simply have a environmental variable for ANDROID_HOME and want to access SDK tools like android and adb from commandline. So, all I had to do was:
- Open Terminal
- Type vi ~/.bash_profile in command line. It creates it, if its already not there
- Type following in vi editor:
export PATH=$PATH:/Users/PTyagi/Developer/android/sdks/android-sdk-macosx/tools:/Users/PTyagi/Developer/android/sdks/android-sdk-macosx/platform-tools
export ANDROID_HOME=/Users/PTyagi/Developer/android/sdks/android-sdk-macosx
Close the Terminal session and open new one and try echo $ANDROID_HOME to check. It should print the path to SDK home.
Android NDK vs Android SDK
This post throws some light on when to use Android SDK over NDK or vice versa. This is a on-going post and will be updated as my research continues on SDK/NDK comparison. Please leave comments if you want to contribute in this list.
NDK
SDK
NDK
- Pros
- Enables legacy code re-use between iOS and Android platforms
- Good for implementing CPU intensive operations that don't allocate much memory like signal processing, physics simulations
- Cons
- Seems to introduce security and stability issues
- NDK activities disables SDK feature use like broadcast receivers, content providers, services. Some better SDK libraries becomes un-usable in NDK.
SDK
- Pros
- Java has superior memory management model
- Superior threading model
- Better exception handling model
- Rich set of libraries
- Superior support for unicode characters
- Cons
Subscribe to:
Posts (Atom)
Scheduling Repeating Local Notifications using Alarm Manager
Learn about Scheduling Repeating Local Notifications using Alarm Manager in this post .
-
Couple of days ago, I started using Google's code projects for Git as well. It required me to setup and use .netrc file. Here are steps...
-
I've been struggling to get this working for about 2 days now. I was able to POST a request directly from sockets, but it took me a whi...
-
Normal structure of build.gradle for an Android library looks like this: apply plugin : 'com.android.library' android { comp...