package no.agens.cassowarylayoutdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import org.klomp.cassowary.clconstraint.ClConstraint;

import no.agens.cassowarylayout.CassowaryLayout;
import no.agens.cassowarylayout.ViewModel;
import no.agens.cassowarylayoutdemo.R;

public class DynamicWidth extends Activity {

    private double delta;

    private CassowaryLayout layout;
    private ClConstraint constraint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_width);

        layout = (CassowaryLayout)findViewById(R.id.layout);

        findViewById(R.id.dragger).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int X = (int) event.getRawX();

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        ViewModel viewModel = layout.getViewModelById(v.getId());
                        delta = X - viewModel.getX().getValue();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (constraint != null) {
                            layout.removeConstraint(constraint);
                        }
                        constraint = layout.addConstraint("dragger.x == " + (X - delta));
                        layout.requestLayout();
                        break;
                }

                return true;
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dynamic_width, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

@@ -234,33 +234,177 @@
 ary.
-clconstraint.ClConstraint
+ClLinearExpression;%0Aimport org.klomp.cassowary.ClStrength;%0Aimport org.klomp.cassowary.clconstraint.ClConstraint;%0Aimport org.klomp.cassowary.clconstraint.ClLinearEquation
 ;%0A%0Ai
@@ -1135,24 +1135,99 @@
 getRawX();%0A%0A
+                ViewModel viewModel = layout.getViewModelById(v.getId());%0A%0A
             
@@ -1339,90 +1339,8 @@
 WN:%0A
-                        ViewModel viewModel = layout.getViewModelById(v.getId());%0A
     
@@ -1869,32 +1869,33 @@
               %7D%0A
+%0A
                 
@@ -1919,58 +1919,157 @@
 t = 
-layout.addConstraint(%22dragger.x == %22 + (X - delta)
+new ClLinearEquation(viewModel.getX(), new ClLinearExpression(X - delta), ClStrength.strong);%0A                        layout.addConstraint(constraint
 );%0A 

Set dynamic width with priority strong to work around overconstraint