Android. Auto complete TextView with ArrayAdapter

        // Create an ArrayAdapter containing country names
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, COUNTRIES);

        // Set the adapter for the AutoCompleteTextView

Android. Working with fragments

Add new fragment

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(, mFriendsFragment);

Replace existing fragment with its state

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(, mFeedFragment);

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:

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

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

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

Application B AndroidManifest.xml:

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

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);
    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");

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

Expandable GridView inside ScrollView


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) {

    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;

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        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;


                   android:numColumns="2" >

Activity or Fragment:

ExpandedGridView gvTypes = (ExpandedGridView) rootView.findViewById(;

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.setMessage("Are you sure to delete bookmark folder?");

                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // delete bookmark from database
                            // NOTE: all words also will be deleted from coldata
                            // table
                            // update bookmarks list

                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // Canceled.

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 –

package com.globalsolutions.medapp.ui;

import android.content.Context;
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);

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


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

               android:divider="@color/list_divider" >

In your activity, initialize your custom ListView:

ExpandedListView mDrawerList = (ExpandedListView) findViewById(;

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 {
    protected void onCreate(Bundle savedInstanceState) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                overridePendingTransition(0, 0);

        }, 1000*3);