Inflating Custom Linear Layouts at runtime

681 Views Asked by At

Following is my requirement: 1. I have a main linear layout defined. 2. I get an arraylist from an Api.

Now, I want to add custom Linear Layouts with 1 imageView and 1 textView and feed values to these from the API data.

The above linear layouts will be added in the main linear layout with vertical orientation.

Custom Linear Layout class:

public class LandingPageLinearLayout extends LinearLayout{

    private ImageView iv_outletInfo_Icon;
    private TextView tv_outletInfo;

    public LandingPageLinearLayout(Context context, String tv_value, int iv_drawable_id) {
        super(context);
        // TODO Auto-generated constructor stub
        /* View view =  LayoutInflater.from(getContext()).inflate(
                    R.layout.landing_page_ll_custom, null);*/

         LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.landing_page_ll_custom, this, true);


         iv_outletInfo_Icon = (ImageView) getChildAt(0).findViewById(R.id.imgview_outletInfoIcon);
         iv_outletInfo_Icon.setBackgroundResource(iv_drawable_id);

         tv_outletInfo = (TextView) getChildAt(1).findViewById(R.id.tv_outletInfo);
         tv_outletInfo.setText(tv_value);



        // this.addView(view);

    }

}

The xml layout of the Custom Linear Layout:

<?xml version="1.0" encoding="utf-8"?>
<com.iwillcode.interfaces.LandingPageLinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:gravity="center_vertical"
    android:padding="14dp" >

    <ImageView
        android:id="@+id/imgview_outletInfoIcon"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:contentDescription="@null" />

    <TextView
        android:id="@+id/tv_outletInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="3.5"
        android:textColor="@color/text_color"
        android:textSize="12sp" />

</com.iwillcode.interfaces.LandingPageLinearLayout>

The method called in the activity:

 private void generateTextViews(int n, LinearLayout mainLinearLayout){
        // Dynamic generation of TextViews as per requirement
        // n = size of arrayList

        final LandingPageLinearLayout[] myTextViews = new LandingPageLinearLayout[n];
         hashMap = new HashMap<String,String>();

         //RatingBar Linear Layout


        for (int i = 0; i < n; i++) {
            // create a new Custom Linear Layout

            hashMap = arraylist_outletInfo.get(i);

            String name = hashMap.get(Config.KEY_NAME);
            String value = hashMap.get("value"); 

            if(name.equalsIgnoreCase("rating")){
                ll_ratingLayout.setVisibility(View.VISIBLE);
                 tv_rating.setText(hashMap.get("value"));
                 ratingBar.setRating(Float.parseFloat(value));

            }

            final LandingPageLinearLayout rowCustomView = new LandingPageLinearLayout(context,value,R.drawable.outlet_icon);

            // Set text of rowTextView
           // rowTextView.setText("This is row #" + i+value);

            // Condition for alternate colors

            if(n%2==0){
                rowCustomView.setBackgroundColor(Color.WHITE);
              }else{
                rowCustomView.setBackgroundColor(Color.parseColor("#f2f2f2"));
              }         

            // add the textview to the linearlayout
            mainLinearLayout.addView(rowCustomView);

            // save a reference to the textview for later
           // myTextViews[i] = rowTextView;
        }


    }

LogCat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goldvip.crownit/com.goldvip.crownit.LandingPageActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
       at android.app.ActivityThread.access$800(ActivityThread.java:149)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:211)
       at android.app.ActivityThread.main(ActivityThread.java:5315)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout
       at android.view.LayoutInflater.createView(LayoutInflater.java:616)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
       at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26)
       at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777)
       at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269)
       at android.app.Activity.performCreate(Activity.java:5933)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
       at android.app.ActivityThread.access$800(ActivityThread.java:149)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:211)
       at android.app.ActivityThread.main(ActivityThread.java:5315)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
       at java.lang.Class.getConstructor(Class.java:531)
       at java.lang.Class.getConstructor(Class.java:495)
       at android.view.LayoutInflater.createView(LayoutInflater.java:580)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
       at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26)
       at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777)
       at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269)
       at android.app.Activity.performCreate(Activity.java:5933)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
       at android.app.ActivityThread.access$800(ActivityThread.java:149)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:211)
       at android.app.ActivityThread.main(ActivityThread.java:5315)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)

The App crashes when the activity is loaded.

3

There are 3 best solutions below

0
Blackbelt On

if you want to the declare your custom view in the layout you need, at least, the constructor that takes Context and AttributeSet as parameter

public LandingPageLinearLayout(Context context, AttributeSet attr) {
        super(context, attr);

And you should avoid to overload the View's constructor. There is the possibility to declare custom attributes for your custom layout.

0
parthpatibandha On

Hope it will helps you.

Add new line in XML after xmlns:android="http://schemas.android.com/apk/res/android"

-New line : xmlns:app="http://schemas.android.com/apk/res/YourMainPackageName"

-Clean and build project.

  • You have to add this when you are using any library.
0
narancs On

Logcat says: Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]

you forgot to implement one of super class constructor, namely: LinearLayout(Context context, AttributeSet attrs)

It`s getting called, because you are using your view in an XML. Make sure to implement this two constructor as well:

LinearLayout(Context context, AttributeSet attrs)

LinearLayout(Context context, AttributeSet attrs, int defStyleAttr)