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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:59:39+00:00 2026-06-04T10:59:39+00:00

I made one horizontalfieldmaanger which will take full width and 10% height of screen.

  • 0

enter image description hereI made one horizontalfieldmaanger which will take full width and 10% height of screen. Now I want to add 3 fields to this manager respectively. First will be BitmapField(10% width of screen), second will be LabelField(80% width of screen), third will be BitmapField(10% width of screen).

Its all done thing that is remaining is centering contents of these fields to centre within field itself.

Here is code:-

public final class MyScreen extends MainScreen
{
public MyScreen()
{   
    final int disWidth = Display.getWidth();
    final int disHeight = Display.getHeight();
    final int heightHFM = (disHeight*10)/100;

    HorizontalFieldManager hfm = new HorizontalFieldManager(){
        protected void sublayout(int maxWidth, int maxHeight) {
            // TODO Auto-generated method stub
            super.sublayout(maxWidth, maxHeight);
            this.setExtent(disWidth,heightHFM);

    }
    };
    hfm.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));

    BitmapField bmp_leftArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*10)/100, height);
        }
    };
    bmp_leftArrow.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));

    LabelField lbl_tit = new LabelField("Current Date"){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*80)/100, height);
        }
    };
    lbl_tit.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
    BitmapField bmp_rightArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*10)/100, height);
        }
    };
    bmp_rightArrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
    hfm.add(bmp_leftArrow);
    hfm.add(lbl_tit);
    hfm.add(bmp_rightArrow);
    add(hfm);

}
}

Need to achieve same output when we use gravity of any view in android

  • 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-04T10:59:41+00:00Added an answer on June 4, 2026 at 10:59 am

    For custom positioning and alignment of the child fields, A dedicated FieldManger with specified layout and field positioning is useful. You can try the following CustomHorizontalFieldManager which extends Manager.

    Generated Output

    enter image description here

    Using the Manager

    public class MyScreen extends MainScreen
    {
        public MyScreen()
        {        
            CustomHorizontalFieldManager chfm = new CustomHorizontalFieldManager();
            Background bg = BackgroundFactory.createSolidBackground(Color.BROWN);
            chfm.setBackground(bg);
    
            BitmapField bmf0 = new BitmapField(Bitmap.getBitmapResource("img0.png"));
            BitmapField bmf1 = new BitmapField(Bitmap.getBitmapResource("img1.png"));
    
            CutomLabelField lblf = new CutomLabelField("Current Date");
    
            bg = BackgroundFactory.createSolidBackground(Color.GREEN);
            lblf.setBackground(bg);
    
            bg = BackgroundFactory.createSolidBackground(Color.GREEN);
            bmf0.setBackground(bg);
    
            bg = BackgroundFactory.createSolidBackground(Color.GREEN);
            bmf1.setBackground(bg);
    
            chfm.add(bmf0);
            chfm.add(lblf);
            chfm.add(bmf1);
    
            add(chfm);
        }
    }
    

    Implementation of the Manager

    class CustomHorizontalFieldManager extends Manager {
    
        public CustomHorizontalFieldManager() {
            super(0);
        }
    
        private int width0, width1, width2;
        private Field f0, f1, f2;
        int x0, x1, x2, y0, y1, y2;
    
        protected void sublayout(int width, int height) {
            if (getFieldCount() == 3) {
                // calculate total width, total height
                width = Display.getWidth();
                height = getPercentage(Display.getHeight(), 10);
    
                // calculate field's width
                width0 = getPercentage(width, 10);
                width1 = getPercentage(width, 80);
                width2 = getPercentage(width, 10);
    
                f0 = getField(0);
                f1 = getField(1);
                f2 = getField(2);
    
                // layout all the child
                layoutChild(f0, width0, height);
                layoutChild(f1, width1, height);
                layoutChild(f2, width2, height);
    
                // Specify position of the child.           
                // Alignment of the fields can be adjusted here.
                // Following lines will align them
                // on center, both vertically and horizontally.
                x0 = (width0 - f0.getWidth()) / 2;
                x1 = width0 + (width1 - f1.getWidth()) / 2;
                x2 = width0 + width1 + (width2 - f2.getWidth()) / 2;
    
                y0 = (height - f0.getHeight()) / 2;
                y1 = (height - f1.getHeight()) / 2;
                y2 = (height - f2.getHeight()) / 2;
    
                setPositionChild(f0, x0, y0);
                setPositionChild(f1, x1, y1);
                setPositionChild(f2, x2, y2);
    
                setExtent(width, height);
            } else {
                // The manager contains some
                // invalid number of fields.
    
                // Make manager invisible.
                setExtent(0, 0);
            }
        }   
    
        int getPercentage(int value, int percent) {
            return (value * percent) / 100;
        }
    }
    

    Draft implementation of CustomLabelField

    class CutomLabelField extends Field {
        private String text;
    
        public CutomLabelField(String text) {
            this.text = (text == null) ? "" : text;
        }
    
        int xText;
        int yText;  
        int availableWidth;
    
        protected void layout(int width, int height) {      
            // positioning text.
            int textWidth = getFont().getAdvance(text);
            availableWidth = width - getPaddingLeft() - getPaddingRight();
            if (availableWidth < textWidth) {
                xText = getPaddingLeft();
            } else {
                xText = getPaddingLeft() + (availableWidth - textWidth) / 2;
            }
            yText = (height - getFont().getHeight()) / 2;
    
            // using all width and height
            setExtent(width, height);
        }
    
        protected void paint(Graphics graphics) {
            // set text color
            graphics.setColor(Color.BLACK);
            graphics.drawText(text, xText, yText, DrawStyle.ELLIPSIS, availableWidth);
        }
    
        public void setText(String text) {
            this.text = (text == null) ? "" : text;
            updateLayout();
        }
    
        public String getText() {
            return this.text;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made one file say a.php. Now I want some thing like if
I made this bash one-liner which I use to list Weblogic instances running along
I have made one project in java. Now in this particular module, i am
I have made one sample project which can add and edit records from SQLite
I have made one application for epaper/emagazine in which I want to give an
I have made one screen with two images and I would like to add
So this was working up until I made one change, and now it's only
I have made one windows phone based application. i want some designing ideas from
I have made one JQgrid which calls Ajax for getting data on loading the
I have an image which is just made of one color ? (it could

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.