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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:38:25+00:00 2026-06-16T07:38:25+00:00

My activity has a LinearLayout with a single child view. I want both to

  • 0

My activity has a LinearLayout with a single child view. I want both to fill the screen, minus a 12 dp margin.

Unfortunately, the child view is drawn 12 dp too big and gets cut off. Apparently match_parent ignores the layout_margin attribute when calculating the size of the child view. What is the simplest way to fix this?

myActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <com.myapp.myView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

myActivity.java

package com.myapp;

import android.app.Activity;
import android.os.Bundle;

public class myActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myActivity);
    }
}

myView.java

package com.myapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class myView extends View {

    private Paint paint = new Paint();

    public myView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(0xFFFF0000); //red
        paint.setStyle(Paint.Style.STROKE); // for unfilled rectangles
        paint.setStrokeWidth(4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size = canvas.getWidth(); // width = height (see onMeasure())
        canvas.drawRect(0, 0, size, size, paint);   
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        // This gives us a square canvas!
    }
}
  • 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-16T07:38:26+00:00Added an answer on June 16, 2026 at 7:38 am

    Child views can have a margin around them, parent views (or view groups like layouts) can have a padding between their boundaries and their child views. In other words, margin is outside a view, padding is inside.

    Also, see this excellent explanation: Difference between a View's Padding and Margin

    Example with standard View and padding instead of margin:

    I created a little example with a standard view instead of your custom one, and using padding for the LinearLayout as suggested above and it works perfectly (see screenshot):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="12dp"
        android:orientation="vertical" >
    
        <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#ff0000"/>
    </LinearLayout>
    

    screenshot from layout editor

    Solution

    Turns out, the problem was your using canvas.getWidth() in your custom view’s onDraw method. Using the view’s getWidth() instead, solved the problem. Finally 🙂

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

Sidebar

Related Questions

I created an Activity that has a title and a web view in a
I have a View that inherits from LinearLayout that inflates itself. After it has
I have a basic activity (SherlockActivity subclass), and the view it loads has a
I have a LinearLayout (holder), which has match_parent for both width and height. In
I want to create an activity that has sidebar on the left that is
I have an Activity with a main screen that simply has a title bar
I have a LinearLayout view that already contains several elements. I want to add
What's this error? 11-25 09:38:33.233: ERROR/WindowManager(248): Activity com.android.browser.BrowserActivity has leaked window android.widget.LinearLayout@44f997e0 that was
Right now, the label for my Activity has the name of the current Activity.
Assume that we have 3 different activities. Each activity has its own XML file

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.