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

  • Home
  • SEARCH
  • 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 7037563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:33:38+00:00 2026-05-28T01:33:38+00:00

I try to create a View programaticaly by inflating an layout. View marker =

  • 0

I try to create a View programaticaly by inflating an layout.

View marker = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.overlay_view, null);

Now i want to convert this View to a Bitmap:

public static Bitmap createDrawableFromView(Context context, View view){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);

        Bitmap cache = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(cache);
        view.draw(canvas);

        return cache;
    }

I also tried measure(1, 1) and layout(0 ,0 ,0 ,0) without any changes.
The Bitmap looks like the filled layout.
Now the Problem: If i put something into the View thats makes the View bigger than the Displaysize in width or height (example: a TextView with a long text), the Bitmap is also bigger.
The Views XML width and height is WRAP_CONTENT but it didnt works.
I never put this View somewhere to display it (becouse i only need the Bitmap).

Question: How can i WRAP the View to a maximum of width = displayWidth?

I tried view.setLayoutParams(new LayoutParam(100, 100)) but it was also bigger than 100?! I really dont understand whats happened, when i inflate the layout and set the content of the View.

The view.getMeasuredWidth and Height is always bigger as my display?! (if i put something bigger in it)

Here is an Example of the Layout:

<RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/somedrawable"
        android:padding="10dp"
        android:clickable="true">
        <TextView android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"/>
        <TextView android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/name"
            android:text="Description"
            android:textSize="20dp"
            android:textColor="#ffffff"/>
    </RelativeLayout>
  • 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-28T01:33:38+00:00Added an answer on May 28, 2026 at 1:33 am

    I found a solution:

    first step: add new LayoutParams to the view. This is important, becouse without you will get a NullPointer.

    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    

    Second:

    view.measure(MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels), MeasureSpec.AT_MOST),
                 MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST));
    

    for view.measure() i used MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST));. First parameter is the display width or heigt and the second is the mode AT_MOST, this is needed to “wrap” the View.

    Works great! (for me :p )

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

Sidebar

Related Questions

When we try to create a view within a funcion we get ERROR: there
I try to create 2 buttons inside my app case WM_CREATE:{ hWnd =CreateWindowEx(NULL, LBUTTON,
When I try to create indexed view I am getting an error datepart returns
I try to create a view without using xib file. Unfortunately, I have problems
In order to try the memory leaks instrument tool, I create a view-based ipad
I try to create a new file inside a JSP and try to save
I try to create a thread in QT, can declare, create and start it,
I try to create a very simple app using windows API. I've done some
i try to create an AEP for my advantage Database. I create a AEP
I try to create a multi-threaded singleton pattern class. Header: class HL{ 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.