Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7663113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:32+00:00 2026-05-31T13:58:32+00:00

I creating LinearLayout programatically then I want to attach it to GridView via custom

  • 0

I creating LinearLayout programatically then I want to attach it to GridView via custom written adapter. I want to set width and height to my newly created LinearLayout. When I try to do so I get following exception:

03-18 15:50:54.648: E/AndroidRuntime(2708): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.GridView.onMeasure(GridView.java:937)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.os.Looper.loop(Looper.java:130)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at java.lang.reflect.Method.invoke(Method.java:507)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 15:50:54.648: E/AndroidRuntime(2708):     at dalvik.system.NativeStart.main(Native Method)

Here is my code:

public View getView(int position, View convertView, ViewGroup parent){
  LinearLayout layout = new LinearLayout(activity);

  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
  layout.setLayoutParams(params);

  return layout
}

Any help will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:58:33+00:00Added an answer on May 31, 2026 at 1:58 pm

    Try it with GridView.LayoutParams. The LayoutParams have to be the params of the parent.

    public View getView(int position, View convertView, ViewGroup parent){
      LinearLayout layout = new LinearLayout(activity);
      layout.setLayoutParams(new GridView.LayoutParams(100, 100));
      return layout
    }
    

    btw the convertView should be used if it is != null. That’s faster.

    public View getView(int position, View convertView, ViewGroup parent){
      View result = convertView;
      if (result == null) {
          result = new LinearLayout(activity);
          result.setLayoutParams(new GridView.LayoutParams(100, 100));
      }
      // set attributes based on position here
      return result;
    }
    

    Edit:

    // using ViewGroup
    public View getView(int position, View convertView, ViewGroup parent){
      ViewGroup result = null;
      if (convertView instanceof ViewGroup) {
          result = (ViewGroup) convertView;
      } else {
         // that will never happen if you never return anything but ViewGroups
      }
    
      if (result == null) {
          // since LinearLayout is a (extends) ViewGroup
          result = new LinearLayout(activity);
          result.setLayoutParams(new GridView.LayoutParams(100, 100));
      }
      // set attributes based on position here
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a custom widget by extending LinearLayout : public class MyWidget extends LinearLayout
I want to add a custom Dialog, but I have problems creating a xml
I am creating a Spinner instance that I bind to my own custom adapter
i am creating an application in which i want set different background images in
I have a problem with a custom LinearLayout I'm creating for an application. Basically,
I am creating a Custom dialog where I want to use both DatePicker and
I am creating a custom compounding control extending LinearLayout. Inside of its constructor, program
I am in the process of creating a LinearLayout and within this layout...there will
I'm creating a custom Layout for android. The layout implementation is exactly the same,
I am creating the bitmap from a linearlayout with the following code public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.