Pages

Showing posts with label tips n tricks. Show all posts
Showing posts with label tips n tricks. Show all posts

Monday, September 26, 2011

Using git + dropbox + eclipse on windows

Some time ago I suffered a hardware crash, and even more recently I thought I lost my computer. Two incidents that both kept me up all night thinking of what content I might have lost and what impact they may have.

Realizing I have some android-projects (like TravAlertLite and Premium) using git for version control, but only using local repo i.e. no back-up what so ever. I promise myself to make some backups the very next day or even better - use a remote repository to push to. But I don't have  a server.. Hey - why not use dropbox?
(signup using my referal invite and we both get 250MB bonus space)

So - let's get to the point..

  • you are using windows
  • you are using eclipse with egit
  • you have local repo of existing project
  • you have dropbox account (default install ~/Dropbox)


  1. Open dropbox folder and create suitable directory, say 'git/private_projects/'
  2. Open the bash in your local repo and
    $ git clone --bare . ~/Dropbox/git/private_projects/my_project.git
  3. Add this new clone as remote called 'dropbox'
    $ git remote add dropbox ~/Dropbox/git/private_projecs/my_project.git
  4. Open eclipse and you are now able to push from menu team>remote

Disclaimer
I have not fully tested this setup but it seem to work just fine for simple personal use, this is not a preferred professional setup.
I think it would work just fine among smaller teams where simultaneous pushes are less likely and also easily avoided.

I do not guarantee that this works nor do I take responsibility for consequences. I'm just sharing my thoughts and experimental experiences.

Tuesday, April 12, 2011

Android Launcher Icons Color Palette for Incscape

Since we are 'forced' to submit a 512x512 launcher icon when publishing apps I guess I'm not the only one thinking that a master vector icon would be handy.

I've made  a color palette for Inkscape with the recommended colors for launcher icons. Simply copy/paste this and save as Android.gpl and move it to your palette folder in Inkscape and it will be loaded as a selectable palette next time you start.

GIMP Palette
Name: Android Launcher Icons
Columns: 3
#
255 255 255     White (#FFFFFF)
  0   0   0    Black (#000000)
 64  64  64   Dark Gray (#404040)
128 128 128    Medium Gray (#808080)
191 191 191     Light Gray (#BFBFBF)
102 153 255     Light Blue (#6699FF)
 51 102 204    Medium Blue (#3366CC)
  0  51 153     Dark Blue (#003399)
153 204  51     Light Green (#99CC33)
  0 204   0     Medium Green (#00CC00)
102 153   0     Dark Green (#669900)
255 204   0     Light Orange (#FFCC00)
255 153   0     Medium Orange (#FF9900)
255 102   0     Dark Orange (#FF6600)
204   0   0     Red (#CC0000)



New vector based icon for TravAlert

Sunday, March 20, 2011

Including QR-reader in your app

Thanks to android intents, it is easy to include functionality from other apps to you own. Say for example you have an app that should be able to read QR-codes. You could easily call a barcode scanner to do the dirty work for you and just bother about the result.

Create a button that will launch Barcode Scanner from Zxing:
public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.setPackage("com.google.zxing.client.android");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

Handle the callback for result:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

This is a raw copy/paste from the ZXING-project. Nothing that I can take credit for, but I have tested it and it works as a charm.

Yes, it requires the user to have the Barcode Scanner installed. This can be handled smoothly by calling url instead - if the barcoder is installed - it will launch - otherwise launch a web browser (with a install guide of course). Very nice!

Wednesday, March 16, 2011

Extract HTML from Android webview

Having a webview in android and wanting to fiddle with the HTML?

Injecting javascript in android webview

Its easy, make sure you have javascript enabled and simply load url containing javascript:
mBrowser = new WebView(context);
mBrowser.getSettings().setJavaScriptEnabled(true);
mBrowser.loadUrl("javascript:getElementById('ad_container').style.display='none');");

In most cases, you first want to load the actual web page, then inject the javascript - this is done by using a WebViewClient which overrides onPageFinished.

mBrowser.setWebViewClient(new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
      // check url
      if (SOME_WEBPAGE.equals(url) {
        loadUrl(JAVASCRIPT_CODE);
      }
   }
});
mBrowser.loadUrl(SOME_WEBPAGE);

Extracting HTML from Android webview

Taking it one step further - let's get HTML from a page and use it in our program. This is accomplished by using a JavascriptInterface with our webview.

mBrowser.getSettings().setJavaScriptEnabled();
// add javascript interface with example method
mBrowser.addJavaScriptInterface(new JavaScriptInterface() {
   public void setTitle(String html) {
      mTitle = html;
   }
}, "MY_JS");
// add webview client that calls javascript interface when page is loaded
mBrowser.addWebViewClient(new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
       view.loadUrl("javascript:window.MY_JS.setTitle(document.getElementsByTagName('title')[0].innerHTML);");
   }

mBrowser.loadUrl(SOME_WEBPAGE);

That's it - using the combination of WebVievClient and JavascriptInterface is very powerful - drawback is that I have not found an easy way of debugging javascript - if your javascript fails it will fail silent.

Nice thing is you can test your javascript on Firefox using Firebug on your main computer before adding code to your android project.

Wednesday, March 9, 2011

QR-code to launch your app

Android intents are powerful, you can even set you application to launch on a certain URL.

Launch app on defined URL

To make your app launch on a specific URL you simply create an intent filter.

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <data android:scheme="http" android:host="xebralabs.blogspot.com" android:path="/travalert"/>
</intent-filter>

This way you can have your application launched from a QR-code containing the specific URL. Nice thing is that if the app isn't installed the user will most likely have the url opened in a browser instead.

Monday, March 7, 2011

Static buttons in listview

A common gui-layout strategy is having static buttons above or below a listview.

Consider the following layout:


This is accomplished by using a relative layout which includes the listview inside LinearLayout with bottom padding, then the buttons relative to parent.

Example layout xml of static buttons below listview. Key parameters are highlighted.
<RelativeLayout 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_alignParentTop="true"
     android:paddingBottom="50sp">

      <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

      <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

   </LinearLayout>

   <Button
     android:layout_height="wrap_content"
     android:layout_width="124sp"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"/>

   <Button
     android:layout_width="124sp"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"/>

   <Button
     android:layout_width="124sp"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"/>

</RelativeLayout>

Sunday, March 6, 2011

Proguard - keeping onclick methods from layout xml

The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names.

When running Proguard on your jar-file it is important to configure it to keep click listeners that you use from your layout xml.
# keep methods accessed from xml/android:onClick
-keepclassmembers class *{
public void *(android.view.View);
~
More on Proguard.
More on click listeners from layout xml.
~

Friday, January 28, 2011

Android Preference Icon

This might be old news, but I recently realized that the default menu icon for settings in android has was changed for android API 5 (Version 2.0). Gone is the ol' edgy and aggressive hammer 'n wrench and in is the new sleeker über-rounded tuning knob.

Eclair style
Old style





Which one do you prefer?

Did you know that you can find all the android default icons inside your android/platform/platform-{api-lvl}/res/drawables?