Reworked (using RUSH) with a bit of extra code to make a temp file of the ttf, and now works in companion and when compiled:
JAVA
package uk.co.metricrat.buttonmaticon;
import android.graphics.Canvas;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import android.widget.Button;
import android.graphics.Paint;
import java.io.*;
import java.io.File;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
public class ButtonMatIcon extends AndroidNonvisibleComponent {
String filePath;
String newFilePath;
public ButtonMatIcon(ComponentContainer container) {
super(container.$form());
}
public String GetFont() throws IOException {
File file = File.createTempFile("temp", ".ttf");
filePath = file.getAbsolutePath();
try {
InputStream in = form.openAssetForExtension(this, "material-icons.ttf");
int size = in.available();
byte[] buffer = new byte[size];
in.read(buffer);
in.close();
FileOutputStream out = new FileOutputStream(filePath);
out.write(buffer);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
public Drawable createMaterialIcon(String icon, int color, int size) throws IOException {
if (icon == "") {
return null;
} else {
newFilePath = GetFont();
Typeface typeface = Typeface.createFromFile(newFilePath);
Paint paint = new Paint();
paint.setTypeface(typeface);
paint.setColor(color);
paint.setTextSize((float)size);
paint.setTextAlign(Paint.Align.CENTER);
Bitmap bit = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bit);
canvas.drawText(icon, (float) (canvas.getWidth() / 2), (((float) (canvas.getHeight() / 2)) - ((paint.descent() + paint.ascent()) / 2)), paint);
return new BitmapDrawable(form.getResources(), bit);
}
}
@SimpleFunction(description = "Sets the material icon of the specified button. Use an empty text block if you do not want an icon to appear. The size of the icons should be in pixels.")
public void SetMaterialButtonIcons(AndroidViewComponent component, String leftIcon, String topIcon, String rightIcon, String bottomIcon, int color, int size) throws IOException {
Button button = (Button) component.getView();
int pixels = size * getDensity();
button.setCompoundDrawablesWithIntrinsicBounds(createMaterialIcon(leftIcon, color, pixels), createMaterialIcon(topIcon, color, pixels),
createMaterialIcon(rightIcon, color, pixels), createMaterialIcon(bottomIcon, color, pixels));
}
@SimpleFunction(description = "Sets the size of the padding between the icon and the text.")
public void SetIconPadding(AndroidViewComponent component, int padding) {
Button button = (Button) component.getView();
button.setCompoundDrawablePadding(padding);
}
public int getDensity(){
DisplayMetrics metrics = form.getResources().getDisplayMetrics();
return (int) metrics.density;
}
}
uk.co.metricrat.buttonmaticon.aix (192.5 KB)

