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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:04:17+00:00 2026-05-26T06:04:17+00:00

How to set programmatically width of TextView to be 0dp ? I am trying

  • 0

How to set programmatically width of TextView to be 0dp ? I am trying to put in LinearLayout couple TextViews but all to be same width, and when I put in xml like

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
 >
 <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
                    />
 <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
        />
 <TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="test"
android:layout_gravity="right"
android:gravity="right"
        />
</LinearLayout>

but when in code try like

TextView txtTemp = new TextView(this);
txtTemp.setText(captures.get(i));
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
                        .getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;

if (i == 0) {
    tempLL.gravity = Gravity.LEFT;
        }
if (i == (captures.size() - 1)) {
    tempLL.gravity = Gravity.RIGHT;
} else {
    tempLL.gravity = Gravity.CENTER;
                }
    txtTemp.setLayoutParams(tempLL);

I got exception on tempLL.width null exception. How to solve 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-26T06:04:17+00:00Added an answer on May 26, 2026 at 6:04 am

    Instead of using

    LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
                        .getLayoutParams();
                tempLL.width =0;
                tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                tempLL.weight = 1;
    

    try this

    LinearLayout.LayoutParams tempLL = new LinearLayout.LayoutParams(0,
            LayoutParams.WRAP_CONTENT,1.0);
    txtTemp.setLayoutParams(tempLL);
    

    You are creating a new textview and there is no layoutparams specified yet. So getLayoutParams should return null. So create a new layoutparam object and set that to the new textview.

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

Sidebar

Related Questions

I would like to programmatically set maxLength property of TextView as I don't want
I'd like to set a connection string programmatically, with absolutely no change to any
I'm trying to programmatically set the constructor sting of a COM+ component from a
How do you set the width of a textbox to fill the container programmatically?
Other questions say that the style cannot be set programmatically, but a View can
How can I set an ImageView 's width and height programmatically?
Is there a way to set the textStyle attribute of a TextView programmatically? There
I'm trying to set the size of a UIImageView programmatically using: [thisImageView setFrame:CGRectMake(x, y,
So, I have a GridView with an ObjectDataSource, and I want to programmatically set
How do I programmatically set the record pointer in a C# DataGridView? I've tried

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.