/** ~~~~~
* Created with the AppyBuilder Code Editor.
* This is a template for basic Extension.
* Modify this template to customize your extension.
*
* **** NOTE: DO NOT use a package name.
* **** The package name will be created for you automatically.
* **** Adding a package name will cause a compile error
*/
import com.google.appinventor.components.runtime.Label;
import android.view.View;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.ComponentConstants;
//Added imports
import android.view.ViewGroup;// https://developer.android.com/reference/android/view/ViewGroup
import android.widget.TextView;// https://developer.android.com/reference/android/widget/TextView
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
@DesignerComponent(version = 1, description = "This Extension was created with the AppyBuilder Code Editor.<br>" +
"Create your own here:<br><a href='https://editor.appybuilder.com' target='_blank'>https://editor.appybuilder.com</a><br>",
category = ComponentCategory.EXTENSION,
nonVisible = true, iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class EditingComponents extends AndroidNonvisibleComponent{
private ComponentContainer container;
private Context context; //Added
private Activity activity; //Added
/**
* @param container container, component will be placed in
*/
public EditingComponents(ComponentContainer container) {
super(container.$form());
this.container = container;
context = (Context) container.$context(); //Added
activity = (Activity) context; //Added
}
@SimpleFunction(description="Edit A Component")
public void EditComponent(AndroidViewComponent comp, int stroke, int strokeColor, int bgcolor, float cornerRadius){
View myView = (View)comp.getView();
final GradientDrawable shape = new GradientDrawable();
// when you chose a shape other than RECTANGLE, setCornerRadius(); function wont work, see this link:
// https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setCornerRadius(float)
shape.setShape(GradientDrawable.RECTANGLE);
// the setColor is an argb int value, example: red color value = 0xffff0000
shape.setColor(bgcolor);
shape.setStroke(stroke, strokeColor);
//shape.setStroke(stroke, Color.Red);
shape.setCornerRadius(cornerRadius);
// shape.setCornerRadii(new float[] { 200, 200, 100, 100, 50, 50, 25, 25});
// here we are setting the shape drawable as a background for our component.
myView.setBackgroundDrawable(shape);
//see this link: https://developer.android.com/reference/android/view/View#setElevation(float)
// myView.setElevation(elevation);
/*
// this method here is to a click event and it will ovveride Kodual Click event for the component you are using now, When you use this Click event you can't use Kodular Click event for this Component and be aware that adding a GradientDrawable to this Component you are using disables any click or touch events that this component have in Kodular.
myView.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View v) {
shape.setColor(0x80ff5032);
//myView.invalidate();
}
});
*/
}
// Label Component extends AndroidViewComponent
@SimpleFunction(description="Edit A Label")
public void EditLabel(Label myLabel, String setText, int setBGColor){
myLabel.Text(setText);
// the setColor is an argb int value, example: red color value = 0xffff0000
myLabel.BackgroundColor(setBGColor);
// see BackgroundColor(); and Text(); functions here
// https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/Label.java
// any function in this file you can use it to manipulate the Label component.
// you can manipulate the Button component also and other component that extends AndroidViewComponent.
// be aware to import whatever component you want to manipulate as above in line number 10.
}
//this function is to add a TextView inside Horizontal or Vertical Arrangement.
// see appinventor-sources for HVArrangement.
@SimpleFunction(description="Add A TextView")
public void AddTextView(HVArrangement layout, String text, int color, int bgColor, int maxLines, int width, int height){
// from Kodular set the background color for Horizontal or Vertical Arrangement you use here to None.
// this is an ID assigned to the TextView inside the Linear Layout for ordering components.
int id = 0;
// myViewGroup now will act as our Horizontal or Vertical Arrangement that we assigned to the HVArrangement.
ViewGroup myViewGroup = (ViewGroup)layout.getView();
// here we initialized a Linear Layout inside myViewGroup to add the TextView to it.
LinearLayout myLinearLayout = (LinearLayout)myViewGroup.getChildAt(0);
// most Android widgets are constructed this way where you can say activity is our Screen1 for example.
TextView myTextView = new TextView(activity);
myTextView.setId(id);
//see this link for a list of functions you can use:
//https://developer.android.com/reference/android/widget/TextView
myTextView.setText(text);
// this will set our TextView to render all the text that it will show as a single line text.
myTextView.setMaxLines(maxLines);
// the setTextColor is an argb int value, example: red color value = 0xffff0000
myTextView.setBackgroundColor(bgColor);
// the setTextColor is an argb int value, example: red color value = 0xffff0000
myTextView.setTextColor(color);
//see this link: https://developer.android.com/reference/android/graphics/Color
// myTextView.setTextColor(Color.RED);
// here we initialized a set of rules for our TextView to follow like it's width and height.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width , height);
// here we assigned the above values to our TextView.
myTextView.setLayoutParams(lp);
// here we added or TextView to our Linear Layout following the rules we set earliar.
myLinearLayout.addView(myTextView, id);
// a refresh method to re-draw our Linear Layout so that our TextView shows on the screen.
myLinearLayout.invalidate();
}
}
You can only return a certain set of types from the Java side to the blocks. You can't return ComponentContainer directly, but if you make the object implement both Component and ComponentContainer it should work.