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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:41:14+00:00 2026-06-12T11:41:14+00:00

I want to get the width of a LinearLayout element in my getView method

  • 0

I want to get the width of a LinearLayout element in my getView method of my custom adapter. My getView method looks like this:

@Override
    public View getView(int position, View v, ViewGroup parent) {
        View mView = v;

        if (mView == null) {

            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(R.layout.levelselector_item, null);


        } 
if (mView != null) {
...
LinearLayout ln = (LinearLayout) mView.findViewById(R.id.lineScore);
            LinearLayout lnTotal = (LinearLayout) mView.findViewById(R.id.lineScoreTotal);
int widthTotal = lnTotal.getWidth()/2;
                ln.getLayoutParams().height = 3;
                ln.getLayoutParams().width = widthTotal;
                ln.requestLayout();
                ln.setBackgroundColor(Color.parseColor("#eef05e"));

}
        return mView;
    }

The problem my widthTotal doesn’t seem to have a value. If I click on an item of the gridview and hit the back button, then I suddenly see a yellow bar. If I enter ln.getLayoutParams().width = 20; he also shows a yellow short bar. I just don’t know when or where I can get the width of the LinearLayout…

  • 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-12T11:41:15+00:00Added an answer on June 12, 2026 at 11:41 am

    I got it to work properly. For those who might be interested:

    I first set my border around my gridview to an amount which is controlable by adding to my gridview in xml:

    android:listSelector="@null"
    android:layout_margin="5dp"
    

    Then I used a combination of Ted Hopp’s and Sameer’s solution: Since I know what the padding of my elements is; 5dp around the gridview, 2dp between the elements and 15dp in an element. I also have 3 items each row, this makes my code look like:

    int parentW = parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight();
                    Resources r = C.getResources();
                    float pxPaddingBetweenItem = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics());
                    float pxPaddingInItem = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, r.getDisplayMetrics());
    
                    int totalBarWidth = (parentW - (int)(2*pxPaddingBetweenItem)  - (int)(6*pxPaddingInItem))/3;
    

    And this takes me exactly where I want to be 🙂
    Thanks!

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

Sidebar

Related Questions

I want to get width and height (in pixels) of images which are stored
I want to get the Height and Width of an Image.Currently what im doing
I want to get the height and width of a .cur file without look
I want to get aspect ratio of a monitor as two digits : width
I am new to iPhone programming. I want to get the height and width
My layout looks like I want it to... up to the point that I
I creating LinearLayout programatically then I want to attach it to GridView via custom
I want to show custom dialog with a spinner. Strangely enough, I get NullPointerException
I have quite a complicated ListView . Each item looks something like this: >
I want to get start with DayPilot control I do not use SQLite and

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.