Friday, January 11, 2013

Embedded Jetty from eclipse and jar

== Starting Jetty ==

public void start() throws Exception{
        Server server = new Server(port);
       
        WebAppContext context = new WebAppContext();
        context.setOverrideDescriptor("src/main/webapp/WEB-INF/web.xml");
       
        URL weburl = this.getClass().getResource("/src/main/webapp");
        if (weburl == null){
            //running from eclipse
            context.setResourceBase("src/main/webapp");
        }else{
            //running from jar
            context.setResourceBase(weburl.toExternalForm());
        }
       
        context.setContextPath("/");
        context.setParentLoaderPriority(true);

        server.setHandler(context);
       
        server.start();
        server.join();
    }

Wednesday, January 18, 2012

Alias for reference project in .Net

Sometimes I need to have a project in C#, that refers to two different assemblies that, however, contain one or more same classes.
This should not happen final products, but it can happen when testing or debugging.

Here is how to resolve the issue:

1. In reference properties add alias, e.g. aliases: global, custom
2. In the file that uses the classes that appear in two references:

extern alias custom;

using custom::MyPackage;

Thursday, November 17, 2011

Debug applet remotely from eclipse

1. Add the following jvm parameters for applet:
-Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:9000,suspend=y

2. In eclipse create a debug configuration.
Remote Java Application, Standard (Socket Listen), port 9000

3. The applet jar file should not be obfuscated.

4. First start listening, then start applet. The browser probably has to be restarted to reload applet.

(more detailed here: http://webmoli.com/2009/01/17/debugging-applet-code-using-eclipse/)

Tuesday, August 23, 2011

Android Dev Guide pdf and doc

Recently I have been searching for a printable version of Android Dev Guide. Unfortunately I found only a 1 year old variant over here: http://czechxan.com/index.php/android/20-android-sdk-dev-guide-in-pdf

So I have decided to spend some time and create a new variant.
So far I managed to add Android Basics and Framework Topics from Activities (included) to the User Interface section (not included).

Later I am planning to add other sections too. For now I am providing just the first part in two variants pdf and doc (in case you want to format something). I shared a folder on the Google docs, so you are welcome to download:

First Part Android Dev Guide

And I will get back to reading...

Saturday, June 11, 2011

Android TableLayout and expanding TextView

Many forms I am used to seeing have the following layout

Label     Input
Label     Input
...

Label     Input
 Save Button

To implement this in android I used a TableLayout inside a ScrollView. Inside the TableLayout I have TextViews and EditTexts. I wanted the EditTexts to take most of the width, so I put android:stretchColumns="1" inside the TableLayout tag. Everything looked fine until I entered a long text. I just couldn't see it. The text is not wrapped and no multi lines appear. It appears that the same column I marked for stretching should be marked for shrinking. So adding  android:shrinkColumns="1" solved my problem. And then I found the explanation in android docs (of course!): 
 If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable. In such a situation, the column will change its size to always use up the available space, but never more. 
Here is the layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="1" 
             android:shrinkColumns="1"
            android:padding="3dip">
        <TableRow>
            <TextView android:text="@string/first_name"
                      android:padding="3dip"/>
            <EditText android:id="@+id/first_name" />
        <TableRow>

        <TableRow>
            <TextView android:text="@string/last_name"
                      android:padding="3dip"/>
            <EditText android:id="@+id/last_name" />
        </TableRow>

        <View android:layout_height="2dip"
              android:background="#FF909090"
              android:layout_marginBottom="3dip"/>

        <Button android:id="@+id/save"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/save_btn"
                android:onClick="save"/>

    </TableLayout>
</ScrollView>


Monday, June 6, 2011

Measurements in Android


We are used to designing interfaces in terms of pixels. But when developing for mobile devices we eventually have to deal with different displays. Resolution-independent measurements help to solve the problem of fitting content into different devices.
Android supports the following units:

  • px (pixels): Dots on the screen.
  • in (inches): Size as measured by a ruler.
  • mm (millimeters): Size as measured by a ruler.
  • pt (points): 1/72 of an inch.
  • dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
  • dip: Synonym for dp, used more often in Google examples.
  • sp (scale-independent pixels): Similar to dp but also scaled by the user’s font size preference.

For making your interface scalable to any current and future type of display, it is recommended you always use the sp unit for text sizes and the dip unit for everything else. Hello Android, 3rd edition

Friday, June 3, 2011

Keeping screen awake when plugged

Here is how to keep the screen awake for certain activity, but only when the phone is plugged.
The provided code should be place inside Activity.

 private BroadcastReceiver powerConnectReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) > 0;
findViewById(R.id.map).setKeepScreenOn(plugged);
}
};

….

@Override
protected void onResume() {
registerReceiver(powerConnectReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}

@Override
protected void onPause() {
unregisterReceiver(powerConnectReceiver);
super.onPause();
}