Tuesday, August 7, 2012

Overwrite MENU key to create custom menu for Andorid

When user click on the hardware MENU key, Android OS will pop-up option menu; refer to the post "Create OptionsMenu in /res/menu/menu.xml" to know how to implement a formal OptionMenu. Alternatively, we can overwrite the default menu to create our custom menu as shown below:

Create /res/layout/menu.xml to make the layout of our manu. You can replace the android:src items for your own icon.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
<ImageButton
 android:id="@+id/opt1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
<ImageButton
 android:id="@+id/opt2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
<ImageButton
 android:id="@+id/opt3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
</LinearLayout>


package com.CustomMenu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class CustomMenuActivity extends Activity {
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
   }
  
   @Override
   public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU) {
     if(menuDialog == null) {
      menuDialog = new MenuDialog(this); 
     }
     menuDialog.show();
     return true; 
    }
    return super.onKeyUp(keyCode, event); 
   }
  
   MenuDialog menuDialog;
   private class MenuDialog extends AlertDialog{
    
    ImageView Option1, Option2, Option3;
    
    public MenuDialog(Context context) {
     super(context);
     setTitle("Custom Menu");
     View menu = getLayoutInflater().inflate(R.layout.menu, null);
     setView(menu); 
     
     Option1 = (ImageView)menu.findViewById(R.id.opt1);
     Option2 = (ImageView)menu.findViewById(R.id.opt2);
     Option3 = (ImageView)menu.findViewById(R.id.opt3);
     
     Option1.setOnClickListener(new View.OnClickListener() {
    
    public void onClick(View v) {
     // TODO Auto-generated method stub
     Toast.makeText(CustomMenuActivity.this, "Option 1", Toast.LENGTH_LONG).show();
     dismiss();
    }
   });
     
     Option2.setOnClickListener(new View.OnClickListener() {
    
    public void onClick(View v) {
     // TODO Auto-generated method stub
     Toast.makeText(CustomMenuActivity.this, "Option 2", Toast.LENGTH_LONG).show();
     dismiss();
    }
   });
     
     Option3.setOnClickListener(new View.OnClickListener() {
      
      public void onClick(View v) {
       // TODO Auto-generated method stub
       Toast.makeText(CustomMenuActivity.this, "Option 3", Toast.LENGTH_LONG).show();
       dismiss(); 
      } 
     });
     
    }
   }
}

Query data from the Contacts content provider for Android

Android ships with a number of content providers for common data types (audio, video, images, personal contact information, and so on). You can see some of them listed in the android.provider package. You can query these providers for the data they contain (although, for some, you must acquire the proper permission to read the data).



package com.GetContact;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.TextView;

public class GetContactActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tvContacts = (TextView)findViewById(R.id.contacts);
        
        String[] contacts = new String[] {People.NAME, People.NUMBER};
        Uri contentUri = People.CONTENT_URI;
        Cursor cursor = managedQuery(contentUri, contacts, null, null, null);
        
        String textContacts = "";
        
        if (cursor.moveToFirst()) {
         String myname = null;
         String mynumber = null;
         do {
          textContacts = textContacts
           + cursor.getString(cursor.getColumnIndex(People.NAME)) + " : "
           + cursor.getString(cursor.getColumnIndex(People.NUMBER)) + "\n";
         } while (cursor.moveToNext()); 
         
         tvContacts.setText(textContacts);
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView  
    android:id="@+id/contacts"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>



In order to query data from the Contacts content provider, we have to grant permission of "android.permission.READ_CONTACTS".

HttpClient and HttpGet for Android

org.apache.http.client.HttpClient is a Interface class for an HTTP client. HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests.

org.apache.http.client.methods.HttpGet provide HTTP GET method.



package com.AndroidHttpGet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidHttpGetActivity extends Activity {
 
 final String httpPath = "http://feeds.feedburner.com/AndroidCoding";
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       TextView text = (TextView)findViewById(R.id.text);
      
       HttpClient httpclient = new DefaultHttpClient();
       HttpGet httpget = new HttpGet(httpPath);
       try {

   HttpEntity httpEntity = httpclient.execute(httpget).getEntity();
   
   if (httpEntity != null){
    InputStream inputStream = httpEntity.getContent();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder stringBuilder = new StringBuilder();
      
    String line = null;
      
    while ((line = bufferedReader.readLine()) != null) {
     stringBuilder.append(line + "\n"); 
    }

    inputStream.close();
      
    text.setText(stringBuilder.toString());
   }
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(AndroidHttpGetActivity.this, e.toString(), Toast.LENGTH_LONG).show();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(AndroidHttpGetActivity.this, e.toString(), Toast.LENGTH_LONG).show();
  }
   }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:id="@+id/text"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>


note: In order to access internet, uses-permission of "android.permission.INTERNET" have to be defined in AndroidManifest.xml.