Thursday, 29 March 2012

Work with contacts

http://app-solut.com/blog/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/
http://chengalva.com/dnn_site/Articles/tabid/41/EntryId/87/Android-What-is-managedQuery-in-Android.aspx
get number in Contacts
   Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};

       
        Cursor people = getContentResolver().query(uri, projection, null, null, null);

        int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        String name;
        String number;
        myTextView = (TextView) findViewById(R.id.myTextView);
        do {
             name = people.getString(indexName);
            number = people.getString(indexNumber);
      //      myTextView.setText(myTextView.getText().toString() + " **** " + number);
           
           
        } while (people.moveToNext());

Tuesday, 27 March 2012

super in android

class ClassOne { public say() { System.out.println("Here goes:"); } }
class ClassTwo extends ClassOne { public say() { super.say(); System.out.println("Hello"); } }
Now, new ClassTwo().say() will output:
Here goes:
Hello
 
more detail
 
http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html
 

Thursday, 22 March 2012

Read write file android


layout.xml

<?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"
    >
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New To Do Item"
    />
    <ListView
        android:id="@+id/myListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

for internal file



 protected void readFile()
    {
        try {
            String file = "input.txt";

            FileInputStream finp = openFileInput(file);
            InputStreamReader isr = new InputStreamReader(finp, "UTF8");
            BufferedReader br = new BufferedReader(isr);
            String line ="";
            while((line = br.readLine()) != null){
                myEditText.setText(line);
            }
            br.close();
            isr.close();
        } catch (Exception e) {
            myEditText.setText(e.toString());
            // TODO: handle exception
        }
    }



protected void writeFile()
    {
        try {
            String file = "input.txt";

            FileInputStream finp = openFileInput(file);
            InputStreamReader isr = new InputStreamReader(finp, "UTF8");
            BufferedReader br = new BufferedReader(isr);
            String line ="";
            int total = 0;
            while((line = br.readLine()) != null){
                //myEditText.setText(line);
                try {
                    total += Integer.parseInt(line);
                } catch(NumberFormatException nfe) {
                   System.out.println("Could not parse " + nfe);
                }
            }
            br.close();
            isr.close();
           
           
            file = "output.txt";

            FileOutputStream fout = openFileOutput(file, MODE_WORLD_WRITEABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fout, "UTF8");
            BufferedWriter bw = new BufferedWriter(osw);

            bw.write(Integer.toString(total));
            //bw.write("helo");

            bw.close();
            osw.close();
           
           
        } catch (Exception e) {
            myEditText.setText(e.toString());
            // TODO: handle exception
        }
    }

file explorer android

Window -> ShowView -> File Explorer

sharedpreferences android tutorial example

activity.java:


import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {
    private  final String MY_NAME = "myname";
    private final String MY_WALLPAPER = "wallpaper";
     public static String MY_PREFS = "MY_PREFS";
     EditText myEditText;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // Inflate your view
        setContentView(R.layout.main);
       
           
       myEditText = (EditText) findViewById(R.id.myEditText);
      
             // Create the array adapter to bind the array to the listview
            // Bind the array adapter to the list view.
             loadPreferences();    
      
    }
   
    @Override
    protected void onPause()
    {
        super.onPause();
         int mode = Activity.MODE_PRIVATE;
        SharedPreferences mySharedPreferences= getSharedPreferences(MY_PREFS,mode);

       
        SharedPreferences.Editor editor = mySharedPreferences.edit();

       
        editor.putBoolean("isTrue", true);
        editor.putFloat("lastFloat", 1f);
        editor.putInt("wholeNumber", 2);
        editor.putLong("aNumber", 3l);
        String currentValue = myEditText.getText().toString();
        editor.putString("textEntryValue", currentValue);
           
       
        editor.commit();
    }
//    @Override
//    protected void onResume()
//    {
//        SharedPreferences mySharedPreferences= getPreferences(0);
//
//       
//        SharedPreferences.Editor editor = mySharedPreferences.edit();
//
//       
//        editor.putBoolean("isTrue", true);
//        editor.putFloat("lastFloat", 1f);
//        editor.putInt("wholeNumber", 2);
//        editor.putLong("aNumber", 3l);
//        editor.putString("textEntryValue", "Not Empty");
//
//   
//        editor.commit();
//    }
    public void loadPreferences() {
            int mode = Activity.MODE_PRIVATE;
            SharedPreferences mySharedPreferences =     getSharedPreferences(MY_PREFS, mode);   
            boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
            float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f);
            int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1);
            long aNumber     = mySharedPreferences.getLong("aNumber", 0);
           
            String stringPreference = mySharedPreferences.getString("textEntryValue","value");
           
           
            myEditText.setText(stringPreference);
           
    }

 layout.xml

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New To Do Item"
    />



Activity life cycle


Thursday, 15 March 2012

clear import

while going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.