Converting a view to AndroidViewComponent

Hello every one..
I'm creating an extension and going to publish it soon..I have only one problem..I have a method that returns a View or A viewGroup..How can i convert this view into an AndroidViewComponent..So it can be returned by the block..As far as i know i can't return a view but i can return a androidviewcomponent so what can i do to achieve that?
Mohamed Tamer

Casting can be an option but I don't know if it will work or not.

1 Like

Sorry,I didn't here about that before :sweat_smile: You mean id cast or what?

I mean casting object type.
Something like this:

FrameLayout fr = (FrameLayout)view;
1 Like

Oh, Thanks now i understood :blush:I will try it now..

1 Like

I don't think typecasting to AndroidViewComponent will work. Since AndroidViewComponent class is not parent of View class. You can instead try casting to Object type and then return.


Thanks @Souvik_Bera i will try this too :+1:

what do you want to do ?

1 Like

Why are you asking?
Any way I'm creating an extension.

so I can help you !

1 Like

I'm working on extension that provides some tools for components.I'm writing its topic already now. :grin: And one of the methods i use in my code was returning a ViewGroup type which couldn't be returned by an app inventor block.So i needed to convert it to a AndroidViewComponent
BTW,@Souvik_Bera and @vknow360 and @Silver Casting to object type worked but it returnes a weird value and not what i expected.Ex. com.appinventor.components.runtime.LinearLayout$1.....{1,54,898,230}
Expected was:
com.appinventor.components.runtime.VerticalArrangemnt@87985 ( something like that )
I removed this block until i found a solution for that..

the other day I made a test extension to handle AndroidViewComponent corner radius, I managed to change button, label , HArrangement, VArrangement etc, corner radius and other settings, I don't know if this can help you or not.

No i'm not trying to input an AndroidViewComponent.I'm trying to return a AndroidViewComponent converted from a ViewGroup.

well, you can ask @Ken here..

1 Like

@Souvik_Bera is correct. You can't cast View/ViewGroup to AndroidViewComponent because one isn't the supertype of the other. You have three options that you can really do:

  1. Change the return type to Object, and return the View/ViewGroup. The user won't be able to manipulate the object with any of the blocks, but you can provide methods on your extension component to manipulate the view.
  2. Return an anonymous subclass of AndroidViewComponent. The user won't be able to manipulate the object with any of the blocks in this case either because the "any component" blocks check the concrete type of the thing they are passed and AndroidViewComponent is abstract.
    final View theView = view;
    return new AndroidViewComponent(form) {
      public View getView() {
        return theView;
  3. Create another class that subclasses AndroidViewComponent and return that (have its constructor take your View/ViewGroup). If you were to call this FooView, for example, then the user can use the "any FooView" component blocks to manipulate the returned instance.

thanks very much @ewpatton :blush: you helped me a lot. I will try all of these methods now :+1:

BTW,I tried this method..And it works.But, a wrong value is returned.

The type that gets shown is exactly what you returned, I'm not sure why this is a surprise. If you want to return a vertical arrangement, then you will need to construct one.


Yes, something like this:
Let's say we have to return an ImageView.But since we can't return it directly so we will create an Image and set same properties as of ImageView.And then return the Image instead of ImageView.

It should work fine.


Thanks @ewpatton :grin:

Now i understood what should i make.I will go and try now.
Thanks @vknow360 :blush:

I think because i'm still learning extension development. Especially working with views. :grin:

This is a constraint in Java generally and has nothing to do with extension development. Java as a language only supports single inheritance in the class hierarchy, so if you are in a situation where you have items a and b of classes A and B respectively and neither A ⊆ B nor B ⊆ A then there's nothing you can do to cast one to the other.