Thursday, 30 April 2015

Prevent an AlertDialog from closing on PositiveButton click

In this article, I am going to demonstrate using AlertDialog, which Prevent an AlertDialog from closing on Positive Button click.

Code:

public void ShowDialog()
   {
  
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setCancelable(false);
       builder.setTitle("Sample Dialog");
       builder.setMessage("Test for preventing dialog close");
       builder.setIcon(R.drawable.ic_launcher);
       builder.setPositiveButton("OK", new OnClickListener()
       { 
      @Override public void onClick(DialogInterface dialog, int which) 
      {
       // TODO Auto-generated method stub 
      
      } });
       builder.setNegativeButton("Cancel", new OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     
      } });
       
      final AlertDialog dialog = builder.create();
      dialog.show();
      
    //Overriding the handler immediately after show is probably a better approach than                                      OnShowListener as described below 
      dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
      @Override public void onClick(View v) {
       Boolean wantToCloseDialog = false;
       //Do stuff, possibly set wantToCloseDialog to true then... 
     if(wantToCloseDialog)
     dialog.dismiss();
       //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you              set cancellable to false. 
      } }); 
     
     dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
      Boolean wantToCloseDialog = false; 
     //Do stuff, possibly set wantToCloseDialog to true then... 
     if(wantToCloseDialog)
     dialog.dismiss();
      //else dialog stays open. Make sure you have an obvious way to close the dialog especially if                     you set cancellable to false. 
     } });

    }

Result:


Monday, 27 April 2015

Clear all multiple instances of single app android

In this post we have learned about to Clear multiple instances of single app  android with code.

In Java file you have to mentioned the following code:

if (!isTaskRoot()) { final Intent intent = getIntent(); final String intentAction = intent.getAction(); if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) { finish(); } }

In Manifest file mention the following code for activity
android:launchMode="singleTask"

How to close All opened stack of activities in android

Intent intent = new Intent(this, Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

Android Deep Linking

App Indexing allows you to connect pages from your website with specific content within your smartphone app. This enables smartphone users who have your app installed to open it directly from relevant mobile search results on Google.

For example, imagine you run a recipe website and have an app that can also show your recipe.
Thanks to the app indexing feature, when a Google searcher on a mobile device is shown one of your recipes as a search result, they will now be able to open that result directly in your app if they have it installed.

Here is how to specify a deep link to your app content:

In your Android manifest file, add one or more <intent-filter> elements for the activities that should be launchable from Google Search results.

Add an <action> tag that specifies the ACTION_VIEW intent action.


Add a <data> tag for each data URI format the activity accepts. This is the primary mechanism to declare the format for your deep links.


Add a <category> for both BROWSABLE and DEFAULT intent categories.

BROWSABLE is required in order for the intent to be executable from a web browser. Without it, clicking a link in a browser cannot resolve to your app and only the current web browser will respond to the URL.

DEFAULT is not required if your only interest is providing deep links to your app from Google Search results. However, the DEFAULT category is required if you want your Android app to respond when users click links from any other web page that points to your web site. The distinction is that the intent used from Google search results includes the identity of your app, so the intent explicitly points to your app as the recipient — other links to your site do not know your app identity, so the DEFAULT category declares your app can accept an implicit intent.

Example:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.deeplinkex"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name" >

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="www.example.com"
                    android:pathPrefix="/android"
                    android:scheme="http" />
          
            </intent-filter>
        </activity>
    </application>

</manifest>

Reading a Data from Incoming Intents:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

               Intent intent=getIntent();
               String action=intent.getIntent();
              Uri data=intent.getData();

}

Otuput: