Wednesday, May 20, 2015

How to integrate WhatsApp with an Android app or website — prefill messages using whatsapp://

We can integrate WhatsApp with an Android app in two ways:

Methods

  1. Using the Android Intent system (covered in my previous post: http://karjol-gt.blogspot.in/2015/05/how-to-share-media-or-message-text-with.html).
  2. Using the custom URL scheme provided by WhatsApp.

We will discuss the second option in this post.

WhatsApp custom URL scheme (website or app)

WhatsApp provides a custom URL scheme to interact with the app. If you have a website and want to open a WhatsApp chat with a pre-defined message, you can use WhatsApp’s custom URL scheme.

Opening whatsapp://send?text= followed by the text to send will open WhatsApp, allow the user to choose a contact, and prefill the input field with the specified text.

Example (on a website)

Use a normal anchor tag. The message must be URL-encoded (spaces become %20, etc.). Example:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

When the user clicks this link, WhatsApp (if installed) opens, the user selects a contact, and the predefined text "Hello World!" appears in the chat input.

Notes and compatibility

  • URL-encode the message text. Unencoded spaces and special characters may break the URL.
  • This method relies on the whatsapp:// custom scheme and therefore requires the WhatsApp app to be installed on the device. Behavior can vary by platform, browser, and WhatsApp version. Test on the target devices and browsers.
  • Consider adding a fallback for environments where the whatsapp:// scheme is unsupported (for example, conditionally offering the WhatsApp FAQ or guiding users to install WhatsApp).

Summary

That’s it. Happy Coding…

Reference

Tuesday, May 19, 2015

Share Text and Media with Social Apps in Android — Intent examples and best practices

I was developing an android application for a customer. One requirement was to share some media and text with the installed social Android apps like Google+, Email, Gmail, Facebook, Drive, Messaging, WhatsApp etc. This post shows how to share predefined text and media from your app.

Overview

In Android you can share text or media with other installed apps using Intents. Below I demonstrate a simple example (sharing a predefined text) from our Panchanga app. I show the original minimal code used in the app and then give recommended, modern improvements (chooser, file sharing, FileProvider, and package checks).

Create the menu item

The original app added a menu item called "Share". The minimal code used was:

MenuItem share;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    share = menu.add("Share");
    share.setIcon(R.drawable.share);
    share.setOnMenuItemClickListener(this);

    return true;
}

Note: share.png was added to the drawable folder.

Implement the onMenuItemClickListener (original example)

This is the original minimal onMenuItemClick implementation in the article:

public boolean onMenuItemClick(MenuItem menuItem) {

    if (menuItem == share) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
    }

    return true;
}

Here, the system picks all the social apps installed on the device and shows them in the chooser. You can select the required app to send. The text "This is my text to send." is what is sent to other apps.

Below are the images from the original article showing the share chooser and examples:

share1

share2

share3

Share only with a specific app (original note)

If you want to target only a specific app instead of showing a chooser, you can call setPackage on the Intent. Example from the original article:

sendIntent.setPackage("com.whatsapp");

The article linked to the Intent.setPackage docs: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

"com.whatsapp" is the package name of WhatsApp; use the package name of the app you want to target.


Recommended improvements and modern best practices

Keep the original minimal examples (above) for reference, but use these improvements for production apps.

1) Always use a chooser so the user sees a consistent share sheet

Replace startActivity(sendIntent) with:

startActivity(Intent.createChooser(sendIntent, "Share via"));

This forces the chooser UI and avoids launching a default app unexpectedly.

2) If targeting a specific package, check that the package exists first

PackageManager pm = getPackageManager();
if (pm.getLaunchIntentForPackage("com.whatsapp") != null) {
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
} else {
    // App not installed — fall back to chooser
    startActivity(Intent.createChooser(sendIntent, "Share via"));
}

Wrap startActivity in try/catch(ActivityNotFoundException) as a final fallback.

3) Sharing files (images, PDFs) — use EXTRA_STREAM and FileProvider on Android N+

When sharing a file (image, PDF, etc.) use a content:// URI and FileProvider for API 24+ to avoid FileUriExposedException.

Example (sharing a single image):

File file = new File(getFilesDir(), "shared_image.jpg");
Uri contentUri = FileProvider.getUriForFile(context, "your.package.name.fileprovider", file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share image"));

You must declare a FileProvider in your AndroidManifest.xml and add a file_paths.xml resource describing the shared paths. See the official docs for details: https://developer.android.com/reference/androidx/core/content/FileProvider

4) Sharing multiple files

Use ACTIONSENDMULTIPLE with an ArrayList in EXTRA_STREAM:

ArrayList<Uri> uris = new ArrayList<>();
// add multiple content URIs
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images"));

5) Attach subject or title when appropriate

For email apps, add EXTRASUBJECT and/or EXTRATITLE:

sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out Panchanga");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");

Note: some apps (notably Facebook) ignore prefilled text in shared content due to their platform policy.

6) MIME types and behaviour

Set the right MIME type: text/plain for text, image/* for images, application/pdf for PDFs. A wrong MIME type may hide some target apps from the chooser.

7) Runtime permissions

If the files you want to share are on external storage, ensure you handle runtime permissions (READEXTERNALSTORAGE) on Android 6.0+.

8) Troubleshooting and UX notes

  • If the chooser shows no apps, check the MIME type and ensure there is at least one app that handles the action and MIME type.
  • If you target a specific package and the app is not installed, handle it by notifying the user or falling back to the chooser.

Conclusion

The original minimal example is valid for sharing simple text. For robust production behavior across Android versions, add a chooser, handle URIs with FileProvider, set correct MIME types, and guard setPackage with a PackageManager check. That will give a safer and more consistent user experience.

Happy Coding!

Sudoku Game Development in Android — Features & Google Play Link

Greetings of the Day!!!

When I was learning Android, I thought of developing a game. The easiest was a Sudoku game. And when I was reading an Android book I saw a basic example of the game and thought of using it and building on top of it. So I started it and completed it for a single game. But I did not share it with anybody. When I was cleaning up the things today I saw that game and thought of sharing it.

Overview

I extended the basic example into a small Android Sudoku app with multiple levels, game persistence, and an in-app Settings screen. The app opens with the previous settings and can remember and resume the last-in-progress game.

Features

  • Levels: Easy, Medium and Hard
  • Multiple puzzles per level; when you select a level the app randomly picks a puzzle for you to play
  • Resume: the app remembers the current game state so you can leave mid-way and continue later
  • Settings: options to show hints and to play music; settings are stored using SharedPreferences so they persist across app restarts

Technical notes

  • Level selection: the app presents predefined puzzles grouped by difficulty and randomly selects a puzzle from the chosen level.
  • Persistence: current game state is saved so a paused or interrupted game can be resumed. Settings (hints, music) are implemented with SharedPreferences so they persist when the app is closed and reopened.

(If you want more developer-focused detail — e.g., code snippets showing how game state is serialized or how SharedPreferences are used — I can add minimal, copy-paste-ready examples.)

Screenshots

aboutpng

splash

main

Capture

level

settings

Download

You can download the game from Google Play:

https://play.google.com/store/apps/details?id=com.marutsoft.sudoku

Notes and next steps

  • This post is a short introduction and screenshots from the app. If you want a follow-up developer post, I can add concrete implementation details: puzzle storage format, random selection algorithm, how I save and restore game state, and code showing SharedPreferences usage for settings. Tell me which part you want first and I will publish a technical walk-through.

Tuesday, May 12, 2015

Change TabHost Tab Colors in Android — Set Selected & Unselected Colors

Introduction

In Android you can change the color of TabHost tabs. By default the TabHost appearance is grey. In this example the author changes the unselected tab color to #A45D0A (dark orange / brown tone) and the selected tab color to #A42C0A (dark red).

This article shows how to initialize tabs (each associated with a ListView in the original example), set initial colors, and update colors inside an onTabChanged listener.

Prerequisites

  • A layout that contains a TabHost (for example, with id R.id.TabHost01).
  • A helper method getTabIndicator(Context, String, Drawable) used in the original example to create a tab indicator view — keep your implementation of that helper.

Code example (corrected and formatted)

// Required imports
import android.graphics.Color;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.OnTabChangeListener;

// ... inside your Activity / Fragment where popupView is available:
final TabHost tabs = (TabHost) popupView.findViewById(R.id.TabHost01);
tabs.setup();

// Optional: show dividers and custom divider drawable
tabs.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
tabs.getTabWidget().setDividerDrawable(R.drawable.custom_tab_indicator_divider);

// Create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator(getTabIndicator(tabs.getContext(), "Dialed", getResources().getDrawable(R.color.white)));
tabs.addTab(tab1);

// Create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator(getTabIndicator(tabs.getContext(), "Missed", getResources().getDrawable(R.color.white)));
tabs.addTab(tab2);

// Create tab 3
TabHost.TabSpec tab3 = tabs.newTabSpec("tab3");
tab3.setContent(R.id.listView03);
tab3.setIndicator(getTabIndicator(tabs.getContext(), "Received", getResources().getDrawable(R.color.white)));
tabs.addTab(tab3);

// Set initial colors: first tab selected color, others unselected
for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
    if (i == 0)
        tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A42C0A")); // selected
    else
        tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A45D0A")); // unselected
}

// Handle tab changes
tabs.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        // Reset all tabs to unselected color
        for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
            tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A45D0A")); // unselected
        }

        // Set the currently selected tab color
        tabs.getTabWidget().getChildAt(tabs.getCurrentTab()).setBackgroundColor(Color.parseColor("#A42C0A")); // selected
    }
});

Notes and explanations

  • The original article used #A45D0A for unselected tabs and #A42C0A for the selected tab. These hex values are preserved above.
  • The helper method getTabIndicator(…) used in setIndicator(…) is not included in the original post — keep your implementation or replace with a standard TextView/ImageView as needed.
  • The code corrects several small issues from the original post (case-sensitive method names and inconsistent variable names). For example, Color.parseColor must be used (not parsecolor), and the listener should reference the same TabHost variable (tabs.getCurrentTab()).

Best practices and improvements to consider

  • Use color resources (res/values/colors.xml) instead of hard-coded hex values:
<color name="tab_selected">#A42C0A</color>
<color name="tab_unselected">#A45D0A</color>

Then use ContextCompat.getColor(context, R.color.tabselected) or getResources().getColor(R.color.tabselected, null) on newer APIs.

  • Prefer using a selector drawable for tab backgrounds instead of changing background color in code. A selector makes state handling simpler and moves styling into XML.

  • Consider modern alternatives: TabHost is an older pattern. For new apps, use TabLayout with ViewPager or ViewPager2 (from the Material components / AndroidX) for better integration and styling.

  • Accessibility: ensure tabs have readable text (high contrast) and consider contentDescription when tabs are primarily icon-based.

  • On API 23+ use ContextCompat.getColor(context, … ) or ResourcesCompat to avoid deprecated calls.

Conclusion

Now populate the data for the ListViews associated with each tab. That’s it — Happy Coding..

Saturday, May 9, 2015

SRSM Panchanga — Manmatha Naam Samvastara data added (Android) – Download & Screenshots

Greetings of the day!!!

Summary

First of all, sorry for the delay in updating the Panchanga app with Manmatha naam data. And thanks to all those who reminded me — I received more than 100 emails requesting the Manmatha naam samvastara update. Earlier I used to get the soft copy of the Panchanga from SRSMutt at least two months before Ugadi; this time they did not send it due to technical issues, so I had to convert the data manually.

What changed

  • Manmatha naam samvastara Panchanga data has been added to the SRSM Panchanga Android app.
  • As of now only the English version of the app is updated. The Kannada version is in progress and will be made available soon.

Where to download

You can download the app from Google Play:

SRSM Panchanga — https://play.google.com/store/apps/details?id=com.sarga.simplecal

(Click the link above to open the Play Store on your device.)

Quick install steps

  • Open the Play Store link on your Android device.
  • Tap Install.
  • After installation, open the app and verify the Panchanga data under the Manmatha naam samvastara section.

Screenshots

Below are some of the app screenshots showing the updated data and features.

SRSM Panchanga screenshot 1

SRSM Panchanga screenshot 2

SRSM Panchanga screenshot 3

SRSM Panchanga screenshot 4

SRSM Panchanga screenshot 5

SRSM Panchanga screenshot 6

SRSM Panchanga screenshot 7

SRSM Panchanga screenshot 8

SRSM Panchanga screenshot 9

SRSM Panchanga screenshot 10

SRSM Panchanga screenshot 11

SRSM Panchanga screenshot 12

SRSM Panchanga screenshot 13

SRSM Panchanga screenshot 14

SRSM Panchanga screenshot 15

SRSM Panchanga screenshot 16

Notes & feedback

  • The Kannada version is in progress and will be released when the Kannada data is ready.
  • If you face issues after installing, please reply to the original post's contact email or use the blog's contact form.

Changelog (brief)

  • Added: Manmatha naam samvastara data (English app release).
  • Pending: Kannada language release (work in progress).

Thank you for your patience and for the many reminders and emails — they helped prioritize this update.

--

(Author)