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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:51:14+00:00 2026-06-11T12:51:14+00:00

I am trying to set a drawable image as a background to a LinearLayout

  • 0

I am trying to set a drawable image as a background to a LinearLayout by stretching the image to cover the entire area, without distorting its proportions (meaning that some of the sides of the image may be out of the screen, which is fine by me).

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  // TODO Auto-generated method stub
  super.onWindowFocusChanged(hasFocus);

    LinearLayout mainLL = (LinearLayout) findViewById(R.id.main_ll);

    Integer lWidth = mainLL.getWidth();
    Integer lHeight = mainLL.getHeight();

    // Read your drawable from somewhere
    Drawable dr = getResources().getDrawable(R.drawable.cash_bg);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    // Scale it the LinearLayout's size
    Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, lWidth, lHeight, true));

    mainLL.setBackgroundDrawable(d);
 }

I am getting the layout’s width and height in the onWindowFocusChanged instead of onCreate, because I heard otherwise they will return 0 (since the layout is not fully rendered in the constructor). The image is not that large so I don’t know why this out of memory issue occurs. It’s 800x945px JPG with 113kb size.

Here’s the error log:

09-13 11:00:00.067: E/AndroidRuntime(2010): FATAL EXCEPTION: main
09-13 11:00:00.067: E/AndroidRuntime(2010): java.lang.OutOfMemoryError
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.Bitmap.nativeCreate(Native Method)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.content.res.Resources.loadDrawable(Resources.java:1937)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.content.res.Resources.getDrawable(Resources.java:664)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at com.myapp.something.FVCalculator.onWindowFocusChanged(FVCalculator.java:66)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2346)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.view.View.dispatchWindowFocusChanged(View.java:5676)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:853)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2530)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.os.Looper.loop(Looper.java:137)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at java.lang.reflect.Method.invoke(Method.java:511)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-13 11:00:00.067: E/AndroidRuntime(2010):     at dalvik.system.NativeStart.main(Native Method)
  • 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-06-11T12:51:15+00:00Added an answer on June 11, 2026 at 12:51 pm

    It seems like setting the background of the LinearLayout in the XML is not affected by this out-of-memory issue (maybe the image is loaded in a different manner).

    Anyway in my case using just this solved it in the layout file solved it:

    android:background="@drawable/cash_bg"
    

    I don’t know how it will center though (and if it will cover the entire container).

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

Sidebar

Related Questions

I've been trying to set a background image on a gtk widget without success,
I am trying to set the background color of an image view. <ImageView android:id=@+id/my_image
I am trying to set a 9-patch image as the background for one of
I am trying to use a nine patch image as a background in an
I'm trying to set a .PNG file as background on Canvas in my app.
I'm trying to set a drawable in a list. My list doesn't contain just
I am trying to set an image to a WebView for my app. I
This seems simple, I am trying to set a bitmap image but from the
I'm trying to create a listview with a static background image (i.e. not contained
I am trying to set border on imagview , once i set image on

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.