Permalink
Browse files

Adding custom Layout / View support and Button widget

  • Loading branch information...
ashwanthkumar committed Jan 4, 2014
1 parent 06220b2 commit 0d512814e23221f3041bf5464e0bc29001125097
@@ -0,0 +1,102 @@
package ketai.ui.button;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import ketai.ui.layout.KetaiLayout;
/*
Usage:
KetaiLayout layout = new KetaiLayout(this, "GridLayout");
KetaiButton button = new KetaiButton(layout, "Sample Button #1"); // Does nothing
class CloseButtonImpl implements KetaiButtonCallback {
void onClick(Button button) {
exit();
}
}
KetaiButton button = new KetaiButton(layout, "Close", new CloseButtonImpl()); // Exits the application
*/
public class KetaiButton {
private KetaiLayout parentView;
private Button button;
public KetaiButton(KetaiLayout _parentLayout, String buttonText) {
parentView = _parentLayout;
button = new Button(parentView.getParent().getApplicationContext());
initButton(buttonText, new KetaiButtonCallBackImpl());
}
public KetaiButton(KetaiLayout _parentLayout, final String buttonText, KetaiButtonCallback buttonCallback) {
parentView = _parentLayout;
button = new Button(_parentLayout.getParent().getApplicationContext());
initButton(buttonText, buttonCallback);
}
private void initButton(final String text, final KetaiButtonCallback callbackImpl) {
final KetaiButton myself = this;
parentView.getParent().runOnUiThread(new Runnable() {
public void run() {
button.setText(text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callbackImpl.onClick(myself);
}
});
parentView.getLayout().addView(button);
}
});
}
public void setTitle(final String text) {
parentView.getParent().runOnUiThread(new Runnable() {
@Override
public void run() {
button.setText(text);
}
});
}
public String getTitle() {
return button.getText().toString();
}
public void setWidth(final int widthInPixels) {
parentView.getParent().runOnUiThread(new Runnable() {
public void run() {
button.setWidth(widthInPixels);
}
});
}
public void setHeight(final int heightInPixels) {
parentView.getParent().runOnUiThread(new Runnable() {
public void run() {
button.setHeight(heightInPixels);
}
});
}
public void wrapContentOnHeight() {
parentView.getParent().runOnUiThread(new Runnable() {
public void run() {
ViewGroup.LayoutParams layoutParams = button.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
button.setLayoutParams(layoutParams);
}
});
}
public void wrapContentOnWidth() {
parentView.getParent().runOnUiThread(new Runnable() {
public void run() {
ViewGroup.LayoutParams layoutParams = button.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
button.setLayoutParams(layoutParams);
}
});
}
}
@@ -0,0 +1,16 @@
package ketai.ui.button;
import android.widget.Button;
import processing.core.PApplet;
public interface KetaiButtonCallback {
public void onClick(KetaiButton button);
}
class KetaiButtonCallBackImpl implements KetaiButtonCallback {
@Override
public void onClick(KetaiButton button) {
PApplet.println("Default Button Callback");
}
}
@@ -0,0 +1,84 @@
package ketai.ui.layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import processing.core.PApplet;
public class KetaiLayout {
private PApplet parent;
protected ViewGroup baseLayout;
/*
If `this` is not the root of the hierarchy, we add the layout to this ViewGroup.
*/
private ViewGroup parentLayout;
/*
Flag denoting if this layout instance is in the root of our layout hierarchy.
*/
private boolean isRootElement = false;
public KetaiLayout(PApplet _parent, String _viewType) {
parent = _parent;
isRootElement = true;
addToUI(_viewType);
}
public KetaiLayout(KetaiLayout _parentLayout, String _viewType) {
parentLayout = _parentLayout.getLayout();
parent = _parentLayout.getParent();
addToUI(_viewType);
}
public PApplet getParent() {
return parent;
}
public ViewGroup getLayout() {
return baseLayout;
}
public void addView(final View childView) {
parent.runOnUiThread(new Runnable() {
@Override
public void run() {
baseLayout.addView(childView);
}
});
}
public void changeLinearLayoutToVertical() {
if (baseLayout instanceof LinearLayout) {
((LinearLayout) baseLayout).setOrientation(LinearLayout.VERTICAL);
} else {
throw new IllegalStateException("Layout is not LinearLayout");
}
}
public void changeLinearLayoutToHorizontal() {
if (baseLayout instanceof LinearLayout) {
((LinearLayout) baseLayout).setOrientation(LinearLayout.HORIZONTAL);
} else {
throw new IllegalStateException("Layout is not LinearLayout");
}
}
private void addToUI(String _viewType) {
baseLayout = LayoutHelper.createViewGroup(_viewType, parent.getApplicationContext());
final ViewGroup.LayoutParams layoutParams = LayoutHelper.createLayoutParamsFor(_viewType);
this.parent.runOnUiThread(new Runnable() {
@Override
public void run() {
if (isRootElement) {
parent.addContentView(baseLayout, layoutParams);
} else {
parentLayout.addView(baseLayout, layoutParams);
}
}
});
}
}
@@ -0,0 +1,40 @@
package ketai.ui.layout;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.*;
public class LayoutHelper {
public static ViewGroup.LayoutParams createLayoutParamsFor(String layoutType) {
if (layoutType.equals("RelativeLayout")) {
return new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else if (layoutType.equals("LinearLayout")) {
return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else if (layoutType.equals("GridLayout")) {
return new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(0));
} else if (layoutType.equals("ScrollView")) {
return new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else if (layoutType.equals("HorizontalScrollView")) {
return new HorizontalScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else {
throw new IllegalArgumentException("Unknown Layout Type");
}
}
public static ViewGroup createViewGroup(String layoutType, Context parentContext) {
if (layoutType.equals("RelativeLayout")) {
return new RelativeLayout(parentContext);
} else if (layoutType.equals("LinearLayout")) {
return new LinearLayout(parentContext);
} else if (layoutType.equals("GridLayout")) {
return new GridLayout(parentContext);
} else if (layoutType.equals("ScrollView")) {
return new ScrollView(parentContext);
} else if (layoutType.equals("HorizontalScrollView")) {
return new HorizontalScrollView(parentContext);
} else {
throw new IllegalArgumentException("Unknown Layout Type");
}
}
}

0 comments on commit 0d51281

Please sign in to comment.