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 6534691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:16:51+00:00 2026-05-25T10:16:51+00:00

I have a custom class (touchbutton) extending the TextView class. I am having trouble

  • 0

I have a custom class (touchbutton) extending the TextView class. I am having trouble resizing the button manually. I can get the button to work for only one type of layout or container, but not both. For the most part, Touchbutton is in gridviews so my method to change the size is as so:

private void setLayout(buttonsize_t size) {

    log("Setting Layout: "+buttonsize_t.getPxl(size));

    final float scale = getContext().getResources().getDisplayMetrics().density;
    int dim = (int) (buttonsize_t.getPxl(size) * scale + 0.5f);

    AbsListView.LayoutParams params = (AbsListView.LayoutParams) getLayoutParams();
    if (params != null) {
        params.height = dim;
        params.width = dim;
    }
    else {
        params = new AbsListView.LayoutParams(dim,dim);
    }
    setLayoutParams(params);
}

However, when the TouchButton is resized in a LinearLayout (for example) I get a crash with the Logcat:

09-01 19:18:35.630: ERROR/AndroidRuntime(20793): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
09-01 19:18:35.630: ERROR/AndroidRuntime(20793):     at com.ians.aac3.TouchButton.setLayout(TouchButton.java:204)

Line 204 refers to the instantiation of params.

I noticed that both GridView and LinearLayout share the parent ViewGroup, so i tried using ViewGroup.LayoutParams. This, however, this will lead to the same behavior for gridviews (logcat citing the same line).

Does anyone know how I might make this work for any type of layout or widget?

UPDATE:
As recommended i tried with View group again:

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
.....
ViewGroup.LayoutParams params = getLayoutParams();
        if (params != null) {
            params.height = dim;
            params.width = dim;
        }
        else {
            params = new LayoutParams(dim,dim);
        }
        setLayoutParams(params);

or without trying to recycle the current layoutparams:

setLayoutParams(new ViewGroup.LayoutParams(dim, dim));

And i get the same type of error:

09-02 09:10:49.680: ERROR/AndroidRuntime(7069): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.GridView.onMeasure(GridView.java:1028)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.View.measure(View.java:10828)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.View.measure(View.java:10828)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)

Update 2:
It seems that using the ViewGroup.LayoutParams specified above works for the LinearLayout. However, as seen above, the gridview doesn’t like it…

  • 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-25T10:16:53+00:00Added an answer on May 25, 2026 at 10:16 am

    To understand what happens here you need to look in the Android source. There is a method in ViewGroup generateLayoutParams which JavaDoc states:

    Returns a safe set of layout parameters based on the supplied layout
    params. When a ViewGroup is passed a View whose layout params do not
    pass the test of
    checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is
    invoked. This method should return a new set of layout params suitable
    for this ViewGroup, possibly by copying the appropriate attributes
    from the specified set of layout params.

    If you look at LinearLayout and AbsListView (the parent of GridView) source you’ll see they convert their children layout params to LinearsLayout.LayoutParams and AbsListView.LayoutParams respectively. But this conversion only happens when the child is added to the layout.
    Thus if you add your TouchButton to the LinearLayout (programmaticaly or via XML) it will receive LinearsLayout.LayoutParams and, if you add it to the GridView (via an adapter) it receives AbsListView.LayoutParams.
    But if you set layout params manually afterwards, you will get ClassCastException somewhere in the parent container code (since it expects its children layout params to be of some specific type).

    For the solution of your issue I suggest you the following:

    private void setLayout(buttonsize_t size) {
    
        log("Setting Layout: "+buttonsize_t.getPxl(size));
    
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int dim = (int) (buttonsize_t.getPxl(size) * scale + 0.5f);
    
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) getLayoutParams();
        if (params != null) {  
            params.height = dim;
            params.width = dim;
            setLayoutParams(params);
        } else {
          // the LayoutParams was not set yet, it must be the GridView 
          // which delays setting till child rendering
          params = new AbsListView.LayoutParams(dim, dim);
          setLayoutParams(params);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have a custom class in a list. I can't seem to get
I have a custom class loader so that a desktop application can dynamically start
I have a custom class that uses boost mutexes and locks like this (only
I have a custom ButtonUI class that paints the button. Before drawing the text,
I have a custom class that is currently not extending anything (it's for executing
I have a custom class: public class Person { public String Name { get;
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I have a custom class Contact . I am trying to bind a List<Contact>
I have a custom class that has as an instance variable of a coordinate:

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.