How to set color to an layout in extensions

@SimpleFunction(description = "")
public void bgset(AndroidViewComponent component,int color) {
 View view = component.getView();
 view.setBackgroundColor (color);
}

i am trying to set colour of horrizontal arangement

but i get error
gnu.mapping.SimpleSymbol cannot be cast to java.lang.integer
am i doing anything wrong


i got reference from source code of canvas

Can you show your imports?

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.common.ComponentCategory;
import com.google.appinventor.components.runtime.util.YailList;
import android.view.View;
import android.graphics.Color;

amazing it work now sorry it is my net issue

1 Like

wait there is another issue
the code

    @SimpleFunction(description = "")
    public void bgset(AndroidViewComponent component,YailList colors) {
     View view = component.getView();
     int index1 = 0;
     for(Object x : colors ) { 
     view.setBackgroundColor((int)x);
     index1++; 
     }
    }

blocks

result
error-gnu.mapping.SimpleSymbol cannot be cast to java.lang.Integer
my last color in list is green so it should be green
i know that the object is related to error

Use this:-

    @SimpleFunction(description = "")
    public void bgset(AndroidViewComponent component, List<Integer> colors){
    String listString = colors.toString();
    View view = component.getView();
    final String[] array = listString.split(" ");
            final int[] ints = new int[array.length];
            for (int i = 0; i < array.length; ++i) {
                try {
                    ints[i] = Integer.parseInt(array[i]);
                }
                catch (NumberFormatException ex) {}
            }
view.setBackgroundColor(ints);
    }

What are you trying to do??

i will try ,

/appinventor/components/src/in/hifi/Bgcolour/Bgcolour.java:49: error: incompatible types: int[] cannot be converted to int
[javac] view.setBackgroundColor(ints);

setBackgroundColor accepts int but you are giving int array here.

1 Like

please follow the

Taifun

1 Like

This is just a learn,test extension I will use it in my main extension

You can achieve:

By adding this import:
import gnu.math.IntNum;

And using this code:
@SimpleFunction(description = "")
public void bgset(AndroidViewComponent component,YailList colors) {
View view = component.getView();
Object[] array = colors.toArray();
for(Object x : array) {
IntNum colorX = (IntNum)x;
view.setBackgroundColor(colorX.intValue())
}
}

5 Likes

Worked!!!.

1 Like

@Ken does that method adds color to the old background color?
Because if it accepts only one value then background color should be the last value of list.

1 Like

Ya that's what I need because i have used this as a doubt part of my another extension this I not major extension

As you suspected, it doesn't add to the background color, it replaces the color.
The end result of this particular method isn't important, it was only to show how you could iterate through a list of colors.
:arrow_down:

3 Likes

quick reminder concerning the naming conventions... :sunglasses:

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.