Fork me on GitHub

mig4android by saynomoo

The Java Layout Manager for Swing, SWT and JavaFX - Now available for android also!

Here's an example of what identical layout code in android and in swing produces (taken from the miglayout demo)

Dependencies

Miglayout version 4.2

Installation

Two jars are needed:

The required minSdkVersion is at least 4. Without this the dimensions are calculated incorrectly. So be sure your Androidmanifest.xml contains:

<uses-sdk android:minSdkVersion="4" />

License

BSD

Usage

Progammatically

The base component is MigLayout, which is a descendant of ViewGroup. There is three methods which can be used to configure the layout: setLayoutConstraints, setColumnConstraints and setRowConstraints. These methods take the corresponding constraints as defined in MiG Layout Cheat Sheet. Other Views can be added to the layout with method addView, optionally providing the component constraints, also defined in the Cheat Sheet.

Example:

    class ExampleLayout extends MigLayout {
        public ExampleLayout(Context context) {
            super(context);
            setLayoutConstraints("wrap 4, debug 1");
            setColumnConstraints("[]15[75px]25[min]25[]");
            setRowConstraints("[]15");

            addView(label("label1"), "skip");
            addView(label("label2"));
            addView(label("label3"), "wrap");
            addView(label("label4"));
            addView(label("label5"), "wmin 30");
            addView(label("label6"), "wmin 30");
            addView(label("label7"), "wmin 30");
        }
        private TextView label(final String label){
            return new TextView(getContext()) {{
                setText(label);
            }};
        }
    }
  

From xml

All the methods used in the programmatic example are usable also from xml layout. The previous layout would translate to:
<com.saynomoo.mig4android.MigLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              layout_constraints="wrap 4, debug 1"
              column_constraints="[]15[75px]25[min]25[]"
              row_constraints="[]15"
        >
    <TextView android:text="label1" component_constraints="skip"/>
    <TextView android:text="label2" />
    <TextView android:text="label3" component_constraints="wrap"/>
    <TextView android:text="label4" />
    <TextView android:text="label5" component_constraints="wmin 30"/>
    <TextView android:text="label6" component_constraints="wmin 30"/>
    <TextView android:text="label7" component_constraints="wmin 30"/>

</com.saynomoo.mig4android.MigLayout>
  

More examples

More examples can be found from the examples module.

Developing

The source distribution comes with idea project files, which should work as long as the android sdk is installed. There is also a sbt build configuration (v 0.7.7) in place. For example using sbt to build the library:
    $ ./sbt package
  
When called from project root, it produces the library jar at path:
    ./mig4android/target/mig4android-1.0.jar
  

Contributing

Contributions are more than welcome. Please fork the project and make a pull request.

Authors

Tuomas Huhtanen

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/saynomoo/mig4android