Start another app using its permission

Application 1:

    private static final String DANGEROUS_ACTIVITY_ACTION = "course.labs.permissions.DANGEROUS_ACTIVITY";
...
        startActivity(new Intent(DANGEROUS_ACTIVITY_ACTION));

Application 1 AndroidManifest.xml:

    <uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM" />

Application 2 AndroidManifest.xml:

    <permission
       android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"
       android:protectionLevel="dangerous" >
    </permission>
...
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="course.labs.permissions.DANGEROUS_ACTIVITY" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Android. Intent chooser example

This example is to show intent chooser.
Application A:

        Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
       
        // TODO - Create a chooser intent, for choosing which Activity
        // will carry out the baseIntent
        // (HINT: Use the Intent class' createChooser() method)
        Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT);
       
        // TODO - Start the chooser Activity, using the chooser intent
        startActivity(chooserIntent);

Application B AndroidManifest.xml:

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

Android. startActivityForResult example

Activity A:

static private final int GET_TEXT_REQUEST_CODE = 1;
...
        // TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
        Intent explicitIntent = new Intent(getApplicationContext(), ExplicitlyLoadedActivity.class);
       
        // TODO - Start an Activity using that intent and the request code defined above
        startActivityForResult(explicitIntent, GET_TEXT_REQUEST_CODE);
...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
        Log.i(TAG, "Entered onActivityResult()");
       
        // TODO - Process the result only if this method received both a
        // RESULT_OK result code and a recognized request code
        // If so, update the Textview showing the user-entered text.
        if(requestCode == GET_TEXT_REQUEST_CODE && resultCode==RESULT_OK){
            String input = data.getStringExtra("result");
            mUserTextView.setText(input);
        }
    }

Activity B:

        // TODO - Create a new intent and save the input from the EditText field as an extra
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result", input);
       
        // TODO - Set Activity's result with result code RESULT_OK
        setResult(RESULT_OK, returnIntent);
       
        // TODO - Finish the Activity
        finish();

Expandable GridView inside ScrollView

ExpandedGridView:

package com.globalsolutions.medapp.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;

public class ExpandedGridView extends GridView {

    boolean expanded = false;

    public ExpandedGridView(Context context) {
        super(context);
    }

    public ExpandedGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandedGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded()) {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }
}

XML:

                <com.globalsolutions.medapp.ui.ExpandedGridView
                   android:id="@+id/gvTypes"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:numColumns="2" >
                </com.globalsolutions.medapp.ui.ExpandedGridView>

Activity or Fragment:

ExpandedGridView gvTypes = (ExpandedGridView) rootView.findViewById(R.id.gvTypes);
....
gvTypes.setAdapter(typesListAdapter);
gvTypes.setExpanded(true);

Android. Alert dialog. Delete or cancel

In this example, I will show how to show alert dialog to delete something or cancel it. But you can use in different situations:

            AlertDialog.Builder alert_delete = new AlertDialog.Builder(this);

            alert_delete.setTitle("Delete");
            alert_delete.setMessage("Are you sure to delete bookmark folder?");

            alert_delete.setPositiveButton("Delete",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // delete bookmark from database
                            // NOTE: all words also will be deleted from coldata
                            // table
                            myDatabase.deleteBookmarkFolder(info.id);
                            // update bookmarks list
                            displayBookmarks();
                        }
                    });

            alert_delete.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // Canceled.
                        }
                    });
            alert_delete.show();

Android. Listview not wrap_content inside ScrollView

I faced problem when I tried to put ListView inside ScrollView. ListView is not wrapping content. It is only showing first item on the list. In this post I will try to explain how to make ListView wrap content.
First of all, create this class – ExpandedListView.java:

package com.globalsolutions.medapp.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ListView;

public class ExpandedListView extends ListView
{
    private android.view.ViewGroup.LayoutParams params;
    private int oldCount = 0;

    public ExpandedListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (getCount() != oldCount)
        {
            int height = getChildAt(0).getHeight() + 1 ;
            oldCount = getCount();
            params = getLayoutParams();
            params.height = getCount() * height;
            setLayoutParams(params);
        }

        super.onDraw(canvas);
    }
}

In your xml file, write this instead of ListView tag:

            <com.globalsolutions.medapp.ui.ExpandedListView
               android:id="@+id/lvButtons"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginBottom="@dimen/margin_between_text_and_buttons"
               android:layout_marginTop="@dimen/margin_between_text_and_buttons"
               android:background="@color/app_background"
               android:divider="@color/list_divider" >
            </com.globalsolutions.medapp.ui.ExpandedListView>

In your activity, initialize your custom ListView:

ExpandedListView mDrawerList = (ExpandedListView) findViewById(R.id.lvButtons);

Splash screen

I am working on one project. I have seen nice piece of code to implement Splash Screen Activity with some delay. In this case delay is 3 secs.

public class Splash extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
       
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           
            @Override
            public void run() {
                //****************************************
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);      
                overridePendingTransition(0, 0);

                finish();
            }
        }, 1000*3);
    }
}

How to handle configuration changes?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        getLastNonConfigurationInstance();
.....
    @Override
    protected void onDestroy() {
        super.onDestroy();
        onRetainNonConfigurationInstance();
    }