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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:26:14+00:00 2026-05-25T18:26:14+00:00

I’m using a FrameLayout to display (on demand) some text on the screen. I

  • 0

I’m using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job… but no, it seems to have no effect whatsoever on where the text goes (and other objects like ImageView don’t even have this method).

So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)

Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout… say, without creating a new LayoutParams object as I’m doing now?

Thanks! For reference, below is a (working) code snippet showing how I’m setting up this FrameLayout and TextView.

    FrameLayout fl1 = new FrameLayout(this);
    FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    fl1.setId(9001);
    fl1.setLayoutParams(flp1);
      .....
    tv1 = new TextView(this);
    FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
        (Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
    tv1.setId(9006);
    tv1.setLayoutParams(tvp1); // This works
    tv1.setBackgroundColor(Color.GRAY);
    tv1.setTextColor(Color.BLACK);
    tv1.setText("Dynamic layouts ftw!");
    tv1.setGravity(Gravity.CENTER); // This does NOT work
     .....
    fl1.addView(tv1);
  • 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-25T18:26:15+00:00Added an answer on May 25, 2026 at 6:26 pm

    1) view.setGravity means hows the view should positions if children.
    In the case of the textview it refers to the positioning of the text.
    When in linearlayouts or viewgroups it refers to its child views.

    2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.

    Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.

    Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.

    If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.

    This should give what your are trying to do

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    FrameLayout fr = new FrameLayout(this);
    fr.setLayoutParams(lp);
    fr.setBackgroundColor(Color.RED);
    
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    LinearLayout l = new LinearLayout(this);
    l.setLayoutParams(lp2);
    l.setGravity(Gravity.CENTER);
    l.setBackgroundColor(Color.BLUE);
    
    ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    TextView t = new TextView(this);
    t.setLayoutParams(vp);
    t.setText("Blessan Mathew");
    t.setBackgroundColor(Color.CYAN);
    
    l.addView(t);
    fr.addView(l);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
i want to parse a xhtml file and display in UITableView. what is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is

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.