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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:46:57+00:00 2026-05-24T17:46:57+00:00

I find myself needing to create a View completely in Java without knowing what

  • 0

I find myself needing to create a View completely in Java without knowing what concrete type the parent is.

example:

public View getView(int position, View convertView, ViewGroup parent){
    if(null == convertView){
        convertView = new TextView(parent.getContext());
    }
    ((TextView) convertView).setText(getItem(position).getName());
}

Now suppose I wanted to change this so that the convertView was wrap_content in both directions. Since this is an Adapter, I’d like to avoid coupling the Adapter with the concrete type of the parent, but the LayoutParams I give it in setLayoutParams() has to be the correct concrete type otherwise the app will crash (i.e. if parent is a ListView it has to be ListView.LayoutParams, if it’s a LinearLayout it must be a LinearLayout.LayoutParams, etc.). I don’t want to use a switch statement either since that’s just a more flexible form of coupling, and if I attach this adapter to a view I didn’t anticipate I still end up with a crash. Is there a generic way to do this?

  • 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-24T17:46:58+00:00Added an answer on May 24, 2026 at 5:46 pm

    You can do this using the following code:

    LayoutParams params = parent.generateLayoutParams(null);
    

    EDIT:
    The method above doesn’t work because ViewGroup.generateLayoutParams() requires android:layout_width and android:layout_height to be set in the passed AttributeSet.

    If you use ViewGroup.LayoutParams with any layout then everything will work fine. But if you use LinearLayout.LayoutParams with RelativeLayout for example, then an exception will be thrown.

    EDIT:
    There’s one working solution for this problem which I don’t really like. The solution is to call generateLayoutParams() with valid AttributeSet. You can create an AttributeSet object using at least two different approaches. One of them I’ve implemented:

    res\layout\params.xml:

    <?xml version="1.0" encoding="utf-8"?>
    
    <view xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dip" />
    

    SomeActivity.java:

    private void addView(ViewGroup viewGroup, View view) {
        viewGroup.addView(view);
        view.setLayoutParams(generateLayoutParams(viewGroup));
    }
    
    private ViewGroup.LayoutParams generateLayoutParams(ViewGroup viewGroup) {
        XmlResourceParser parser = getResources().getLayout(R.layout.params);
        try {
            while(parser.nextToken() != XmlPullParser.START_TAG) {
                // Skip everything until the view tag.
            }
            return viewGroup.generateLayoutParams(parser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    

    Another way to create an AttributeSet object is to implement AttributeSet interface and make it return android:layout_width, android:layout_height and other layout attributes you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself needing to store public key certificates, and a single private key
When writing comments, I sometimes find myself needing to talk about a type (class,
I find myself needing to generate a checksum for a string of data, for
I often find myself needing reference to an object that is several objects away,
I always find myself needing to enclose a block of code in curly braces
I find myself needing to return various JSON results to the client from my
I find myself needing to use Perl more and more since PHP's regular expressions
I find myself often needing addClassName and removeClassName functions for Element s. However, I
I often find myself needing a quick ( in terms of code ), lightweight
Every few months I find myself needing to call Win32 from C#. Though I've

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.