How to play ringtone/alarm/notification sound in Android

Playing the default notification sound of the device is the most effective way to notify a user. This can be established by getting the Uri of the audio file from RingtoneManager. To play default alarm tone , ringtone or notification sound from an android app, the following code snippet is useful.

Play Notification Tone

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Play Alarm Tone

To play an Alarm-tone change TYPE_NOTIFICATION to TYPE_ALARM.

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Set & Play a Tone from Raw resources folder

To set a file from the raw folder of your app just change the uri to get the desired file from raw resources. If you have included a tone called sownd.mp3 in your raw resources folder, all you have to do is change

 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

to

  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");

The code should look like this now

try {
  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");
  // The line below will set it as a default ring tone replace
  // RingtoneManager.TYPE_RINGTONE with RingtoneManager.TYPE_NOTIFICATION
  // to set it as a notification tone
  RingtoneManager.setActualDefaultRingtoneUri(
                    getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                    path);
  Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), path); 
  r.play();
 } 
catch (Exception e) {
 e.printStackTrace(); 
}
 

Android Layout Finder – A useful online tool

Android developers have to write a lot of findViewbyId() codes in their java classes, which is often boring, this can be avoided by either use of  annotation based library (dependency injection frameworks) like Android Annotations, RoboGuice (which the Official document of Android suggests to avoid ) or use Android Layout Finder (an online tool).

Android Layout Finder tool can automate process of writing findViewById() code and thereby we can have JAVA code automated way and can save our valuable time.You just have to paste XML layout code and it gives you JAVA code with findViewById and click listeners.

Give it a try 🙂

 

Useful Android Libraries

There are many third-party libraries for Android but several of them are “must have” libraries that are extremely popular and are often used in almost any Android project. Each has different purposes but all of them make life as a developer much more pleasant. The major libraries are listed below in a few categories.

Standard Pack

This “standard pack” listed below are libraries that are quite popular, widely applicable and should probably be setup within most Android apps:

Name Description
Retrofit A type-safe REST client for Android which intelligently maps an API into a client interface using annotations.
Picasso A powerful image downloading and caching library for Android.
ButterKnife Using Java annotations, makes Android development better by simplifying common tasks.
Parceler Android Parcelable made easy through code generation
IcePick Android Instance State made easy
Crouton Context-sensitive, configurable alert notices much better than toasts
Hugo Easier logging using annotations

Convenience

  • ButterKnife – Using Java annotations, makes Android development better by simplifying common tasks. Downloadand install as a jar. Make sure to setup eclipse integration for this to work.
  • Dagger – A fast dependency injector for Android and Java.
  • Parceler – Android Parcelable made easy through code generation.
  • IcePick – Android Instance State made easy
  • Hugo – Easier logging within your app
  • AndroidAnnotations – Framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what’s really important. By simplifying your code, it facilitates its maintenance
  • RoboGuice – Powerful extensions to Android using dependency injection.
  • Calligraphy – Custom fonts made easy

Extensions

  • Otto – An enhanced Guava-based event bus with emphasis on Android support
  • EventBus – Android optimized event bus that simplifies communication between components.
  • Tape – Tape is a collection of queue-related classes for Android and Java
  • RxJava – Reactive Extensions for the JVM
  • Priority JobQueue – Easier background tasks
  • ACRA – Crash reporting made easy and free. Check the setup instructions and open-source backend.

Networking

  • Picasso – A powerful image downloading and caching library for Android.
  • Ion – Powerful asynchronous networking library. Download as a jar here.
  • Android Async HTTP – Asynchronous networking client for loading remote content such as JSON.
  • Retrofit – A type-safe REST client for Android and Java which intelligently maps an API into a client interface using annotations.

ListView

Easy Navigation

  • PagerSlidingTabStrip – An interactive indicator to navigate between the different pages of a ViewPager.
  • ViewPagerIndicator – Paging indicator widgets compatible with the ViewPager from the Android Support Library and ActionBarSherlock.
  • JazzyViewPager – Pager with more animations
  • ParallaxPager – ViewPager with Parallax scrolling effects
  • SlidingMenu – Library that allows developers to easily create applications with sliding menus like those made popular in the Google+, YouTube, and Facebook apps.
  • MenuDrawer – Library for easy menu drawers
  • Android Satellite Menu – Radial menu which is configurable reminiscent of the “Path” menu style.
  • ArcMenu – Alternate radial menu modeled after the “Path” menu style.
  • AndroidSlidingUpPanel – Sliding Up Panel
  • DraggablePanel – Panels that can be dragged

UI Components

Drawing

Scanning

Persistence

Compatibility

Resources

Check out the following resources for finding libraries:

References

 

Android Libraries and tools collection

A collection of free and opensource libraries and tools for developers that can prove to be very helpful.

 

How to filter logcat in Android Studio?

In logcat of Android Studio there is usually too much output, sometimes a developer would want to filter out these results so that he only gets logs from the application he is debugging. This can be achieved by the following method.
On the left side (right next to the tabs) is an icon with green arrows – it can be toggled on/off to display only logcat from the process selected in the list 🙂

aJJJa

UPDATE:
as of android studio ver 0.4.5 u will get messages from the app that is running only by default. Log cat has a new option (ON by default) which creates an application filter automatically such that only the launched application’s output is shown