Android Open Source - editable-layout Editable Layout Activity






From Project

Back to project page editable-layout.

License

The source code is released under:

GNU General Public License

If you think the Android project editable-layout listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package nl.robinkanters.editablelayout.app;
/*from  w  w  w  .java2s .  com*/
import static android.content.ClipData.newPlainText;
import static android.view.DragEvent.ACTION_DRAG_LOCATION;
import static android.view.DragEvent.ACTION_DROP;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import nl.robinkanters.editablelayout.R;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout.LayoutParams;

public class EditableLayoutActivity extends Activity {
  private FrameLayout mView;

  ClipboardManager mClipboard;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

    setContentView(makeLayout());
  }

  private View makeLayout() {
    mView = new FrameLayout(this);
    LayoutParams params = new FrameLayout.LayoutParams(MATCH_PARENT,
        MATCH_PARENT);

    mView.setLayoutParams(params);
    mView.setOnDragListener(new DragListener());

    return mView;
  }

  protected void addView(View view) {
    mView.addView(view);
    addListeners(view);
  }

  private void addListeners(View view) {
    view.setOnTouchListener(new TouchDragListener());
  }

  private final class TouchDragListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == ACTION_DOWN) {
        ClipData data = newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
            view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(INVISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

  private final class DragListener implements OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
      int action = event.getAction();

      if(action == ACTION_DRAG_LOCATION || action == ACTION_DROP) {
        View view = (View) event.getLocalState();

        float   x = event.getX(),
            y = event.getY();

        if(action == ACTION_DROP) {
          view.setVisibility(VISIBLE);

          x -= view.getWidth()/2;
          y -= view.getHeight()/2;
        }

        view.setX(x);
        view.setY(y);
      }
      return true;
    }
  }

  protected void addView(String viewType) {
    View v = createViewFromViewType(viewType);
    v.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        
        v.setX(100f);
        v.setY(150f);
        
        try {
          ((android.widget.TextView) v).setText(getRandomName());
        } catch (ClassCastException e) {
          // do nothing, the view simply cannot accept text
        }
        
    addView(v);
  }

  private View createViewFromViewType(String viewType) {
    View object = null;

    try {
      Class<?> classObj = Class.forName(viewType);
      Constructor<?> constr = classObj.getConstructor(Context.class);
      object = (View) constr.newInstance(this);
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (ClassCastException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }

    return object;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    Spinner s = (Spinner) menu.findItem(R.id.action_add).getActionView().findViewById(R.id.menu_spinner);
    s.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Ignore the first list item
        if(position < 1) return;

        String[] strings = getResources().getStringArray(R.array.classnames);
        makeText(getApplicationContext(), strings[position], LENGTH_SHORT).show();

        addView(strings[position]);

        parent.setSelection(0);
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
        // do nothing
      }
    });

    return true;
  }
  
  private String getRandomName() {
    String[] names = getResources().getStringArray(R.array.names);
    int length = names.length;
    int randomIndex = (int) (Math.random() * (double)length);
    
    return names[randomIndex];
  }
}




Java Source Code List

nl.robinkanters.editablelayout.app.EditableLayoutActivity.java
nl.robinkanters.editablelayouttest.MainActivity.java