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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:43:46+00:00 2026-06-03T06:43:46+00:00

How can I set the gravity and margin for auto-generated(within the code) TextViews and

  • 0

How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.

Here is my generation code:

        for(int i=0; i<num_enter; i++){
    final int cuco = i;
    LinearLayout linlay = new LinearLayout(this);
    linlay.setOrientation(0);
    TextView text = new TextView(this);
    text.setText(name[cuco] + "        ");
    linlay.addView(text);
    TextView num = new TextView(this);
    num.setId(cuco);
    num.setText("" + current[cuco]);
    linlay.addView(num);
    Button minus = new Button(this);
    minus.setText("-");
    minus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] - 1;
            current[cuco] = current[cuco] -1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    minus.setLayoutParams(p);
    linlay.addView(minus);
    Button plus = new Button(this);
    plus.setText("+");
    plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] + 1;
            current[cuco] = current[cuco] + 1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    plus.setHeight(LayoutParams.WRAP_CONTENT);
    plus.setWidth(50);
    plus.setLayoutParams(w);
    linlay.addView(plus);

    main.addView(linlay);
    }

Hope you find a way to set the button gravity without changing it size.

  • 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-03T06:43:48+00:00Added an answer on June 3, 2026 at 6:43 am

    1) Get Reference of TextView say tv.

    2) tv.setGravity(Gravity.CENTER);

    It is not sure work with LinearLayout.. Try Relative Or Absolute

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

Sidebar

Related Questions

I know we can set the following values to the android:gravity and android:layout_gravity properties:
I am currently setting up 3 textviews that go into a LinearLayout(code below). However
Here you can see I've got the LL_BtnR layout inside the longer linearLayout3. LL_BtnR's
How can I dynamically set the gravity ? Bitmap bmp; im = new ImageView
Hi friends i have set my layout thru TabLayout within LinearLayout but still my
I can set the relationship between View Model and view through following DataContext syntax:
You can set the Vim color scheme by issuing :colorscheme SCHEME_NAME but, oddly enough,
I can set the event on the calendar on my device by using this
How can I can set a global variable for the username of the logged-in
I know I can set a CultureInfo object with an specified culture in the

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.