Wednesday, April 25, 2012

Android: Load bitmap from SD Card

       String fileSavePath = Environment.getExternalStorageDirectory() + "abc.png";
       Bitmap bitmap = BitmapFactory.decodeFile(fileSavePath);
       ImageView imageView = (ImageView)findViewById(R.id.imageview);
       imageView.setImageBitmap(bitmap);

Monday, April 16, 2012

Android: UI thread example

Some cases cannot be ran on a thread processor such as Updating UI.
In this case, you run on UI Thread:

             new Thread() {
                    @Override
                    public void run() {
                           try {
                                 // code runs in a thread
                                 activity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                               // code runs in a UI(main) thread
                                        }
                                 });
                           } catch (final Exception ex) {
                           }
                    }
             }.start();

To make this easier (or harder in some cases), there's AsyncTask.

Android: new thread example

Simple example:

        new Thread() {
            @Override
            public void run() {
                try {
                    // do something useful
                } catch (Exception e) {
                   
                }
            }
        }.start();

Or use AsyncTask

Android: Open browser with link example

You simply call a new Intent with URL link like following:


    public void openBrowser(final String link) {
       if(link.contains("http")) {
             Intent i = new Intent(Intent.ACTION_VIEW);
             i.setData(Uri.parse(link));
             startActivity(i);
       }
    }


Friday, April 13, 2012

Android: How to refresh on transparent background webview?

If you set Webview's background to be transparent, then whenever you load a new page, contents will overlap.

To solve this issue:


       webView.clearView();
       webView.loadUrl(url);

Clear view before you load something else.

Thursday, April 12, 2012

Android Webview with transparent background

Yes, it is possible!

           webView.setBackgroundColor(0x00FFFFFF);

However, if in your AndroidManifest.xml contains:

             android:hardwareAccelerated="true"
webView's background, for some reason, does not change to transparency.
(It took me while to figure this out!)

This is how to disable hardware acceleration on the view that contains webview:

             view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Wednesday, April 11, 2012

Android facebook api: what is Android Key Hash?

When registering Facebook App, it requires you to type 'Android Key Hash'.
How to get Android Key Hash?







4 Prerequisites:
1. Must have Eclipse installed and android sdk installed
2. Locate 'debug.keystore' : usally at - C:\Users\(user name)\.android\debug.keystore
3. Locate 'keytool.exe' : usally at - C:\Program Files\Java\jre6 (your version)\bin
4. Open ssl: for 32 bit: here , for 64 bit: here.
    Extract to : C:\openssl










Now..

Type: keytool -exportcert -alias androiddebugkey -keystore C:\Users\Joshua\.android\debug.keystore | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl base64






type: 'android' for the password

That it!!

Monday, April 9, 2012

Android EditText set cursor position to end

Set cursor to end:

        EditText editText = (EditText)findViewById(R.id.edit_text);
        editText.setSelection(editText.getText().length());

Set cursor to front:
        EditText editText = (EditText)findViewById(R.id.edit_text);
        editText.setSelection(0);

Android Dialog with default width

Having trouble making Dialog width to default length? (At least curtain length)

Use MinWidth theme:

final Dialog dialog = new Dialog(this, android.R.style.Theme_Holo_Dialog_MinWidth);
dialog.setContentView(R.layout.main);
dialog.setCancelable(true);
dialog.show();

You can also use: android.R.style.Theme_Holo_Light_Dialog_MinWidth

Android Dialog with transparent background

This is an example of when an Activity is set as Dialog style:

Ex:
AndroidManifest.xml:

        <activity
            android:theme="@android:style/Theme.Holo.Light.Dialog.MinWidth"
            android:name=".DialogActivity" />

Different Dialog styles are:
- android:theme="@android:style/Theme.Dialog"
- android:theme="@android:style/Theme.Holo.Dialog"
- android:theme="@android:style/Theme.Holo.Dialog.MinWidth"
- android:theme="@android:style/Theme.Holo.Dialog.NoActionBar"
- android:theme="@android:style/Theme.Holo.Dialog.NoActionBar.MinWidth"
- android:theme="@android:style/Theme.Holo.DialogWhenLarge"
- android:theme="@android:style/Theme.Holo.DialogWhenLarge.NoActionBar"
- android:theme="@android:style/Theme.Holo.Light.Dialog"
- android:theme="@android:style/Theme.Holo.Light.Dialog.MinWidth"
- android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar"
- android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"
- android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge"
- android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge.NoActionBar"

DialogActivity.java:

       @Override
       protected void onCreate(Bundle savedInstanceState){
             super.onCreate(savedInstanceState);
             requestWindowFeature(Window.FEATURE_NO_TITLE);
             getWindow().setBackgroundDrawable(new ColorDrawable(0));
             setContentView(R.layout.main);
       }

-------------------------------------

OR:

Dialog:


             final Dialog dialog = new Dialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth);
             dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
             dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
             dialog.setContentView(R.layout.main);
             dialog.setCancelable(true);
             dialog.show();

Android AsyncTask example: how to pass different or multiple objects

How to implement AsyncTask:

               public class AsyncTaskExample extends AsyncTask<Object, Void, Boolean> {
                             
                              @Override
                              protected Boolean doInBackground(Object... params) {
                                             String s = (String) params[0];
                                             Long lValue = (Long) params[1];
                                             Integer iValue = (Integer) params[2];
                                             Boolean bValue = (Boolean) params[3];
                                             // do something useful
                                             return new Boolean(true);
                              }
                             
                              @Override
                              protected void onPostExecute(Boolean result) {
                                             // do something useful
                              }
               }

How to use AsyncTask:
             new AsyncTaskExample().execute("Hello World", new Long(12345),
                           new Integer(12345), new Boolean(true));

------------------------------------------------------------

OR

How to implement AsyncTask:

               public class AsyncTaskExample extends AsyncTask<Void, Void, Boolean> {

                              private String s;
                              private Long lValue;
                              private Integer iValue;
                              private Boolean bValue;
                             
                              public AsyncTaskExample(String s, Long lValue, Integer iValue, Boolean bValue)
        {
            this.s = s;
            this.lValue = lValue;
            this.iValue = iValue;
            this.bValue = bValue;
        }
                             
                              @Override
                              protected Boolean doInBackground(Void... params) {
                                             // do something useful
                                             return new Boolean(true);
                              }
                             
                              @Override
                              protected void onPostExecute(Boolean result) {
                                             // do something useful
                              }
               }
How to use AsyncTask:



             new AsyncTaskExample("Hello World", new Long(12345),
                           new Integer(12345), new Boolean(true)).execute();