How do you set height of any component in building extension?

How to set height of any component in building extension?

package com.faraz.ListWithImageAndText;
import com.google.appinventor.components.annotations.SimpleFunction;

import com.google.appinventor.components.annotations.SimpleProperty;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.errors.YailRuntimeError;

import com.google.appinventor.components.runtime.util.YailList;

import com.google.appinventor.components.runtime.HVArrangement;

import android.widget.ListView;

import android.view.View;

import android.widget.FrameLayout;

public class ListWithImageAndText extends AndroidNonvisibleComponent {

public ComponentContainer container;
public View v;

public ListWithImageAndText(ComponentContainer container) {

super(container.$form());

this.container = container;

}

@SimpleFunction(description = "Creates list with image and text in the arrangement that you give")

public void Create(HVArrangement createIn){

View view = createIn.getView();

FrameLayout frameLayout = (FrameLayout) view;

ListView listView = new ListView(this.container.$context());

v = (listView);

frameLayout.addView(listView, new FrameLayout.LayoutParams(-1, -1));

}

@SimpleProperty(description = "Set height of List with image and text -1 = automatic, -2 = fill parent")

public void Height(int height){

v.setHeight(height);

}

}


 Compiling Java files
│    warning: The following options were not recognized by any processor: '[output, org, root, version, extName]'
│
│ ERR src\com\faraz\ListWithImageAndText\ListWithImageAndText.java:56: error: cannot find symbol
│    v.setHeight(height);
│     ^
│      symbol:   method setHeight(int)
│      location: variable v of type View
│
│ Total error(s): 1
└ Failed

• Build failed [6s 118ms]
2 Likes

Use this Code :

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import com.google.appinventor.components.runtime.AndroidViewComponent;
.............
.....
.........

    @SimpleFunction(description = "")
    public void SetHeight(AndroidViewComponent component, int height) {
        View view = component.getView();
        LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }
3 Likes

@Salman_Dev you are a good developer

1 Like

Thank you very much @Faraz_Firoz :heart_eyes: :kissing_heart:
You are also a good developer :sunglasses: :+1:

2 Likes

How to import color

1 Like

import android.graphics.Color;

1 Like

In android we use android:divider = red to set divider color of listview
How we use this in building extension I try v.setDivider(color); but this not works

1 Like
           // Set ListView divider color
            lv.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));

            // set ListView divider height
            lv.setDividerHeight(2);

You can try this.
Code from - https://stackoverflow.com/a/56847623/13611846

2 Likes

@Techno_Vedang thankyou I try it tomorrow
What I need to import for ColorDrawable

2 Likes

import android.graphics.drawable.ColorDrawable;

2 Likes

Thankyou both of you two

2 Likes

@SimpleProperty(description = "Set divider color of List with image and text")

public void DividerColor(String color){

v.setDivider(new ColorDrawable(Color.parseColor(color)));

}

Compiling Java files
│ warning: The following options were not recognized by any processor: '[output, org, root, version, extName]'

│ ERR src\com\faraz\ListWithImageAndText\ListWithImageAndText.java:62: error: cannot find symbol
│ v.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));
│ ^
│ symbol: method setDivider(ColorDrawable)
│ location: variable v of type View

│ Total error(s): 1
└ Failed

• Build failed [2s 528ms]

1 Like

What is V in the method DividerColor ?

1 Like

public View v;
View v = (listView);

1 Like

You are missing java concepts... I'll better suggest you to learn methods on Java


Here , you haven't declared v as any variable...
Do somthing like this ;

    public void MethodName(int color , AndroidViewComponent view){
    View v = (View) view.getView();
    //Do whatever you wanted 
    }
2 Likes

My other code run with v.

package com.faraz.ListWithImageAndText;

import com.google.appinventor.components.annotations.SimpleFunction;

import com.google.appinventor.components.annotations.SimpleProperty;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.errors.YailRuntimeError;

import com.google.appinventor.components.runtime.util.YailList;

import com.google.appinventor.components.runtime.HVArrangement;

import android.widget.ListView;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.FrameLayout;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

public class ListWithImageAndText extends AndroidNonvisibleComponent {

public ComponentContainer container;

public View v;

public ListWithImageAndText(ComponentContainer container) {

super(container.$form());

this.container = container;

}

@SimpleFunction(description = "Creates list with image and text in the arrangement that you give")

public void Create(HVArrangement createIn){

View view = createIn.getView();

FrameLayout frameLayout = (FrameLayout) view;

ListView listView = new ListView(this.container.$context());

View v = (listView);

frameLayout.addView(listView, new FrameLayout.LayoutParams(-1, -1));

}

@SimpleProperty(description = "Set height of List with image and text -1 = automatic, -2 = fill parent, 5 = 5px, 0.5 = 5%")

public void Height(int height){

LayoutParams layoutParams = v.getLayoutParams();

layoutParams.height = height;

v.setLayoutParams(layoutParams);

}

@SimpleProperty(description = "Get height of List with image and text -1 = automatic, -2 = fill parent, 5 = 5px, 0.5 = 5%")

public Object Height(){

return v.getHeight();

}

@SimpleProperty(description = "Set width of List with image and text -1 = automatic, -2 = fill parent, 5 = 5px, 0.5 = 5%")

public void Width(int width){

LayoutParams layoutParams = v.getLayoutParams();

layoutParams.width = width;

v.setLayoutParams(layoutParams);

}

@SimpleProperty(description = "Get width of List with image and text -1 = automatic, -2 = fill parent, 5 = 5px, 0.5 = 5%")

public Object Width(){

return v.getWidth();

}

@SimpleProperty(description = "Set divider color of List with image and text")

public void DividerColor(String color){

v.setDivider(new ColorDrawable(Color.parseColor(color)));

}

}

1 Like

You are saying 'v' is Android View but you didn't set v as View and also there is no method setDivider in android.view.View class ...
ListView and View are two different things . ListView is an adapter view which is subclass of View.

1 Like

When I remove v.setDivider It works properly
Build initialized

┌ Checking project files

│ OK Metadata file (rush.yml) found

│ OK AndroidManifest.xml file found
└ Done
┌ Compiling Java files
└ Done
┌ Processing the extension
└ Done
┌ Converting Java bytecode to DEX bytecode
└ Done
┌ Finalizing the build
└ Done

• Build successful [6s 413ms]

2 Likes

Okay. you've added public View v ...

v.setDivider will not work because

1 Like

Then what is this
setDividerHeight(int)

1 Like