Amazon Gift Cards

Wednesday, February 18, 2009

Detecting if the Sd-Card had been mounted in Android

Here is short article on how to detect if the sd-card had been mounted on android.

String storagestate = Environment.getExternalStorageState();
if (!storagestate.equals(Environment.MEDIA_MOUNTED) ) {
  AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  alertDialog.setTitle(this.getText(R.string.error_sd_card_title));
  alertDialog.setMessage(this.getText(R.string.error_sd_card_desc));
  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      finish();
      return;
  } });
  alertDialog.show();
}

Quick Explanation
String storagestate = Environment.getExternalStorageState();
--- This line gets the storage state, external means your sd-card, change it to internal then it means get the state of your phone memory.

if (!storagestate.equals(Environment.MEDIA_MOUNTED) ) { }
--- This check if the storage's state is mounted, then ! at the front would reverse the result. For more values to compare to, see Environment class


Update History
   Jan 17, 2012 - Visual Update

Saturday, February 14, 2009

MaxLength in EditText using Codes

In android there is EditText and sometimes you may want to limit the character input. EditText in XML layout would give you android:maxLength to do this thing but in codes you might wonder why there isn't any setMaxLength function. The reason behind this is that when you want to restrict the EditText to accept certain value, you have to filter them and this would be invoke by setFilters and thus to make our EditText to have a fixed size we shall.
EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);


Reference:
androidblogger

Update History
   Jan 17, 2012 - Visual Update

Tuesday, February 3, 2009

WebView with ZoomControls in Android

Here is an article on how to use WebView with ZoomControls enabled. First of all add an Internet permission to your manifest file.
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

Layout (webview.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" 
>
  <WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  />
</LinearLayout>


public class Main extends Activity {
  private WebView myWebView;
  private static final FrameLayout.LayoutParams ZOOM_PARAMS =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.webview);
    this.myWebView = (WebView) this.findViewById(R.id.webView);

    FrameLayout mContentView = (FrameLayout) getWindow().
    getDecorView().findViewById(android.R.id.content);
    final View zoom = this.myWebView.getZoomControls();
    mContentView.addView(zoom, ZOOM_PARAMS);
    zoom.setVisibility(View.GONE);

    this.myWebView.loadUrl("http://www.almondmendoza.com");
  }
}



Quick Explanation:
The stuff here were extracted from BrowserActivity, so if there is an error on my explanation, i apologize in advance.

FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
-- According to Windows Decoration, this would retrieve the top-level window decor view.

final View zoom = this.myWebView.getZoomControls();
-- this would get the zoom controls of the webview object

mContentView.addView(zoom, ZOOM_PARAMS);
-- this would add the zoom controls to our our top-level window, the ZOOM_PARAMS was just copied from BrowserActivity, so i dont actually know what it means :)

zoom.setVisibility(View.GONE);
-- This would hide the zoom at the start, we do this because we want to show the zoom controls only when the user's scroll the browser.

this.myWebView.loadUrl("http://www.almondmendoza.com");
-- This would load the url to our webview.


Source:
The codes above are all the sources, you just have to import the right classes. In eclipse just highlight the errors and right click then Quick Fix.

Reference:
BrowserActivity